linux学习笔记(一) make命令和Makefile

要使用make,必须编写一个叫Makefile的文件,它描述了软件包中各个文件之间的关系,提供了更新每一个文件的命令,在一个软件包里,通常是可执行文件由链接目标文件更新,而且目标文件由编译源文件更新! 当一个适当的Makefile存在时,每次改变某些源文件,用简单的shell命令 make 将足以文成所有必须的重新编译。比如假设应用程序名为exel,它由2个目标代码模块组成,分别是module1和module2,如果键入如下命令:

  

gcc module1.o module2.o -o exel

生成了可执行文件exel,可以用make来表示目标、依赖模块和命令的相关行为:

exel:module1.o module2.o

    gcc module1.o module2.o -o exel

 若module1.o依赖module1.c和头文件module1.h,module2.o依赖module2.c和头文件module2.h,这些依赖关系可以写成

module1.o:module1.c module1.h
                gcc -c module1.c

module2.o:module2.c module2.h
                gcc -c module2.c

综上所述,Majefile文件格式如下:

目标:依赖项列表
        
    【命令】

 Makefile文件也可以在描述语句前面加#表示注释,make程序将跳过此行不执行,相关行如果过长,还可以使用反斜线"\"作为后接换行符来续行

 make程序执行Makefile的相关行的默认情况是将执行状态显示出来,如果在相关行前加"@",就可以避免显示该行

 

上面给出的exel程序完整的Makefile文件

exel:module1.o module2.o

    gcc module1.o module2.o -o exel

module1.o:module1.c module1.h
                gcc -c module1.c

module2.o:module2.c module2.h
                gcc -c module2.c

clean:
    rm -f exel *.o

在Makefile文件中除了依赖关系的描述外,还可以含有宏。宏是代表文件名和命令任选项的短名

 

Makefile中的条件语句(ifeq,else,endif)

ifeq($(VAR),1)

    gcc -o exe1 module

else
    gcc -o exe2  module

endif

上述条件说明在变量VAR=1时,把module模块编译输出文件为exe1,不等于1时,把module模块编译输出文件为exe2

 

posted @ 2014-11-06 10:35  andy_叶  阅读(183)  评论(0)    收藏  举报