简体   繁体   中英

MacOs: How to change the C++ compiler and stdlib

I have a C++ project with CMake (and conan) and I want to switch the compiler to GCC-10, and its corresponding stdlibc++. How do I do this on MacOS (Big Sur).

Specifying -DCMAKE_CXX_COMPILER= which g++-10 in the cmake configure command successfully makes the switch to the g++ 10 compiler that I have on my system (installed with brew). However, I do not see any indicator that the includes and the stdlib are changed.

My buidld command looks like:

/usr/local/bin/g++-10 -DCONCORE_USE_GLM=1 -DCONCORE_USE_OPENMP=1 -DCONCORE_USE_TBB=1 -DRC_USE_RTTI -I../test/. -I../test/../include -I../include -isystem /usr/local/Cellar/tbb/2020_U3/include -isystem /Users/lucteo/.conan/data/Catch2/2.11.0/catchorg/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /Users/lucteo/.conan/data/rapidcheck/20200131/_/_/package/d2dbafdccc1ddd834eb76a31bdfdc6cc51e23ec1/include -fsanitize=address -fsanitize=undefined  -D_GLIBCXX_USE_CXX11_ABI=1 -O3 -DNDEBUG -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -Wall -std=gnu++17 -MD -MT test/CMakeFiles/test.concore.dir/func/test_serializers.cpp.o -MF test/CMakeFiles/test.concore.dir/func/test_serializers.cpp.o.d -o test/CMakeFiles/test.concore.dir/func/test_serializers.cpp.o -c ../test/func/test_serializers.cpp

What is the best way to switch the compiler and stdlib?

To change stdlib you need to provide the -stdlib=stdlibc++ flag to the compiler in order to activate stdlibc++. This can be done through ccmake (turn on advanced mode (with t), and set CMAKE_CXX_FLAGS to -stdlib=stdlibc++ ), or through an equivalent directive in your CMakeLists.txt:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=stdlibc++")

You also may need set(CMAKE_EXE_LINKER_FLAGS "-stdlib=stdlibc++") :

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=stdlibc++")

To update your conan profile (say default ) use the command as follows:

conan profile update settings.compiler.libcxx=stdlibc++ default

Note: -stdlib is a Clang flag and will not work with any version of GCC ever released. GCC always uses libstdc++ unless you tell it to use no standard library at all with the -nostdlib option (in which case you either need to avoid using any standard library features, or use -I and -L and -l flags to point it to an alternative set of header and library files).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM