g++编译多个文件

来源:百度知道 编辑:UC知道 时间:2024/09/20 06:45:58
有三个文件:
file1:utility.h
--------------------------------------------
#include <string.h>
#define U32 unsigned
void __DropWord( char *cmd, char sc);
--------------------------------------------
file2:utility.c
--------------------------------------------
#include "utility.h"

static void __DropWord( char *cmd, char sc )
{
if( cmd )
{
U32 i, j, len = strlen( cmd );
//printf("%d,%s",len,cmd);
for( i = 0; i < len; i++ )
if( cmd[i] == sc )
break;
if( i < len )
i++;
len = len - i;
for( j = 0; j <= len; j++ )
cmd[j] = cmd[i++];
}
}
--------------------------------------------
file3:main.c
--------------------------------------------
#include <stdio.h>
#include <iostream>
#include "utility.h"

main()
{
char mm[]="hello world.&qu

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

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

int main()
{
cout<<"hello"<<endl;
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 <stdio.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;
}<