g++多文件编译和简单MakeFile文件写法
2015-07-31 10:55
519 查看
/article/8017832.html
分类: linux相关知识2013-12-02
16:05 2533人阅读 评论(0) 收藏 举报
上文(g++基本用法)介绍简单的g++编译器的用法,只是针对没有依赖关系的单个文件的操作,当我们有多个文件需要编译的时候,是如何工作的呢?下面以简单的实例进行介绍,然后把实例以MakeFile文件实现,并对MakeFile文件进行简单介绍。
准备工作,下面是需要的简单实例文件及代码:
main.cxx
1、手动多文件编译
①先分别直接汇编(编译)为.o文件
②链接阶段
如果直接执行
所以需要将3个obj文件链接到一个文件上:
这样就能解决多文件编译问题,但是一般情况下,一个项目下的文件比较多,如果这样输入,比较费劲,所以就需要把编译过程写进一个MakeFile文件中
执行过程:
makefile可以参考:
1、ASimple
Makefile Tutorial
2、中文makefile教程
版权声明:本文为博主原创文章,未经博主允许不得转载。
g++多文件编译和简单MakeFile文件写法
分类: linux相关知识2013-12-0216:05 2533人阅读 评论(0) 收藏 举报
上文(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>的定义。
所以需要将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教程
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- UVA - 709 Formatting Text
- 【机房收费系统】第一次机房收费系统之er图、实体联系图小试
- 快速幂取模 例题uva10006
- CSV 数字转化文本
- 打印对象和toString方法
- 建立SSH信任
- 敏捷的站立会怎么开
- 值传递 和 引用传递的区别
- 黑马程序员——JAVA基础------反射
- 数据结构二:企业级线性链表存储底层封装
- Ubuntu下调整swap分区的大小
- linux reboot flow
- 【第三步】熟练掌握设计UI界面的方法
- Network of Schools (POJ 1236)
- CentOS中gerrit + jenkins整合
- Android中使用Post请求
- GridView九宫格菜单实现方式
- declaration may not appear after executable statement in block
- 如何高效地分析框架源码
- 删除OSX中第三方的「偏好设置」程序(.prefPane)