我是 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 編譯器。
uj5u.com熱心網友回復:
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,所以它是未定義的。
我猜,您想要gcc -C -c test.i -o test.o創建一個目標檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/353645.html
上一篇:如何從grep輸出中獲取提交
