makefile是为编译程序代码而生,有以下要点:
11、makefile文件写好后,只要一个make命令,操作系统就会读取当前目录下的makefile文件,并按照makefile文件的内容规则有序编译整个工程代码。
12、makefile的书写规则如下:
target... : prerequisites ...
command
...
...
-------------------------------------------------------------------------------
target 编译目标文件,可以是Object File,也可以是执行文件。还可以是一个标签(Label),对于标签这种特性。
prerequisites,生成target所依赖的文件或目标。
command make需要执行的命令。(任意的Shell命令)
这是一个源文件和目标文件的依赖关系,target是一个或多个的目标文件依赖于prerequisites中的文件,其生成规则定义在command中。prerequisites中如果有一个以上的文件比target文件要新的话,command所定义的命令就会被执行。这就是Makefile的规则,也是Makefile中最核心的内容。
13、代码实例
为了说清楚makefile文件的写法,特别写了以下c++程序代码,包括5个.cpp文件(main.cpp,make1.cpp,make2.cpp,make3.cpp,make4.cpp)和4个.h(make1.h,make2.h,make3.h,make4.h)文件,还有一个makefile文件,使用make命令编译出可执行目标文件makefile_test。代码文件内容贴到下而
以下是makefile文件内容
# my first makefile for a c++ project makefile_test
# 2018-5-21 by kevin
#makefile_test : main.o make1.o make2.o make3.o make4.o #这是最直接的写法,下面改成了变量的方式,方便维护
# g++ -o makefile_test main.o make1.o make2.o make3.o make4.o
objects = main.o make1.o make2.o make3.o make4.o #把.o串给变量objects
makefile_test : $(objects) #链接成最终目标所依赖的中间目标文件 .o文件
g++ -o makefile_test $(objects) #编译链接命令
main.o : main.cpp make1.h
g++ -c -g main.cpp #-c only compile to middle tag file *.o -g debug compile type
make1.o : make1.cpp make1.h make2.h #编译目标make1.o所依赖的源文件
g++ -c -g make1.cpp #编译命令
make2.o : make2.cpp make2.h make3.h
g++ -c -g make2.cpp
make3.o : make3.cpp make3.h make4.h
g++ -c -g make3.cpp
make4.o : make4.cpp make4.h
g++ -c -g make4.cpp
.PHONY : clean # .PHONY 指明clean是个伪目标。 伪目标:在这里指不存在的文件,并不能与其他文件建立依赖,所以叫做伪目标。
clean:
#rm makefile_test main.o make1.o make2.o make3.o make4.o
rm makefile_test $(objects)
main.cpp
#include <iostream>
#include "make1.h"
int main(int argc,char **argv)
{
std::cout<<"calling fuction main"<<std::endl;
int aa = make1(80);
std::cout<<"number = "<<aa<<std::endl;
return aa;
}
make1.h
int make1(int a1);
make1.cpp
#include <iostream>
#include "make1.h"
#include "make2.h"
int make1(int a1)
{
std::cout<<"calling fuction make1"<<std::endl;
return make2(a1)+1;
}
make2.h
int make2(int a2);
make2.cpp
#include <iostream>
#include "make2.h"
#include "make3.h"
int make2(int a2)
{
std::cout<<"calling fuction make2"<<std::endl;
return make3(a2)+2;
}
make3.h
int make3(int a3);
make3.cpp
#include <iostream>
#include "make3.h"
#include "make4.h"
int make3(int a3)
{
std::cout<< "calling function make3"<<std::endl;
return make4(a3) + 3;
}
make4.h
int make4(int a4);
make4.cpp
#include <iostream>
#include "make4.h"
int make4(int a4)
{
std::cout<<"calling function make4"<<std::endl;
return a4+4;
}