Reputation: 45284
I have a C++ project with mostly native code (core libraries + WIndows service) and a configuration GUI written in managed C++. I've ported most of this project to CMake, but I'm stuck with building the managed C++ code using CMake.
The top-level targets look like:
# Native C++ static libraries.
add_library(sql STATIC ...)
add_library(w32 STATIC ...)
add_library(smdr STATIC ...)
# Native C++ programs depending on above libraries.
add_executable(logging-service ...)
target_link_libraries(logging-service sql w32 smdr)
# ... more native executables here...
# Managed C++ GUI application (doesn't link with any of the above).
add_executable(configuration-editor ...)
I can't find any relevant information in the CMake documentation, and most solutions I see in search results (e.g. messages in the mailing list) involve tweaking the CMAKE_CXX_FLAGS*
variables. However, this creates conflicts the other C++ projects.
How would you set up the build scripts to have two sets of C++ compiler flags, one for native libraries and one for managed applications?
Upvotes: 1
Views: 1130
Reputation: 45284
After reading of various posts in the CMake mailing lists, it seems that the CMAKE_CXX_FLAGS*
variables are local to each CMakeLists.txt
and inherited by child directories using add_subdirectory()
.
So apparently, the recommended way to have multiple sets of compiler flags is to make sibling sub-directories that each set their own compiler flags. I just added two folders native
and managed
and each sets their own compiler flags in their respective CMakeLists.txt
. Now, everthing builds as expected.
Upvotes: 2
Reputation: 15110
Assuming you have something along the lines of:
project(my_libraries)
...
add_library(standard SHARED ${src_files})
add_library(managed SHARED ${src_files})
then you can do:
set(incomp_flags <list of incompatible flags>)
get_property(comp_flags TARGET managed PROPERTY COMPILE_FLAGS)
foreach(flag ${incomp_flags})
list(REMOVE_ITEM comp_flags ${flag})
endforeach()
set_property(TARGET managed PROPERTY COMPILE_FLAGS ${comp_flags} <flag for managed build>)
The idea is that you need to get the flags for the build of the managed library, and then you need to remove incompatible ones and add the one you want.
Upvotes: 0