GCC 分步骤编译 C 语言程序

GCC 分步骤编译 C 语言程序

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

GCC 可以一次性完成 C 语言源文件的编译,也可以分步完成。
本文演示分步骤编译源文件。

gcc 命令可以将编译和链接分开,每次只完成一项任务。

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) 编译 (Compile)

将源文件编译成目标文件需要使用 -c 选项,例如:

strong@foreverstrong:~/ForeverStrong$ cd hello_world/
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 12
drwxrwxr-x 2 strong strong 4096 Feb 21 09:26 ./
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 -c hello_world.c 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 2 strong strong 4096 Feb 21 09:38 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
-rw-rw-r-- 1 strong strong 1520 Feb 21 09:38 hello_world.o
strong@foreverstrong:~/ForeverStrong/hello_world$

就将 hello_world.c 编译为 hello_world.o。打开 hello_world 目录,就会看到 hello_world.o

在这里插入图片描述

Visual C++ 或者 Visual Studio 中,目标文件的后缀为 .obj。GCC 编译器,目标文件的后缀为 .o

一个源文件会生成一个目标文件,多个源文件会生成多个目标文件,源文件数目和目标文件数目是一样的。通常情况下,默认的目标文件名字和源文件名字是一样的。

如果希望自定义目标文件的名字,那么可以使用 -o 选项,例如:

strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 12
drwxrwxr-x 2 strong strong 4096 Feb 21 09:46 ./
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 -c hello_world.c -o a.o
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 2 strong strong 4096 Feb 21 09:47 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong 1520 Feb 21 09:47 a.o
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$

生成的目标文件的名字就是 a.o

(2) 链接 (Link)

gcc 命令后面紧跟目标文件的名字,就可以将目标文件链接成为可执行文件,例如:

strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 12
drwxrwxr-x 2 strong strong 4096 Feb 21 09:48 ./
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 -c hello_world.c 
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 2 strong strong 4096 Feb 21 09:48 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
-rw-rw-r-- 1 strong strong 1520 Feb 21 09:48 hello_world.o
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.o 
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 28
drwxrwxr-x 2 strong strong 4096 Feb 21 09:49 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rwxrwxr-x 1 strong strong 8608 Feb 21 09:49 a.out*
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
-rw-rw-r-- 1 strong strong 1520 Feb 21 09:48 hello_world.o
strong@foreverstrong:~/ForeverStrong/hello_world$

就将 hello_world.o 链接为 a.out。打开 hello_world 目录,就会看到 a.out

gcc 命令后面紧跟源文件名字或者目标文件名字都是可以的,gcc 命令能够自动识别到底是源文件还是目标文件。如果是源文件,那么要经过编译和链接两个步骤才能生成可执行文件。如果是目标文件,只需要链接就可以了。

使用 -o 选项仍然能够自定义可执行文件的名字,例如:

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

生成的可执行文件的名字就是 hello_world.out

strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 2 strong strong 4096 Feb 21 09:52 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
-rw-rw-r-- 1 strong strong 1520 Feb 21 09:48 hello_world.o
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.o -o hello_world.out
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 28
drwxrwxr-x 2 strong strong 4096 Feb 21 09:53 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 08:30 ../
-rw-rw-r-- 1 strong strong  475 Feb 20 23:36 hello_world.c
-rw-rw-r-- 1 strong strong 1520 Feb 21 09:48 hello_world.o
-rwxrwxr-x 1 strong strong 8608 Feb 21 09:53 hello_world.out*
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ./hello_world.out 
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$

References

http://c.biancheng.net/

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/87855158