问题
I've just updated to OSX 10.14.6 (Mojave) and now, after upgrading Xcode and migrating MacPorts, even the simplest C program will not link with any gcc version. For example, linking the program
int main() {}
gives:
dcs16> gcc test.c
ld: library not found for -lSystem
collect2: error: ld returned 1 exit status
I am using gcc 6.5 in /opt/local/bin:
dcs16> gcc --version
gcc (MacPorts gcc6 6.5.0_4) 6.5.0
The Xcode version is 11.0 and I have installed the package (which was recommended for solving some compile problems that other people had):
/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
Any help is greatly appreciated.
Note: There is no problem linking with clang.
Note gcc search dirs are:
test> gcc -print-search-dirs
install: /opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/
programs: =/opt/local/libexec/gcc/x86_64-apple-darwin18/6.5.0/:/opt/local/libexec/gcc/x86_64-apple-darwin18/6.5.0/:/opt/local/libexec/gcc/x86_64-apple-darwin18/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../../../x86_64-apple-darwin18/bin/x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../../../x86_64-apple-darwin18/bin/
libraries: =/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../../../x86_64-apple-darwin18/lib/x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../../../x86_64-apple-darwin18/lib/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../
回答1:
Adding "-L/usr/lib" solves the problem.
Also downgrading from Xcode 11.0 to Xcode 10.3 solves the problem! Note: I also checked Xcode 11.1 which just came out but the problem still exists with 11.1.
Note added: Another programmer informs me that upgrading to OSX 10.15 solves the problem. So this is an incompatibility between OSX 10.14 and Xcode 11.
[Added 28/12/2020] And another programmer informs me that he ran into this problem with Xcode 12.3. His solution was to downgrade to 12.2.
来源:https://stackoverflow.com/questions/58072318/cannot-link-any-c-program-with-gcc-on-mac-mojave