参考文章:
http://www.linuxidc.com/Linux/2011-01/31227.htm
1.单个文件编译
编写源文件hello.c
编译:gcc hello.c
生成a.out
指定生成文件名:gcc -o test hello.c
生成test
2.编译成目标文件
编译:gcc -c hello.c
生成:hello.o
指定生成文件名:gcc -c -o test.o hello.c
编译:gcc -c file1.c file2.c file3.c
生成:file1.o file2.o file3.o
3.多文件编译
编写计算字符串长度函数string.c如下:
#define ENDSTRING '\0'
int StrLen(char *string)
{
int len=0;
while(*string++ != ENDSTRING)
len++;
return len;
}
编写主函数main.c 如下:
#include<stdio.h>
extern int StrLen(char* str);
int mian(void)
{
char src[]="Hello Dymatic";
printf("string length is: %d\n",StrLen(src));
return 0;
}
编译:gcc -o test string.c main.c
生成:test
执行结果:string length is: 13
也可先生成目标文件:gcc -c string.c main.c
编译:gcc -o test string.o main.o