g++多文件编译,并实现makefile

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



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

  main.cxx  

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ cat main.cxx</span>
<span style="font-family:Courier New,monospace;">#include <iostream></span>
<span style="font-family:Courier New,monospace;">#include "printf1.hxx"</span>
<span style="font-family:Courier New,monospace;">#include "printf2.hxx"</span>
<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ g++ -c main.cxx</span>

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ g++ -c  printf1.cxx</span>

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ g++ -c printf2.cxx</span>
<span style="font-family:Courier New,monospace;">
int main(){ printf1(); printf2();}</span>
<span style="font-family:Courier New,monospace;">printf1.hxx</span>
<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ cat printf1.hxx</span>
<span style="font-family:Courier New,monospace;">#ifndef _PRINTF_1_H_</span>
<span style="font-family:Courier New,monospace;">#define _PRINTF_1_H_</span>

<span style="font-family:Courier New,monospace;">void printf1();</span>

<span style="font-family:Courier New,monospace;">#endif</span>
<span style="font-family:Courier New,monospace;">printf1.cxx</span>
<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ cat printf1.cxx</span>
<span style="font-family:Courier New,monospace;">#include "printf1.hxx"</span>
<span style="font-family:Courier New,monospace;">#include <iostream></span>

<span style="font-family:Courier New,monospace;">using namespace std;</span>

<span style="font-family:Courier New,monospace;">void printf1()</span>
<span style="font-family:Courier New,monospace;">{</span>
<span style="font-family:Courier New,monospace;">        cout<<"printf1"<<endl;</span>
<span style="font-family:Courier New,monospace;">}</span>
<span style="font-family:Courier New,monospace;">printf2.hxx</span>
<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ cat printf2.hxx</span>
<span style="font-family:Courier New,monospace;">#ifndef _PRINTF_2_H_</span>
<span style="font-family:Courier New,monospace;">#define _PRINTF_2_H_</span>

<span style="font-family:Courier New,monospace;">void printf2();</span>

<span style="font-family:Courier New,monospace;">#endif</span>
<span style="font-family:Courier New,monospace;">printf2.cxx</span>
<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ cat printf2.cxx</span>
<span style="font-family:Courier New,monospace;">#include "printf2.hxx"</span>
<span style="font-family:Courier New,monospace;">#include <iostream></span>

<span style="font-family:Courier New,monospace;">using namespace std;</span>

<span style="font-family:Courier New,monospace;">void printf2()</span>
<span style="font-family:Courier New,monospace;">{</span>
<span style="font-family:Courier New,monospace;">        cout<<"printf2"<<endl;</span>
<span style="font-family:Courier New,monospace;">}</span>
共计<span style="font-family:Courier New,monospace;">5</span>个文件,<span style="font-family:Courier New,monospace;">3</span>个<span style="font-family:Courier New,monospace;">cxx</span>文件,<span style="font-family:Courier New,monospace;">2</span>个<span style="font-family:Courier New,monospace;">hxx</span>头文件

1、手动多文件编译

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

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ g++ -c main.cxx</span>

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ g++ -c printf1.cxx</span>

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ g++ -c printf2.cxx</span>

②链接阶段

如果直接执行

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ g++ main.cxx -o main</span>
<span style="font-family:Courier New,monospace;">/tmp/cc9LFDvP.o:main.cxx:(.text+0xc): undefined reference to `printf1()'</span>
<span style="font-family:Courier New,monospace;">/tmp/cc9LFDvP.o:main.cxx:(.text+0x11): undefined reference to `printf2()'</span>
<span style="font-family:Courier New,monospace;">collect2: ld </span>返回<span style="font-family:Courier New,monospace;"> 1</span>
出现上边错误,原因是编译器找不到<span style="font-family:Courier New,monospace;">printf1()</span>和<span style="font-family:Courier New,monospace;">printf2()</span>的定义。

所以需要将3obj文件链接到一个文件上:

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ g++ main.cxx printf1.cxx printf2.cxx -o main</span>

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ ./main</span>
<span style="font-family:Courier New,monospace;">printf1</span>
<span style="font-family:Courier New,monospace;">printf2</span>
并输出结果。

这样就能解决多文件编译问题,但是一般情况下,一个项目下的文件比较多,如果这样输入,比较费劲,所以就需要把编译过程写进一个MakeFile文件中

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ cat makefile</span>
<span style="font-family:Courier New,monospace;">cc=g++</span>
<span style="font-family:Courier New,monospace;">exe=main</span>
<span style="font-family:Courier New,monospace;">obj=main.o printf1.o printf2.o</span>

<span style="font-family:Courier New,monospace;">$(exe):$(obj)</span>
<span style="font-family:Courier New,monospace;">        $(cc) -o $(exe) $(obj)</span>
<span style="font-family:Courier New,monospace;">main.o:main.cxx</span>
<span style="font-family:Courier New,monospace;">        $(cc) -c main.cxx</span>
<span style="font-family:Courier New,monospace;">printf1.o:printf1.cxx</span>
<span style="font-family:Courier New,monospace;">        $(cc) -c printf1.cxx</span>
<span style="font-family:Courier New,monospace;">printf2.o:printf2.cxx</span>
<span style="font-family:Courier New,monospace;">        $(cc) -c printf2.cxx</span>
<span style="font-family:Courier New,monospace;">clean:</span>
<span style="font-family:Courier New,monospace;">        rm -rf *.o main</span>
其中
<span style="font-family:Courier New,monospace;">cc=g++</span>
<span style="font-family:Courier New,monospace;">exe=main</span>
<span style="font-family:Courier New,monospace;">obj=main.o printf1.o printf2.o</span>
为变量的定义,<span style="font-family:Courier New,monospace;">$(...)</span>作为引用,可以分析一下,是不是和上文中单个操作效果一样?

执行过程:

<span style="font-family:Courier New,monospace;">Administrator@72cec870236147e /home/liujl/mytest</span>
<span style="font-family:Courier New,monospace;">$ make</span>
<span style="font-family:Courier New,monospace;">g++ -c main.cxx</span>
<span style="font-family:Courier New,monospace;">g++ -c printf1.cxx</span>
<span style="font-family:Courier New,monospace;">g++ -c printf2.cxx</span>
<span style="font-family:Courier New,monospace;">g++ -o main main.o printf1.o printf2.o</span>





makefile可以参考:

1ASimple Makefile Tutorial

2中文makefile教程

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

经纬方略

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值