准备工作,下面是需要的简单实例文件及代码:
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>的定义。
所以需要将3个obj文件链接到一个文件上:
<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可以参考:
1、ASimple Makefile Tutorial
2、中文makefile教程