makefile自动化编译
测试代码:
(1)String.h文件:
#ifndef _STRING_H_
#define _STRING_H_
#include <iostream>
using namespace std;
#include <string.h>
class String{
public:
String(const char* cstr = NULL);
String(const String& str);
String& operator=(const String& str);
~String();
char* c_str() const {
return m_data;
}
private:
char* m_data;
};
ostream& operator<<(ostream& os, const String& str);
#endif // _STRING_H_
(2)String.cpp文件
#include "String.h"
String::String(const char* cstr /*= NULL*/) {
if (cstr) {
m_data = new char[strlen(cstr) + 1];
strcpy(m_data, cstr);
}
else {
m_data = new char[1];
*m_data = '\0';
}
}
String::String(const String& str) {
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
}
String& String::operator=(const String& str) {if (this == &str)
return *this;
delete [] m_data;
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
return *this;
}
String::~String() {
delete[] m_data;
}
ostream& operator<<(ostream& os, const String& str) {
os << str.c_str();
return os;
}
(3)StringTest.cpp文件
#include "String.h"
using namespace std;
int main() {
String s1;
String s2("hello");
String s3(s1); //拷贝构造函数
cout << s3 << endl;
s3 = s2; //拷贝赋值函数
cout << s3 << endl;
return 0;
}
makefile文件
//目标:依赖
StringTest:String.o StringTest.o String.h
//命令
g++ -o StringTest StringTest.o String.o
String.o:String.cpp
g++ -c String.cpp
StringTest.o:StringTest.cpp
g++ -c StringTest.cpp
//假想目标
clean:
rm String.o StringTest.o StringTest
tips:
实际中的项目的cpp文件是很多的,所以我们要在makefile中分开使得cpp文件先生成.o文件,然后再链接,这样可以避免修改文件导致所有文件都要重新的编译,浪费时间。
makefile提供了很多的规则让我们可以对makefile文件进行简化,接下来我们认识以下这些规则并且尝试对我们的makefile文件进行简化.
(1) 使用自定义的变量
变量的定义和使用
//定义了一个变量OBJ
OBJ = StringTest
//$(OBJ)此时相当于宏替换,替换成StringTest
$(OBJ):String.o StringTest.o String.h
修改后的makefile文件
OBJ = StringTest.o String.o
StringTest:$(OBJ)
g++ -o StringTest $(OBJ)
String.o:String.cpp
g++ -c String.cpp
StringTest.o:StringTest.cpp
g++ -c StringTest.cpp
clean:
rm $(OBJ) StringTest
(2)makefile提供了自动推导的规则,可以通过头文件来推导其对应的.cpp文件并且生成.o文件
String.o:String.cpp
g++ -c String.cpp
StringTest.o:StringTest.cpp
g++ -c StringTest.cpp
简化后的makefile文件:
OBJ = StringTest.o String.o
StringTest:$(OBJ)
g++ -o StringTest $(OBJ)
//根据头文件自动推导
String.o:String.h
StringTest.o:String.h
clean:
rm $(OBJ) StringTest
执行make
shell终端的命令
[kiosk@foundation45 makefile]$ make
g++ -c -o StringTest.o StringTest.cpp
g++ -c -o String.o String.cpp
g++ -o StringTest StringTest.o String.o
(3) 假想目标
表达动作的目标称为假想目标。通常规则会生成或者更新与目标的同名文件,但是假想目标不生成文件,只是作为几个命令组成特殊规则的名称。例如例子中的clean
,只是执行清理动作。如果,makefile同级目录存在与假象目标同名的文件(例如:clean
),那么会导致命令不会被执行。所以需要把目标显示声明为假想目标。
.PHONY: 目标
常用的假想目标:
假想目标 | 应用场景 |
---|---|
clean | 执行清理动作,删除make所生成的目标和中间文件 |
all | make只是默认生成第一个目标,如果想要make生成多个目标,就可以定义假想对象all来包含所有的要生成的目标,执行make all就可以生成全部的目标 |
tar | 源程序打tar包备份 |
makefile示例
OBJ = StringTest.o String.o
//假想目标all和clean
.PHONY:all clean
all:StringTest
StringTest:$(OBJ)
g++ -o StringTest $(OBJ)
String.o:String.h
StringTest.o:String.h
clean:
rm $(OBJ) StringTest
(4)系统预定义变量
预定义的变量是系统自己已经定义好的变量,自己带有默认值,用户也可以更改这些预定义的变量的值。
变量 | 默认值 |
---|---|
CC | c语言编译工具 gcc |
CXX | c++编译工具 g++ |
CPP | $(CC) -E 带有标准输出的C语言预处理程序 |
CFLAGS | 用于C编译器的额外标志 |
CXXFLAGS | 用于C++编译器的额外标志 |
makefile示例
StringTest:$(OBJ)
//使用了系统定义的变量 cxx
$(CXX) -o StringTest $(OBJ)
(5)自动变量通配符号
自动变量 | 作用 |
---|---|
$^ | 通配所有的依赖 |
$< | 通配第一个的依赖 |
$@ | 指定目标 |
> 通配符
通配符主要用于匹配文件名,makefile中使用%
作为通配符。从匹配目标格式的目标名中依据通配符抽取部分字符串,再按照抽取字符串分配到每一个依赖格式中产生依赖名。例如,使用%.o:%.cpp
。
makefile示例
OBJ = StringTest.o String.o
.PHONY:all clean
all:StringTest
StringTest:$(OBJ)
$(CXX) -o StringTest $^
$(OBJ):%.o:%.cpp
$(CXX) -c $(CXXFLAGS) $< -o $@
clean:
rm $(OBJ) StringTest
tips:makefile里面的注释用#来注释!