前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >makefile 如何 使用

makefile 如何 使用

作者头像
用户8418197
修改2021-08-30 11:00:30
2.4K0
修改2021-08-30 11:00:30
举报
文章被收录于专栏:howtouselinuxhowtouselinux

目的:掌握make的用法,Makefile的内容,能在linux下C++编程。

环境:ubuntu 15.10

准备:三个文件 test1.cpp, test2.cpp, test2.h

test1.cpp

#include "test2.h"

int main() {

printf("Print test1!\n");

test2Print();

return 0;

}

test2.cpp

#include "test2.h"

void test2Print() {

printf("Print test2!\n");

}

test2.h

#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

void test2Print();

(1)基础:

Makefile文件

firstTest:test1.o test2.o

g++ test1.o test2.o -o firstTest

test1.o:test1.cpp test2.h

g++ -c test1.cpp -o test1.o

test2.o:test2.cpp test2.h

g++ -c test2.cpp -o test2.o

clean:

rm -rf *.o firstTest

一个Makefile主要含有一系列的规则,如下:

X:Y

(tab)<command>

每个命令行前必须要有tab符号(如Makefile中书写方法)

上面的Makefile文件就是要编译出一个firstTest的可执行文件,逐句分析:

firstTest:test1.o test2.o :firstTest依赖于test1.o、test2.o两个目标文件

g++ test1.o test2.o -o firstTest:编译出可执行文件firstTest。-o表示你指定的可执行文件名称

test1.o:test1.cpp test2.h :test1.o依赖于test1.cpp、test2.h两个文件

g++ -c test1.cpp -o test1.o:编译出test1.o文件,-c表示只把给它的文件编译成目标文件,用源码文件的文件名命名,但把其后缀由“.cpp”变成“.o”。

此句中可以省略-o test1.o,编译器默认生成test1.o文件。

clean:

rm -rf *.o firstTest:当键入make clean的时候,会删除所有.o文件和firstTest文件

如果要编译c文件,把g++改成gcc即可。

Makefile写好后,在命令行直接键入make命令,就会执行Makefile中的内容了。此例子的话,最终会产生firstTest可执行文件。

Understanding Makefile With Examples

注:如果一行写的内容过多,可以用“\”来分解多行,注意“\”后面不加空格,用以上的例子,顺便把-o test1.o去掉了:

firstTest:test1.o \

test2.o

g++ test1.o test2.o \

-o firstTest

test1.o:test1.cpp test2.h

g++ -c test1.cpp

test2.o:test2.cpp test2.h

g++ -c test2.cpp

clean:

rm -rf *.o firstTest

(2)Makefile中使用变量

OBJS = test1.o test2.o

G = g++

CFLAGS = -Wall -O -g

firstTest:$(OBJS)

$(G) $(OBJS) -o firstTest

test1.o:test1.cpp test2.h

$(G) $(CFLAGS) -c test1.cpp

test2.o:test2.cpp test2.h

$(G) $(CFLAGS) -c test2.cpp

clean:

rm -rf *.o firstTest

在这里我们使用了变量。变量设置的格式是“varName = content”;以后要引用这个变量,只需要用$即可,$(varName)。

CFLAGS = -Wall -O -g 配置编译器设置,并把它赋值给CFLAGS变量

-Wall:输出所有警告信息

-O:在编译时进行优化

-g:表示编译debug版本

这样写的Makefile比较简单,但是缺点显著。那就是要列出所有的.cpp文件。就好比你添加一个.cpp文件,就需要修改Makefile文件,这样子还是很麻烦的。

(3)使用函数

C = gcc

G = g++

CFLAGS = -Wall -O -g

TARGET = ./firstTest

%.o:%.c

$(C) $(CFLAGS) -c $< -o $@

%.o:%.cpp

$(G) $(CFLAGS) -c $< -o $@

SOURCES = $(wildcard .c .cpp)

OBJS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES)))

$(TARGET):$(OBJS)

$(G) $(OBJS) -o $(TARGET)

chmod a+x $(TARGET)

clean:

rm -rf *.o firstTest

<1>函数wildcard:

产生一个所有以'.c'结尾的文件列表。

SOURCES = $(wildcard .c .cpp) 表示产生一个所有以.c、.cpp结尾的文件列表,然后存入变量SOURCES里。

<2>函数patsubst:

匹配替换,三个参数。第一个是需要匹配的样式,第二个目标样式,第三个是需要处理的由空格分离的列表。

OBJS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES)))

把SOURCES文件列表中所有.cpp变成.o、.c变成.o,然后形成一个新的列表,存入OBJS变量。

<3>

%.o:%.c

$(C) $(CFLAGS) -c $< -o $@

%.o:%.cpp

$(G) $(CFLAGS) -c $< -o $@

表示把所有的.c、.cpp文件编译成.o文件。

注:这里有三个比较有用的内部变量。$@扩展成当前规则的目的文件名,$<扩展成依靠列表中的第一个依靠文件,$^扩展成整个依靠的列表(除掉里面所有重复的文件名)。

<4>

chmod a+x $(TARGET) 表示把firstTest强制变成可执行文件。

以上内容是整理出的基础用法。要深入了解Makefile,必须更加深入实践才行。

Understanding Makefile With Examples

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档