Search code examples
cmakemanaged-c++

Building both managed and unmanaged C++ applications with CMake?


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?


Solution

  • 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.