Linux 下g++ makefile

上文(g++基本用法)介绍简单的g++编译器的用法,只是针对没有依赖关系的单个文件的操作,当我们有多个文件需要编译的时候,是如何工作的呢?下面以简单的实例进行介绍,然后把实例以MakeFile文件实现,并对MakeFile文件进行简单介绍。

准备工作,下面是需要的简单实例文件及代码:

main.cxx

Administrator@72cec870236147e /home/liujl/mytest
$ cat main.cxx
#include
#include “printf1.hxx”
#include “printf2.hxx”
Administrator@72cec870236147e /home/liujl/mytest
$ g++ -c main.cxx

Administrator@72cec870236147e /home/liujl/mytest
$ g++ -c printf1.cxx

Administrator@72cec870236147e /home/liujl/mytest
$ g++ -c printf2.cxx

int main(){ printf1(); printf2();}
printf1.hxx
Administrator@72cec870236147e /home/liujl/mytest
$ cat printf1.hxx
#ifndef PRINTF_1_H
#define PRINTF_1_H

void printf1();

#endif
printf1.cxx
Administrator@72cec870236147e /home/liujl/mytest
$ cat printf1.cxx
#include “printf1.hxx”
#include

using namespace std;

void printf1()
{
cout<<“printf1”<<endl;
}
printf2.hxx
Administrator@72cec870236147e /home/liujl/mytest
$ cat printf2.hxx
#ifndef PRINTF_2_H
#define PRINTF_2_H

void printf2();

#endif
printf2.cxx
Administrator@72cec870236147e /home/liujl/mytest
$ cat printf2.cxx
#include “printf2.hxx”
#include

using namespace std;

void printf2()
{
cout<<“printf2”<<endl;
}
共计5个文件,3个cxx文件,2个hxx头文件
1、手动多文件编译

①先分别直接汇编(编译)为.o文件

Administrator@72cec870236147e /home/liujl/mytest
$ g++ -c main.cxx

Administrator@72cec870236147e /home/liujl/mytest
$ g++ -c printf1.cxx

Administrator@72cec870236147e /home/liujl/mytest
$ g++ -c printf2.cxx
②链接阶段

如果直接执行

Administrator@72cec870236147e /home/liujl/mytest
$ g++ main.cxx -o main
/tmp/cc9LFDvP.o:main.cxx:(.text+0xc): undefined reference to printf1()' /tmp/cc9LFDvP.o:main.cxx:(.text+0x11): undefined reference toprintf2()’
collect2: ld 返回 1
出现上边错误,原因是编译器找不到printf1()和printf2()的定义。
所以需要将3个obj文件链接到一个文件上:

Administrator@72cec870236147e /home/liujl/mytest
$ g++ main.cxx printf1.cxx printf2.cxx -o main

Administrator@72cec870236147e /home/liujl/mytest
$ ./main
printf1
printf2
并输出结果。
这样就能解决多文件编译问题,但是一般情况下,一个项目下的文件比较多,如果这样输入,比较费劲,所以就需要把编译过程写进一个MakeFile文件中

Administrator@72cec870236147e /home/liujl/mytest
$ cat makefile
cc=g++
exe=main
obj=main.o printf1.o printf2.o

( e x e ) : (exe): (obj)
$(cc) -o $(exe) $(obj)
main.o:main.cxx
$(cc) -c main.cxx
printf1.o:printf1.cxx
$(cc) -c printf1.cxx
printf2.o:printf2.cxx
( c c ) c p r i n t f 2. c x x c l e a n : r m r f . o m a i n c c = g + + e x e = m a i n o b j = m a i n . o p r i n t f 1. o p r i n t f 2. o (cc) -c printf2.cxx clean: rm -rf *.o main 其中 cc=g++ exe=main obj=main.o printf1.o printf2.o 为变量的定义, (…)作为引用,可以分析一下,是不是和上文中单个操作效果一样?
执行过程:

Administrator@72cec870236147e /home/liujl/mytest
$ make
g++ -c main.cxx
g++ -c printf1.cxx
g++ -c printf2.cxx
g++ -o main main.o printf1.o printf2.o

作者:richerg85
来源:CSDN
原文:https://blog.csdn.net/richerg85/article/details/17074193
版权声明:本文为博主原创文章,转载请附上博文链接!

发布了2 篇原创文章 · 获赞 1 · 访问量 212

猜你喜欢

转载自blog.csdn.net/arthas_zy/article/details/85016889
今日推荐