I have the following content in my CMakeLists.txt:
project( Matfile )
SET ( CMAKE_CXX_FLAGS \"-std=c++0x\" )
set ( SOURCES
\"foo.cpp\"
\"bar.cp
I had this situation with C++ / clr project, where I needed to modify two variables - CMAKE_CXX_FLAGS
and CMAKE_CXX_FLAGS_DEBUG
. CMAKE_CXX_FLAGS
can be modified using set_source_files_properties / COMPILE_FLAGS
, but not CMAKE_CXX_FLAGS_DEBUG
.
I've concluded to create separate, project specific cmake file, in separate folder.
If project is isolated - then variable modifying will work only within current C++/clr project.
I guess if you want to have multiple C++ projects and multiple C++/clr projects, makes sense to separate them at least in two different folder (if not more) - native C++ specific, and managed C++ specific.
In similar manner you can find out on cmake test application: https://github.com/Kitware/CMake/blob/master/Tests/CSharpLinkToCxx/CMakeLists.txt
Here is the same thing, only with more explanation:
# These variables affects also other C++ projects, that's why separate cmake for this c++ dll
# Clr compiling requires these:
# "Enable C++ Exceptions" - "Yes (/EHsc)" => "Yes with SEH Exceptions (/EHa)"
string(REPLACE "/EHsc" "/EHa" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# "Basic Run-time checks" - "Both (/RTC1, equiv. to /RTCsu) (/RTC1)" => "Default"
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
This is one approach. If you however want to generate native c++ and managed c++ projects in a loop - one approach is to disable global flags:
# Disable global flag, configure on per project basis, using target_compile_options, target_link_options
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REPLACE "/INCREMENTAL" "" CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
#string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
and then configure each project setting individually for each project, for example like this:
if(${is_managed})
# Managed C++ code
set_target_properties(${project} PROPERTIES VS_GLOBAL_CLRSupport "true")
set_property(TARGET ${project} PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.7.2")
# Being able to debug dll
target_link_options(${project} PRIVATE "$<$<CONFIG:Debug>:/ASSEMBLYDEBUG>")
# Disable incremental linking
target_link_options(${project} PRIVATE "$<$<CONFIG:Debug>:/INCREMENTAL:NO>")
# "Enable C++ Exceptions" - "Yes with SEH Exceptions (/EHa)"
set(compile_flags /EHa)
else()
# Native C++ code
# "Basic Run-time checks" - "Both (/RTC1, equiv. to /RTCsu) (/RTC1)" => "Default"
target_compile_options(${project} PRIVATE "$<$<CONFIG:Debug>:/RTC1>")
# Enable incremental linking
target_link_options(${project} PRIVATE "$<$<CONFIG:Debug>:/INCREMENTAL>")
# "Enable C++ Exceptions" - "Yes (/EHsc)"
set(compile_flags /EHsc)
endif()
set_target_properties(${project} PROPERTIES COMPILE_FLAGS ${compile_flags})
This is the solution I currently use:
if(CMAKE_COMPILER_IS_GNUCXX)
add_definitions(-std=c++0x)
add_definitions(-std=gnu++11)
endif()
Or, if you have an older version of cmake and you want to see it show up in cmake-gui:
set_property(CACHE CMAKE_CXX_FLAGS PROPERTY VALUE "-std=c++0x")
I've come up with a better method that works on older versions of cmake, e.g. Ubuntu 14 has 2.8.12, and target expressions are 3.3 feature.
Here is how you would set some C++ specific flags:
STRING(REGEX REPLACE "<FLAGS>" "<FLAGS> -std=gnu++11 -fpermissive -fexceptions " CMAKE_CXX_COMPILE_OBJECT ${CMAKE_CXX_COMPILE_OBJECT})
Does it help to use the FORCE
flag?
SET ( CMAKE_CXX_FLAGS "-std=c++0x" CACHE STRING "compile flags" FORCE)