1.单个程序
hello.c文件
#include <stdio.h>
int main(void){
printf(“Hello Word!\n”);
printf(“this is my first C in Linux\n”);
}
gcc编译hello.c
[root@testserver eg]# gcc hello.c <=编译hello.c
[root@testserver eg]# ./a.out <=此时产生这个文件,执行后,结果出来了。
Hello World
This my first C in Linux!
[root@testserver eg]#
gcc编译hello.c,并生成目标文件
[root@testserver eg]# gcc -c hello.c <=带c参数时生成目标文件
[root@testserver eg]# ll hello*
-rw-r--r-- 1 root root 161 2011-11-02 hello.c
-rw-r--r-- 1 root root 1576 11-02 21:52 hello.o <=这是生成的目标文件
[root@testserver eg]# gcc -o myexe hello.o <=带o参数时生成myexe的可执行文件
[root@testserver eg]# ./myexe <=执行得出结果。
Hello World
This my first C in Linux!
[root@testserver eg]#
2.主程序包含其他子程序时。
hello.c主程序文件
#include <stdio.h>
int main(void){
printf(“Hello Word!\n”);
printf(“this is my first C in Linux\n”);
myinclude(); //调用子程序函数
}
hello.c子程序文件
#include <stdio.h>
int myinclude(void){
printf(“this a include test.\n”);
printf(“my first time!\n”);
}
用gcc编译
[root@testserver eg]# gcc -c hello.c include.c <=两个文件同时编译
[root@testserver eg]# ll hello* include*
-rw-r--r-- 1 root root 159 2011-11-02 hello.c
-rw-r--r-- 1 root root 1640 11-02 22:02 hello.o
-rw-r--r-- 1 root root 108 11-02 21:22 include.c
-rw-r--r-- 1 root root 1576 11-02 22:02 include.o
[root@testserver eg]# gcc -o subexe hello.o include.o
[root@testserver eg]# ll
总计 40
-rwxr-xr-x 1 root root 6743 11-02 21:49 a.out
-rw-r--r-- 1 root root 159 2011-11-02 hello.c
-rw-r--r-- 1 root root 1640 11-02 22:02 hello.o
-rw-r--r-- 1 root root 108 11-02 21:22 include.c
-rw-r--r-- 1 root root 1576 11-02 22:02 include.o
-rwxr-xr-x 1 root root 6743 11-02 21:53 myexe
-rwxr-xr-x 1 root root 6963 11-02 22:03 subexe
[root@testserver eg]# ./subexe
Hello World
This my first C in Linux!
this a include test.
my first time!
[root@testserver eg]#