makefile

完整大神版链接:跟我一起写 Makefile  ---  http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=408225&fromuid=28610854


编写规则:

以#开始的为注释

具体编译过程:

对象:依赖项

编译方式

注:makefile文本向右缩进时使用TAB键,不能用空格代替


cc=g++

exe=main

obj=main.o file1.o file2.o

$(exe):$(obj) //依赖关系

$(cc) -o $(exe) $(obj)//执行动作

main.o:main.cpp file1.h file2.h

$(cc) -c main.cpp

file1.o:file1.cpp file1.h

$(cc) -c file1.cpp

file2.o:file2.cpp file2.h

$(cc) -c file2.cpp

clean:

rm -fr *.o $(exe)


make target

告诉make该执行哪个目标,而忽略其他的目标。eg:输入make hello,就只有hello被执行。

如果只输入make命令,make总会寻找第一个目标,并在执行完以后就不管其他的目标了。

eg:

install:

cp hello /home/garfield

install这个目标不依赖任何其他的东西,一但输入make install,这个目标下的所有命令都被执行。这种情况下hello将被安装到用户的目录下。应用程序的makefile正是这样写的,以便程序在正确编译后可以被安装到正确的目录。


原文地址:https://www.cnblogs.com/garfieldx/p/3381470.html