我有一個名為 main.c 的 ac 檔案
#include <stdio.h>
extern int main(int argc, char* argv[], char* envp[]);
void start(){
...;
int return_code = main(argc, argv, envp);
exit(return_code);
}
你可以看到我宣告了 main 但是當使用 ld 鏈接它時:
$ (use ld to link, I didn't write it down because it's quite verbose and irrelevant)
ld: bin/test.o: in function `start':
/home/user/Desktop/test/test.c:28: undefined reference to `main'
make: *** [Makefile:49: link] Error 1
那我該怎么辦(對不起,如果這對你來說是一個簡單的問題)
uj5u.com熱心網友回復:
一般來說,呼叫ld自己是一個貪吃的懲罰。使用您的C編譯器進行鏈接,直到證明并非如此。
gcc -o bin/test bin/test.o將為您鏈接一個 C 程式。
看起來您試圖通過提供_start自己來“修復”它。你不能(在C中)。_start不是函式。
uj5u.com熱心網友回復:
在 C 中,您必須定義一個將由您的程式自動呼叫的 main 函式,這是您的代碼的基礎。
我看到您包含“stdio.h”,這是一個允許訪問某些函式的庫,例如在我的程式中的函式“printf”。如果您不需要它,請不要包含它:)
例如,這里是如何使用 main 函式制作您的第一個程式。
#include <stdio.h>
int main(int argc, char *argv[]){
... // Your code
printf("Hello world"); // Just print on your terminal this string
return (0); // 0 is the default return code if there is no errors
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/430169.html
上一篇:通過平方求冪的時間復雜度是多少?
下一篇:在C中使用指標回圈
