Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146981
  • 博文数量: 33
  • 博客积分: 1455
  • 博客等级: 上尉
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-01 18:36
文章分类

全部博文(33)

文章存档

2011年(1)

2010年(1)

2009年(2)

2008年(29)

我的朋友

分类: LINUX

2008-10-17 00:11:06

终于弄懂了如何编译多个模块的以c/c++程序.

说实在的,本人目前接触linux时间才几个月,所有我所知道的可能是一些最基本的东西.呵呵,下面写的东西对于那些高手来说不定有帮助,不过对于刚接触gcc/g++的朋友可能有些帮助.

1.简单程序(单模块程序)的编译
文件file1.c
#include
int main()
{
        printf("hello\n");
        return 0;
}

文件file1.cpp
#include
using std::cout;
using std::endl;

int main()
{
        cout<<"hello"<        return 0;
}
[xiaochen@freeware ~]$ gcc file1.c -o file1
[xiaochen@freeware ~]$ g++ file1.cpp -o file1_cpp
[xiaochen@freeware ~]$ ./file1
hello
[xiaochen@freeware ~]$ ./file1_cpp
hello

对于只有一个文件的c/c++用GCC/G++来编译很容易

对于多个文件即多个模块的程序来说,其实也并不是很难.
2.多模块程序的编译
下面举个例子:
文件first.h
int first();
文件first.c
#include
#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;
}

下面是在终端中输入的内容
[xiaochen@freeware ~]$ gcc -c first.c
[xiaochen@freeware ~]$ gcc -c second.c
[xiaochen@freeware ~]$ gcc -c main.c
[xiaochen@freeware ~]$ gcc first.o second.o main.o -o main
[xiaochen@freeware ~]$ ./main
this is just a test!20
当然啦也可以这么输入
[xiaochen@freeware ~]$ gcc first.c second.c main.c -o main

不过以上的方法不是很好,因为对于文件数不是很多的程序,手动输入以上几个命令还不是很累,但如果是个文件数很多的程序呢,如果这样输入,那肯定会很累.

对于模块数很多程序,我们可以写一个makefile文件.然后使用make命令就可以了.






阅读(16816) | 评论(0) | 转发(0) |
0

上一篇:shell的简介

下一篇:轻松实现共享上网

给主人留下些什么吧!~~