makefile基础

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所生成的目标和中间文件
allmake只是默认生成第一个目标,如果想要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)系统预定义变量

预定义的变量是系统自己已经定义好的变量,自己带有默认值,用户也可以更改这些预定义的变量的值。

变量默认值
CCc语言编译工具 gcc
CXXc++编译工具 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里面的注释用#来注释!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值