Cannot link any C program with gcc on Mac Mojave

安稳与你 提交于 2021-01-06 04:09:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!