I have the Nlhomann JSON library as a sub-project in a subdirectory in my CMake-based parent project. The library is referenced by my CMakeLists.txt which is essentially defined as below. The problem is that when building the whole project this library somehow adds an unwanted -std=gnu++11 compiler flag after my already specified -std=c++2a flag. This addition makes the compilation to fail as my source code requires the latter.
Do you know how to modify my CMakeLists.txt in order to:
- remove this specific flag specified in the
jsonsub-project - force the compilation of the sub-project to use the C++ standard I want
An answer to either one is okay, but if you can answer both, that would be much appreciated.
# CMakeLists.txt
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options(-std=c++2a) # repetitive probably
set(JSON_Install OFF CACHE INTERNAL "") # disable installation
add_subdirectory(json)
add_executable(${PROJECT_NAME}
"main.cpp"
)
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)
I tried this (ugly) solution, but it did not work:
get_target_property(_opt_old nlohmann_json INTERFACE_COMPILE_OPTIONS)
string(REPLACE "gnu++11" "c++2a" _opt_new "${_opt_old}")
set_target_properties(nlohmann_json PROPERTIES INTERFACE_COMPILE_OPTIONS "${_opt_new}")