Linux下用gcc编译c程序

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]#
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值