GCC -c 选项 (只编译不链接,生成目标文件)
-c
选项表示编译、汇编指定的源文件 (编译源文件),但是不进行链接。使用 -c
选项可以将每一个源文件编译成对应的目标文件。
目标文件是一种中间文件或者临时文件,如果不设置该选项,gcc
一般不会保留目标文件,可执行文件生成完成后就自动删除了。
strong@foreverstrong:~/ForeverStrong/function_printf$ pwd
/home/strong/ForeverStrong/function_printf
strong@foreverstrong:~/ForeverStrong/function_printf$
strong@foreverstrong:~/ForeverStrong/function_printf$ ll
total 20
drwxrwxr-x 2 strong strong 4096 Feb 21 11:12 ./
drwxrwxr-x 7 strong strong 4096 Feb 21 11:10 ../
-rw-rw-r-- 1 strong strong 451 Feb 21 11:12 function_printf_1.c
-rw-rw-r-- 1 strong strong 451 Feb 21 11:12 function_printf_2.c
-rw-rw-r-- 1 strong strong 451 Feb 21 11:12 function_printf_3.c
strong@foreverstrong:~/ForeverStrong/function_printf$
strong@foreverstrong:~/ForeverStrong/function_printf$ gcc -c function_printf_1.c function_printf_2.c function_printf_3.c
strong@foreverstrong:~/ForeverStrong/function_printf$
strong@foreverstrong:~/ForeverStrong/function_printf$ ll
total 32
drwxrwxr-x 2 strong strong 4096 Feb 21 11:12 ./
drwxrwxr-x 7 strong strong 4096 Feb 21 11:10 ../
-rw-rw-r-- 1 strong strong 451 Feb 21 11:12 function_printf_1.c
-rw-rw-r-- 1 strong strong 1536 Feb 21 11:12 function_printf_1.o
-rw-rw-r-- 1 strong strong 451 Feb 21 11:12 function_printf_2.c
-rw-rw-r-- 1 strong strong 1536 Feb 21 11:12 function_printf_2.o
-rw-rw-r-- 1 strong strong 451 Feb 21 11:12 function_printf_3.c
-rw-rw-r-- 1 strong strong 1536 Feb 21 11:12 function_printf_3.o
strong@foreverstrong:~/ForeverStrong/function_printf$
strong@foreverstrong:~/ForeverStrong/function_printf$ ls -l *.o
-rw-rw-r-- 1 strong strong 1536 Feb 21 11:12 function_printf_1.o
-rw-rw-r-- 1 strong strong 1536 Feb 21 11:12 function_printf_2.o
-rw-rw-r-- 1 strong strong 1536 Feb 21 11:12 function_printf_3.o
strong@foreverstrong:~/ForeverStrong/function_printf$
如果不使用 -c
选项,则仅仅生成一个可执行文件,没有目标文件。使用 -c
选项表示只编译源文件,而不进行链接,对于链接中的错误是无法发现的。
gcc
编译器在使用 -c
选项的时候不会发现链接错误。
(1) 编辑两个源文件。
在 function_printf.c 中定义了 function_printf() 函数:
/*
============================================================================
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 中调用 function_printf() 和 function_scanf() 函数:
/*
============================================================================
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();
function_scanf();
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
function_scanf() 函数并没有定义,所以在链接时会产生错误 (编译时不会产生错误)。
(2) 使用 -c
选项编译两个源文件。
strong@foreverstrong:~/ForeverStrong/hello_world$ pwd
/home/strong/ForeverStrong/hello_world
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 2 strong strong 4096 Feb 21 11:15 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 11:14 ../
-rw-rw-r-- 1 strong strong 447 Feb 21 11:15 function_printf.c
-rw-rw-r-- 1 strong strong 498 Feb 21 11:15 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();
^
hello_world.c:16:5: warning: implicit declaration of function ‘function_scanf’ [-Wimplicit-function-declaration]
function_scanf();
^
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 24
drwxrwxr-x 2 strong strong 4096 Feb 21 11:19 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 11:14 ../
-rw-rw-r-- 1 strong strong 447 Feb 21 11:15 function_printf.c
-rw-rw-r-- 1 strong strong 1528 Feb 21 11:19 function_printf.o
-rw-rw-r-- 1 strong strong 520 Feb 21 11:17 hello_world.c
-rw-rw-r-- 1 strong strong 1656 Feb 21 11:19 hello_world.o
strong@foreverstrong:~/ForeverStrong/hello_world$
编译器没有输出 error information。
(3) 不使用 -c
选项编译两个源文件。
报错信息:
strong@foreverstrong:~/ForeverStrong/hello_world$ pwd
/home/strong/ForeverStrong/hello_world
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 2 strong strong 4096 Feb 21 11:21 ./
drwxrwxr-x 6 strong strong 4096 Feb 21 11:14 ../
-rw-rw-r-- 1 strong strong 447 Feb 21 11:15 function_printf.c
-rw-rw-r-- 1 strong strong 520 Feb 21 11:17 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc 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();
^
hello_world.c:16:5: warning: implicit declaration of function ‘function_scanf’ [-Wimplicit-function-declaration]
function_scanf();
^
/tmp/ccGch1W8.o: In function `main':
hello_world.c:(.text+0x14): undefined reference to `function_scanf'
collect2: error: ld returned 1 exit status
strong@foreverstrong:~/ForeverStrong/hello_world$
由于没有找到 function_scanf() 函数的定义,发生了链接错误。