4

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?)?

telephone
  • 1,131
  • 1
  • 10
  • 29
  • Update: My problem with wrong stdlib (and questions above) was caused by a line `set(CMAKE_OSX_DEPLOYMENT_TARGET 10.7)` in one of my sub-CMakeLists.txt; see comment below! – telephone Nov 18 '16 at 22:34

2 Answers2

8

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.

rocambille
  • 15,398
  • 12
  • 50
  • 68
  • It does not work for me. I have Ogre3D as a submodule. Let us only consider to build it standalone. cmake configuring goes well, but compilation (via make) fails. Consider its CMakeLists.txt: https://bitbucket.org/sinbad/ogre/src/108ab0bcc69603dba32c0ffd4bbbc39051f421c9/CMakeLists.txt?at=v1-9. If I remove lines 35-43 and add your code at top, the compilation fails (unknown STL templates from c++14). Keeping the CMakeLists.txt as is, compilation goes well (c++11). – telephone Nov 09 '16 at 18:59
  • After lot a of digging (by looking at compiler calls with `make VERBOSE=1` for cmake), it was a flag `mmacosx-version-min=10.7` to clang causing the compile error [by giving it the wrong headers](http://stackoverflow.com/a/2927217/753850). It turned out that the _CMakeLists.txt_ by Ogre3D was the problem: It had a line `set(CMAKE_OSX_DEPLOYMENT_TARGET 10.7)`. This actually solves my question totally! – telephone Nov 13 '16 at 17:57
2

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

link_libraries("-stdlib=libc++")
arrowd
  • 33,231
  • 8
  • 79
  • 110
  • Thank you! I will first try to get `CMAKE_CXX_STANDARD` work (as in the above answer), but your answer is what I was searching for in the first place (have not tried it yet, though). – telephone Nov 09 '16 at 19:03