Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let cmake with clang use c++11 (c++14)

My cmake project shall compile c++14 code. It also uses the CMakeLists.txts included from its external libraries (which are git submodules in my project). The build fails on macOS Sierra (cmake 3.6.2) because the default STL of clang is old and doesn't handle c++11. As far as I understand, there are two STLs shipped with clang: libstdc++ (from gcc) (default) or libc++. So if I add the -stdlib=libc++ option to cmake, the source compiles:

add_compile_options( "$<$<COMPILE_LANGUAGE:CXX>:-std=c++14>" )
add_compile_options( "$<$<COMPILE_LANGUAGE:CXX>:-stdlib=libc++>" )

But then it fails at link time because it tries to use libstdc++ for linking. How do I specify in cmake that the new STL libc++ shall be used for the whole build process?

PS: What is the rationale behind clang using the gcc STL by default if it is too old? Could I permanently specify which STL it shall use? Or am I doing something completely wrong (could some of my subprojects silently force gcc?)?

like image 550
telephone Avatar asked Dec 23 '22 23:12

telephone


2 Answers

You should rely on CMake to handle compile options. Just specify the wanted standard version:

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

target_compile_features can also be used to require particular features of the standard (and implicitly ask CMake to set the adequate configuration). More information here.


EDIT

You figured out the solution, you also had to remove the following line in the CMakeLists of Ogred3D:

set(CMAKE_OSX_DEPLOYMENT_TARGET 10.7)

Removing it prevented CMake to add the flag mmacosx-version-min=10.7 causing the error.

like image 133
rgmt Avatar answered Dec 28 '22 20:12

rgmt


I suppose, you also need to pass that flang to the linker in the clang case:

link_libraries("-stdlib=libc++")
like image 26
arrowd Avatar answered Dec 28 '22 20:12

arrowd