GCC 编译 C 语言程序

GCC 编译 C 语言程序

原文阅读于 http://c.biancheng.net/

GCC 是一个编译器,没有界面,必须在命令行模式下使用。通过 gcc 命令可以将源文件编译成可执行文件。GCC 可以一次性完成 C 语言源文件的编译,也可以分步完成。

本文演示一次性完成源文件的编译。

hello_world.c

/*
 ============================================================================
 Name        : hello_world.c
 Author      : Foreverstrong Cheng
 Version     :
 Copyright   : Copyright 2019 DeepNorth License
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
	return EXIT_SUCCESS;
}

(1) 生成可执行程序

strong@foreverstrong:~/ForeverStrong$ cd hello_world/
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 12
drwxrwxr-x 2 strong strong 4096 Feb 21 08:31 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c 
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 24
drwxrwxr-x 2 strong strong 4096 Feb 21 08:33 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rwxrwxr-x 1 strong strong 8608 Feb 21 08:33 a.out*
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$

gcc 命令后面紧跟源文件名,# 是 shell 中的注释格式。
打开 hello_world 目录,一个名为 a.out 的文件,是最终生成的可执行文件,如下图所示:

在这里插入图片描述

这样就一次性完成编译和链接的全部过程。

Linux 不以文件后缀来区分可执行文件,Linux 下的可执行文件后缀理论上可以是任意的,.out 只是用来表明它是 GCC 的输出文件。不管源文件的名字是什么,GCC 生成的可执行文件的默认名字始终是 a.out

如果不想使用默认的文件名,那么可以通过 -o 选项来自定义文件名,例如:

strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c -o hello_world.out
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 24
drwxrwxr-x 2 strong strong 4096 Feb 21 08:39 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
-rwxrwxr-x 1 strong strong 8608 Feb 21 08:39 hello_world.out*
strong@foreverstrong:~/ForeverStrong/hello_world$ 

这样生成的可执行程序的名字就是 hello_world.out

Linux 下可执行文件的后缀仅仅是一种形式,所以可执行文件也可以不带后缀,例如:

strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c -o hello_world
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 24
drwxrwxr-x 2 strong strong 4096 Feb 21 08:38 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rwxrwxr-x 1 strong strong 8608 Feb 21 08:38 hello_world*
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 

生成的可执行程序的名字就是 hello_world

通过 -o 选项可以将可执行文件输出到其他目录,并不一定非要在当前目录下,例如:

strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 12
drwxrwxr-x 2 strong strong 4096 Feb 21 08:58 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ mkdir lib
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c -o ./lib/hello_world.out
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll ./lib/
total 20
drwxrwxr-x 2 strong strong 4096 Feb 21 08:59 ./
drwxrwxr-x 3 strong strong 4096 Feb 21 08:58 ../
-rwxrwxr-x 1 strong strong 8608 Feb 21 08:59 hello_world.out*
strong@foreverstrong:~/ForeverStrong/hello_world$

或者

strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c -o lib/hello_world.out
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll ./lib/
total 20
drwxrwxr-x 2 strong strong 4096 Feb 21 09:00 ./
drwxrwxr-x 3 strong strong 4096 Feb 21 08:58 ../
-rwxrwxr-x 1 strong strong 8608 Feb 21 09:00 hello_world.out*
strong@foreverstrong:~/ForeverStrong/hello_world$

将可执行文件输出到当前目录下的 lib 目录,命名为 hello_world.out./ 表示当前目录,如果不写,默认也是当前目录。

lib 目录必须存在。如果不存在,gcc 命令不会自动创建,而是抛出一个错误。

(2) 运行可执行程序

运行可执行程序:

strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 24
drwxrwxr-x 2 strong strong 4096 Feb 21 09:11 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rwxrwxr-x 1 strong strong 8608 Feb 21 09:11 a.out*
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ a.out
a.out: command not found
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ./a.out 
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$

./ 表示当前目录,命令的意思是运行当前目录下的 a.out 程序。如果不写 ./,Linux 会到系统路径下查找 a.out,而系统路径下显然不存在这个程序,所以会运行失败。

系统路径,就是环境变量指定的路径。可以通过修改环境变量添加自己的路径,或者删除某个路径。一条 Linux 命令对应一个可执行程序,如果执行命令时没有指明路径,那么就会到系统路径下查找对应的程序。

输入完上面的命令,按下回车键,程序就开始执行了,它会将输出结果直接显示在控制台上,如下所示:

strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 24
drwxrwxr-x 2 strong strong 4096 Feb 21 09:11 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rwxrwxr-x 1 strong strong 8608 Feb 21 09:11 a.out*
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ a.out
a.out: command not found
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ./a.out 
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$

如果程序在其它目录下,运行程序时还要带上目录的名字,例如:

strong@foreverstrong:~/ForeverStrong/hello_world$ ./lib/hello_world.out 
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$ 

或者

strong@foreverstrong:~/ForeverStrong/hello_world$ lib/hello_world.out 
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$

这个时候加不加 ./ 都一样,Linux 能够识别出 lib 是一个目录,而不是一个命令,它默认会在当前路径下查找该目录,而不是去系统路径下查找,所以不加 ./ 也不会出错。

如果程序没有执行权限,可以使用 sudo 命令来增加权限,例如:

strong@foreverstrong:~/ForeverStrong/hello_world$ ll ./lib/hello_world.out 
-rwxrwxr-x 1 strong strong 8608 Feb 21 09:16 ./lib/hello_world.out*
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ sudo chmod 777 ./lib/hello_world.out 
[sudo] password for strong: 
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll ./lib/hello_world.out 
-rwxrwxrwx 1 strong strong 8608 Feb 21 09:16 ./lib/hello_world.out*
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 12
drwxrwxr-x 2 strong strong 4096 Feb 21 09:15 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ mkdir lib
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 3 strong strong 4096 Feb 21 09:15 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
drwxrwxr-x 2 strong strong 4096 Feb 21 09:15 lib/
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c -o ./lib/hello_world.out
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll ./lib/
total 20
drwxrwxr-x 2 strong strong 4096 Feb 21 09:16 ./
drwxrwxr-x 3 strong strong 4096 Feb 21 09:15 ../
-rwxrwxr-x 1 strong strong 8608 Feb 21 09:16 hello_world.out*
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ./lib/hello_world.out 
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ lib/hello_world.out 
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$

References

http://c.biancheng.net/

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/87854394
今日推荐