Makefile 笔记
来源:互联网 发布:犯罪心理学 知乎 编辑:程序博客网 时间:2024/05/05 15:51
a.cpp -(编译)-> a.oa.o -(链接)-> a.out
我们可以通过g++命令行方式完成以上操作:
g++ -c a.cpp -o a.o
g++ a.o -o a.out
我们还可以写一个makefile,然后用make命令完成以上操作:
a.out: a.o g++ a.o -o a.outa.o: a.cpp g++ -c a.cpp -o a.o然后执行
make makefile
这样看来makefile并没有什么特别的用途,对我而言
首先,每次只敲"make makefile"要比每次都敲两条g++命令方便多了
另外,如果 a.out 不仅仅需要链接 a.o 还需要链接 b.o, c.o, d.o,这些目标文件分别由源文件 a.cpp, b.cpp, c.cpp, d.cpp 编译生成,当其中一些源文件发生变化时,如果用命令行方式,我们需要“人工”地判断哪些源文件发生了变化,“人工”地重编这些文件,再把它们链在一起。
make自动管理依赖的作用在这里就非常有用了,我只需执行"make makefile", make会自动检查更新,只编译发生变化的源文件。
makefile解释:
<target>: <prerequesit>
<tab><command>
<target> 我们要生成的目标,比如说,我们想要生成a.o,这个目标就是a.o,想要生成 a.out,这个目标就是 a.out
<prerequesit> 目标所依赖的前提,生成 a.out, 需要 a.o,那么 a.o 就是 a.out 的前提,
当执行make命令时,make 会自动检查到 a.o 的更新,如果 a.o 更新了, make就会重新生成 a.out,
如果 a.o 不存在时,make 会自动查找生成 a.o 的规则,生成 a.o,所以,make 的执行顺序类似于函数调用,makefile中定义的第一条规则是默认的入口
<command> 是生成目标要执行的命令,makefile规定<command>之前一定要用<tab>
二,makefile的执行顺序:
执行"make makefile"时,make默认要生成的唯一目标是makefile中的第一个目标。
然后用类似递归的方法生成一个目标
将当前目标的每一个前提条件当目标,查找规则并生成该前提条件
生成全部前提条件后,生成当前目标
a.out: a.o b.o c.o g++ a.o b.o c.o -o a.outa.o: a.cpp g++ -c a.cpp -o a.ob.o: b.cpp g++ -c b.cpp -o b.oc.o: c.cpp g++ -c c.cpp -o c.o
就可以了a.out: a.o b.o c.o g++ a.o b.o c.o -o a.out
Logically:a.cpp -(compile)-> a.oa.o -(link)-> a.outCommand:g++ -c a.cpp -o a.og++ a.o -o a.outMakefile:a.out:a.o g++ a.o -o a.outa.o:a.cpp g++ -c a.cpp -o a.o
<Target>:<Dependency><tab><command to execute>To build <Target>, say, "a.out", it requires <Dependency>, e.g. "a.o", that is when a.o is changed, a.out need to rebuild.to build <Target>, it also requires to execute the <command to execute>, e.g. g++ a.o -o a.outmake sure that it has a table before the command
The difference between makefile and the command is that the makefile defines a dependency. In above case, we don't see any benefit as a.out only depends on one source file to change.Think if a.out depends on tens of source file, even more than hundreds of source file, instead of compile all the source file one by one, we can only compile those which has been changed. and, instead of manually decide which one has been changed, we can relay on make command as well as makefile who specify the dependency.
However, to build a.out it needs a.o as specified in its dependency, so, make has to look for the rule of a.o in makefile, which is the 2nd rule in makefile and create a.o based the rule.
"make <target>"
b.cpp -(compile)-> b.o
c.cpp -(compile)-> c.o
a.out:a.o b.o c.o g++ a.o b.o c.o -o a.outa.o:a.cpp g++ -c a.cpp -o a.ob.o:b.cpp g++ -c b.cpp -o b.oc.o:c.cpp g++ -c c.cpp -o c.o
a.out:a.o b.o c.o g++ a.o b.o c.o -o a.out
OBJ := a.o b.o c.o d.o e.oa.out:$(OBJ) g++ $(OBJ) -o a.out
obj/a.out:obj/a.o g++ obj/a.o -o obj/a.outobj/a.o:src/a.cpp g++ -c src/a.cpp -o obj/a.oobj/b.o:src/b.cpp g++ -c src/b.cpp -o obj/b.oobj/c.o:src/c.cpp g++ -c src/c.cpp -o obj/c.o
obj/%.o:src/%.cpp g++ -c $< -o $@
first %, for wild-card matching2nd %, I guess refer to the first one
$< refer to the dependence$@ refer to the target
obj/a.out:obj/a.o obj/b.o obj/c.o g++ obj/a.o obj/b.o obj/c.o -o obj/a.outobj/%.o:src/%.cpp g++ -c $< -o $@
OBJ := obj/a.o obj/b.o obj/c.oobj/a.out:$(OBJ) g++ $(OBJ) -o obj/a.outobj/%.o:src/%.cpp g++ -c $< -o $@
obj/%.o:src/%.cppg++ -c $< -o $@
src/a.cppb.cpptest/c.cpp
a.o b.o: obj/%.o:src/%.cpp g++ -c $< -o $@c.o:obj/%.o:test/%.cpp g++ -c $< -o $@
src/a.cppb.cppc.cppe.cpp...
OBJ := obj/a.o\obj/b.o\obj/c.o\obj/d.o\obj/e.o\...
OBJ := src/*.cpp
SRC:= $(wildcard src/*.cpp)#SRC = a.cpp b.cpp c.cpp ...OBJ := $(SRC: src/%.cpp=obj/%.o)#OBJ := obj/a.o obj/b.o obj/c.o
src/a.cppb.cpptest/c.cpptarget directory obj/
SOURCE := $(wildcard src/*.cpp)SRCOBJ := $(SOURCE:src/%.cpp=obj/%.o)obj/a.out:$(SRCOBJ) obj/c.o g++ $(SRCOBJ) obj/c.o -o obj/a.out$(SRCOBJ):obj/%.o:src/%.cpp g++ -c $< -o $@obj/c.o:obj/%.o:test/%.cpp g++ -c $< -o $@
/usr/CppUTest/includeAll its libs are in:
/usr/CppUTest/lib
so we need to put in makefile:
CPPFLAGS += -I/usr/CppUTest/include
CPPFLAGS is used to specify compile options.-Idir is directory option
add 3rd party lib in the link command
a.out:a.o b.o c.og++ a.o b.o c.o -L/usr/CPPUTest/lib -lCppUTest -lCppUTestExt -o a.out-Ldir: directory option-llib: lib option, no space between "-l" and "lib", and "lib" is the name of library without prefix and suffix, e.g. to link libMyLib.o, the option should written as -lMylib
- Makefile笔记
- makefile 笔记
- makefile 笔记
- Makefile笔记
- 笔记:makefile
- Makefile 笔记
- makefile笔记
- makefile笔记
- makefile笔记
- makefile笔记
- Makefile 笔记
- Makefile 笔记
- makefile 笔记
- Makefile笔记
- Makefile笔记
- makefile笔记
- Makefile笔记
- Makefile笔记
- 待研究 微信的商业模式与创业机会
- Post方式访问Http或者是Webserver
- dvorak两个月&检讨购买新键盘
- [HTML5]使用Box2dWeb模拟射箭效果并添加能量槽
- 358. Practice makes perfect. 熟能生巧
- Makefile 笔记
- SQL Server 数据库巡检脚本
- 三角形类(二)
- 二叉排序树
- POJ 1423
- 题目24: 素数距离问题
- 让你乐的死去活来的论坛签名
- 给你生活的忠告
- 武侠电视剧规律