链接期间未定义主要但在完整编译过程中定义
undefined main during linking but defined during full compilitaion process
我是 C 编程的新手。所以我学习了不同的编译过程(预处理、编译、链接)。我的程序是
#include <stdio.h>
#define testDefinition(x) printf(#x " is equal to %lf\n",x)
int main(void)
{
testDefinition(3.15);
return 0;
}
这是一个没有任何意义的简单程序,但问题是当我使用 gcc -o test test.c
它工作正常,但是当我这样做时
gcc -E test.c -o test.i
gcc -C test.i -o test.o
gcc test.o -o test
我收到错误
usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
我正在使用 Ubuntu 20.04 和 GCC 编译器。
test.o
已经是可执行文件了,你没传-c
.
$ gcc -E test.c -o test.i
$ gcc -C test.i -o test.o
$ ./test.o
3.15 is equal ....
因此,test.o
是一个 ELF 文件,gcc
将其视为共享库(我认为)。因为没有传入源文件gcc test.o -o test
也没有main
,所以是undefined.
我猜,您想 gcc -C -c test.i -o test.o
创建目标文件。
我是 C 编程的新手。所以我学习了不同的编译过程(预处理、编译、链接)。我的程序是
#include <stdio.h>
#define testDefinition(x) printf(#x " is equal to %lf\n",x)
int main(void)
{
testDefinition(3.15);
return 0;
}
这是一个没有任何意义的简单程序,但问题是当我使用 gcc -o test test.c
它工作正常,但是当我这样做时
gcc -E test.c -o test.i
gcc -C test.i -o test.o
gcc test.o -o test
我收到错误
usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
我正在使用 Ubuntu 20.04 和 GCC 编译器。
test.o
已经是可执行文件了,你没传-c
.
$ gcc -E test.c -o test.i
$ gcc -C test.i -o test.o
$ ./test.o
3.15 is equal ....
因此,test.o
是一个 ELF 文件,gcc
将其视为共享库(我认为)。因为没有传入源文件gcc test.o -o test
也没有main
,所以是undefined.
我猜,您想 gcc -C -c test.i -o test.o
创建目标文件。