對于我的編譯器課程,我遵循Abdulaziz Ghuloum 的編譯器構造增量方法以及隨附的教程。在某些時候,我們有以下兩個 .c 檔案:
runtime.c
#include <stdio.h>
int main(int argc, char** argv) {
printf("%d\n", entry_point());
return 0;
}
ctest.c
int entry_point() {
return 7;
}
然后作者運行以下命令:
$ gcc -Wall ctest.c runtime.c -o test
[bunch of warnings]
$ ./test
7
$
但是當我運行時,$ gcc -Wall ctest.c runtime.c -o test我收到以下錯誤:
runtime.c:9:20: error: implicit declaration of function 'entry_point' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
printf("%d\n", entry_point());
我希望能夠以與作者使用 gcc 相同的方式編譯和鏈接我的兩個 .c 檔案,但它一直向我拋出該錯誤。我一直在做一些研究,但同樣的命令 ( $ gcc file1.c file2.c -o combined) 不斷出現。幫助將不勝感激。
我在 MacOS Monterey 12.6 上運行它并進行gcc --version顯示:
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
提前致謝
uj5u.com熱心網友回復:
在 macOS 上,默認編譯器是clang,而不是gcc. 后者只是一個符號鏈接,clang所以要記住這一點。
Clang 看到了對entry_point()in的呼叫runtime.c,但還不知道。對于這樣一個未定義的函式,傳統的 C 語言假設它回傳int并且不接受引數。但是 Clang 默認情況下會走安全路線,而不是僅僅警告它,而是將其視為錯誤,因為大多數情況下這種假設只是錯誤的并且可能導致運行時問題。
您有多種選擇:
- 在您
int entry_point(void);的.#includeruntime.c - 在.
int entry_point(void);_runtime.c - 傳遞
-Wno-error=implicit-function-declaration給編譯器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/518352.html
標籤:C苹果系统海合会
上一篇:數素數無限回圈
