I have a top level project that sets the standard to 14, set(CMAKE_CXX_STANDARD 14).
I have a target below (that is included via add_subdirectory) that links to an external library that requires the -std=gnu++1y flag to be set.
I added target_compile_options(myTarget PUBLIC -std=gnu++1y) to the target. However, when I build this target, the end of my makefile looks like “…-std=gnu++1y -std=c++14 -o myFile.cpp”. The set CMAKE_CXX_STANDARD is overriding the target compile option. Is there a way for me to work around this?
Note, I originally just didn’t set the CMAKE_CXX_STANDARD at all. Instead, I had an add_compile_options(-std=gnu++1y) set in the top level project. However after linking (target_link_library) a new library (Qt), this library appears to be setting the -std=c++11 internally, overriding the -std=gnu++1y setting, which leaves me with the desire to set the CMAKE_CXX_STANDARD at the top level. Alternatively if I can enforce that -std=gnu++1y will always be set for the project, that may be ideal.
set_property(TARGET tgt PROPERTY CXX_STANDARD 14)
Alternatively (not recommended):
set(CMAKE_CXX_STANDARD 11)
add_executable(tgt1 tgt1.cxx)
set(CMAKE_CXX_STANDARD 14)
add_executable(tgt2 tgt2.cxx)
Thanks for the quick response!
On that feedback, I was able to do
unset(CMAKE_CXX_STANDARD)
add_library(foo foo.cxx)
set(CMAKE_CXX_STANDARD 14)
Unfortunately, it seems like I need my project to just use the -std=gnu++1y
flag, rather than -std=c++14
. That said is there a way to enforce the -std=gnu++1y
to always be at the end of the compilation flags. Specifically when I link in and build with Qt (target_link_libraries(myTarget PRIVATE Qt::Core)
, I don’t want their std=c++11
flag to come after mine, overriding it. Any ideas?
I did try
find_package(Qt5Core)
set_property(TARGET Qt5::Core PROPERTY COMPILE_OPTIONS -std=gnu++1y)
But there is still something in there that is forcing whatever I link to Qt5::Core to append -std=c++11
at the end of the build flags.
If you want the GNU extensions, please try the following:
set_property(TARGET foo PROPERTY CXX_STANDARD 14)
set_property(TARGET foo PROPERTY CXX_EXTENSIONS ON)
We generally try to avoid manually passing -std=
flags - CMake prefers to handle this on its own.
Yes! That fixed my issue! Previously I thought I had tried to compile with CXX_EXTENSIONS ON
, but apparently not. That seems to be equivalent to -std=gnu++1y
.
Side note, if I did need my flag (~-std=gnu++1y) to be at the end, behind the
CXX_STANDARD` flag, I’m not sure how I would’ve accomplished this. This issue seems to relate to my struggle, https://gitlab.kitware.com/cmake/cmake/issues/16608. Though my issue is solved
, thank you!