GCC -o 选项 (生成可执行文件)

GCC -o 选项 (生成可执行文件)

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

-o 选项用来生成二进制可执行文件,格式为:

[file1] -o [file2]

[file1] 表示输入文件 (要处理的文件)。它可以是源文件,也可以是汇编文件或者是目标文件。
[file2] 表示输出文件 (处理的结果),生成的可执行文件。

[file1] 和 [file2] 可以是一个文件,也可以是一组文件:
如果 [file1] 是一组文件,那么就表示有多个输入文件。
如果 [file2] 是一组文件,那么相当于生成了该二进制可执行文件的多个副本。

将源文件作为输入文件

下述程序为编译示例程序,正式程序需要添加 function_printf.h,添加函数声明,主函数中 include 头文件。

有 function_printf.c 和 hello_world.c 两个源文件。其中 function_printf.c 的内容:

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

#include <stdio.h>

void function_printf() {
	puts("!!!Hello Printf!!!"); /* prints !!!Hello Printf!!! */
}

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) {
    function_printf();
	puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
	return EXIT_SUCCESS;
}

下面对 function_printf.c 和 hello_world.c 进行编译:

strong@foreverstrong:~/ForeverStrong/hello_world$ pwd
/home/strong/ForeverStrong/hello_world
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ls -l
total 8
-rw-rw-r-- 1 strong strong 447 Feb 21 11:34 function_printf.c
-rw-rw-r-- 1 strong strong 498 Feb 21 11:34 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc hello_world.c function_printf.c -o app
hello_world.c: In function ‘main’:
hello_world.c:15:5: warning: implicit declaration of function ‘function_printf’ [-Wimplicit-function-declaration]
     function_printf();
     ^
strong@foreverstrong:~/ForeverStrong/hello_world$ 

编译完成后,使用 ls -l 命令查看生成的文件:

strong@foreverstrong:~/ForeverStrong/hello_world$ ls -l
total 20
-rwxrwxr-x 1 strong strong 8688 Feb 21 11:37 app
-rw-rw-r-- 1 strong strong  447 Feb 21 11:34 function_printf.c
-rw-rw-r-- 1 strong strong  498 Feb 21 11:34 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 

function_printf.c 和 hello_world.c 两个源文件被编译、链接后生成了一个名为 app 的可执行文件。

通过下面的方式来执行 app 程序:

strong@foreverstrong:~/ForeverStrong/hello_world$ ./app 
!!!Hello Printf!!!
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$ 

将目标文件作为输入文件

gcc 的输入文件不仅仅可以是 C 语言源文件 (.c 文件),也可以是编译好的目标文件 (.o 文件)。

首先使用 -c 选项来生成两个目标文件:

gcc -c function_printf.c hello_world.c

然后将两个目标文件作为输入文件:

gcc function_printf.o hello_world.o -o app

使用 ll 命令来查看整个过程中生成的所有文件:

strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 36
drwxrwxr-x 2 strong strong 4096 Feb 21 14:02 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 11:14 ../
-rwxrwxr-x 1 strong strong 8688 Feb 21 14:02 app*
-rw-rw-r-- 1 strong strong  447 Feb 21 11:34 function_printf.c
-rw-rw-r-- 1 strong strong 1528 Feb 21 14:01 function_printf.o
-rw-rw-r-- 1 strong strong  498 Feb 21 11:34 hello_world.c
-rw-rw-r-- 1 strong strong 1592 Feb 21 14:01 hello_world.o
strong@foreverstrong:~/ForeverStrong/hello_world$

最后运行生成的 app 程序:

strong@foreverstrong:~/ForeverStrong/hello_world$ ./app 
!!!Hello Printf!!!
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ pwd
/home/strong/ForeverStrong/hello_world
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 2 strong strong 4096 Feb 21 11:44 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 11:14 ../
-rw-rw-r-- 1 strong strong  447 Feb 21 11:34 function_printf.c
-rw-rw-r-- 1 strong strong  498 Feb 21 11:34 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc -c function_printf.c hello_world.c 
hello_world.c: In function ‘main’:
hello_world.c:15:5: warning: implicit declaration of function ‘function_printf’ [-Wimplicit-function-declaration]
     function_printf();
     ^
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ls -l
total 16
-rw-rw-r-- 1 strong strong  447 Feb 21 11:34 function_printf.c
-rw-rw-r-- 1 strong strong 1528 Feb 21 14:01 function_printf.o
-rw-rw-r-- 1 strong strong  498 Feb 21 11:34 hello_world.c
-rw-rw-r-- 1 strong strong 1592 Feb 21 14:01 hello_world.o
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc function_printf.o hello_world.o -o app
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 36
drwxrwxr-x 2 strong strong 4096 Feb 21 14:02 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 11:14 ../
-rwxrwxr-x 1 strong strong 8688 Feb 21 14:02 app*
-rw-rw-r-- 1 strong strong  447 Feb 21 11:34 function_printf.c
-rw-rw-r-- 1 strong strong 1528 Feb 21 14:01 function_printf.o
-rw-rw-r-- 1 strong strong  498 Feb 21 11:34 hello_world.c
-rw-rw-r-- 1 strong strong 1592 Feb 21 14:01 hello_world.o
strong@foreverstrong:~/ForeverStrong/hello_world$ 
strong@foreverstrong:~/ForeverStrong/hello_world$ ./app 
!!!Hello Printf!!!
!!!Hello World!!!
strong@foreverstrong:~/ForeverStrong/hello_world$ 

References

http://c.biancheng.net/

猜你喜欢

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