gcc(g++)多文件编译的问题
2016-05-01 15:44
357 查看
1.简单程序(单模块程序)的编译
文件file1.c#include <stdio.h> int main() { printf("hello\n"); return 0; }
文件file1.cpp
#include <iostream> using namespace std; int main() { cout<<"hello"<<endl; return 0; }
编译运行
$ gcc file1.c -o file1 $ g++ file1.cpp -o file1_cpp $ ./file1 hello $ ./file1_cpp hello
对于只有一个文件的c/c++用GCC/G++来编译很容易
对于多个文件即多个模块的程序来说,其实也并不是很难.
2.多模块程序的编译
下面举个例子:文件first.h
int first();
文件first.c
#include "include.h" #include "first.h" first() { printf("this is just a test!"); return 0; }
文件second.h
int mymax(int,int);
文件second.c
mymax(x,y) { if(x>y) return x; else return y; }
文件main.c
#include "first.h" #include "second.h" #include int main() { int a,b; a=10; b=20; first(); printf("%d\n",mymax(a,b)); return 0; }
下面是在终端中输入的内容
$ gcc -c first.c $ gcc -c second.c $ gcc -c main.c $ gcc first.o second.o main.o -o main $ ./main this is just a test!20
当然啦也可以这么输入
$ gcc first.c second.c main.c -o main
不过以上的方法不是很好,因为对于文件数不是很多的程序,手动输入以上几个命令还不是很累,但如果是个文件数很多的程序呢,如果这样输入,那肯定会很累.
对于模块数很多程序,我们可以写一个makefile文件.然后使用make命令就可以了.
原文地址
http://blog.chinaunix.net/uid-20682749-id-2238158.html相关文章推荐
- thinkphp 设置跨域请求
- 驱动
- rapidjson初探——使用C++解析json
- 学习进度(2016.5.1)
- 异常 找不到xsd文件的声明
- workers.properties说明
- Android学习笔记1:创建Hello World项目
- CListCtrl::DeleteColumn
- hdu1061 Rightmost Digit 标准快速幂
- RFC1321 MD5算法信息介绍
- 基于全注解方式实现MyBatis链接数据库的demo
- 大道至简阅读笔记02
- Tricks(四十五)—— 可视化 min 和 max(表示一种截断)
- POJ 1275 Cashier Employment (差分约束 + spfa)
- Ubuntu16.04 Sublime Text3 安装激活和设置
- 2016.5.1
- LeetCode 4. Median of Two Sorted Arrays(两个有序数组的中位数)
- 第 29 章 CSS3 弹性伸缩布局[上]
- 我的大学规划
- 读《Spring-技术内幕》-第二章:IoC容器的实现-1