linux下的项目组成
1认识Makefile
程序的自动编译
当程序中有多个源文件时,
先编译:
g++ ‐c main.cpp ‐o main.o
g++ ‐c other.cpp ‐o other.o
再链接
g++ main.o other.o ‐o helloworld
考虑:能不能写一个脚本,自动执行所有的步骤
?
对比:在VC下面,我们点一个“生成解决方案”就能 生成整个项目了。。。。
Makefile
Makefile是一个描述“如何生成整个项目”的脚 本文件
方法:
① 创建一个文件叫Makefile
② 输入命令,根据Makefile里面的指示,自动执 行所有的步骤
make ‐f Makefile
(或者直接make 不带参数)
演示1: helloworld: g++ main.cpp other.cpp ‐o helloworld
Makefile的写法
Makefile就是一个普通的文本文件。。。
它由很多条“规则rule”组成,这些规则就是描 述了“先干什么、后干什么”。。。
每一条规则的格式为:
target: dependencies
【TAB】
system command1
【TAB】system command2
【TAB】system command…
target: dependencies
【TAB】 system command1
【TAB】 system command2
【TAB】 system command…
target: 目标 ,
dependencies: 依赖
【TAB】: 每行命令前必须插一个TAB
system command: 系统命令
读作:要完成目标target,必须执行命令commands…
演示1
helloworld:
【TAB】g++ main.cpp other.cpp ‐o helloworld
演示2
helloworld:
【TAB】g++ ‐c main.cpp ‐o main.o
【TAB】g++ ‐c other.cpp ‐o other.o
【TAB】g++ main.o other.o ‐o helloworld
Makefile的写法
当存在很多规则时,默认从第一条规则开始 执行。 也可以显式地执行某条规则。
演示2
helloworld:
【TAB】g++ ‐c main.cpp ‐o main.o
【TAB】g++ ‐c other.cpp ‐o other.o
【TAB】g++ main.o other.o ‐o helloworld
clean:
【TAB】rm ‐rf *.o helloworld