您的位置:首页 > 运维架构 > Linux

linux下Makefile用法(基础)

2016-07-25 16:57 766 查看
目的:掌握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可执行文件。

注:如果一行写的内容过多,可以用“\”来分解多行,注意“\”后面不加空格,用以上的例子,顺便把-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,必须更加深入实践才行。

网上写的较详细的Makefile:http://blog.csdn.net/liang13664759/article/details/1771246
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux Makefile make