Cant compile c++ on OS X Big Sur, “ld: library not found for -lgcc_s.10.4”

安稳与你 提交于 2021-02-09 07:30:15

问题


After upgrading my MacBook Pro to OS X 11.1 Big Sur, I am unable to get compilation of c++ programs using gcc to work.

I am using CLion with CMake, and I get the following error when reloading the CMake configuration

ld: library not found for -lgcc_s.10.4

The things that I have tried are installing Xcode, it installed without error.

I have tried to create a symlink as suggested here https://github.com/Paxa/fast_excel/issues/33

$ cd /usr/local/lib
$ sudo ln -s ../../lib/libSystem.B.dylib libgcc_s.10.4.dylib

It appears that the library libSystem.B.dylib is not present. Some sites mention that the libraries starting with Big Sur reside in some "shared cache", which I have no idea of what it is and how to access it, let alone make ld access it on its own.

Any suggestions on how to solve this are very welcome. Thank you!


回答1:


In general, gcc tends not to work on more recent versions of Mac OS. The solution is to use the build in C/C++ compilers. To automatically use these, instead of GCC, set these environment variables:

CC="clang"
CXX="clang++"

This will use the built in Mac compilers. Once doing this, I have yet to run into an issue with compiling that wasn't due to the actual code being compiled.




回答2:


According to this answer you should use: g++-10 -o main main.cpp

  • The correct path of brew installed g++ in MacOS is:
$ which g++-10
> /usr/local/bin/g++-10
--
$ which g++ 
> /usr/bin/g++ //this is alias of clang (same for lyb)

If you use CMakeLists.txt file you will configure it like this:

set(CMAKE_CXX_COMPILER "/usr/local/bin/g++-10" CACHE STRING "C compiler" FORCE)
set(CMAKE_C_COMPILER "/usr/local/bin/gcc-10" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_CXX_STANDARD 17)



回答3:


Thank you for all the answers highlighting different things that could solve the issue. What ended up working was to run brew reinstall gcc, and pointing CLion (or plainly CMake as Mike mentioned) to use the correct compiler (something that I had already done, but I want to mention it here if anyone else finds this question and has the same problem), the paths that I use are

/usr/local/Cellar/gcc/10.2.0/bin/gcc-10
/usr/local/Cellar/gcc/10.2.0/bin/g++-10

These paths are actually just the explicit locations that are linked from

/usr/local/bin/g++-10
/usr/local/bin/gcc-10

Also, as Matt Braunstein mentioned, it is possible to use clang that is supplied with Mac OS X, something I did while figuring out how to solve my issues with gcc.

My thoughts on the problem is that somehow, installing gcc with homebrew didnt install everything needed as it appeared to already be installed from a previous version, the reinstall command seemed to rectify this.

Again, thank you for the answers that helped me find this solution, and possible workarounds.



来源:https://stackoverflow.com/questions/65332141/cant-compile-c-on-os-x-big-sur-ld-library-not-found-for-lgcc-s-10-4

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