Home > Net >  CMake multi config generator on multi platform
CMake multi config generator on multi platform

Time:05-11

I am working on a project that uses CMake to generate an multi-platform based C projects that need to be compiled in both Windows and Linux platforms. In the Windows based projects, I am interested in generating MSVC based solutions that have these four profiles configurations: (Debug, Release, RelWithDebInfo and MinSizeRel).

Please notice that I set some variables (FLAGS_COMPILE_DEBUG, DEFS_COMPILE_DEBUG, LINKER_OPTIONS_DEBUG, FLAGS_COMPILE_RELEASE, DEFS_COMPILE_RELEASE and LINKER_OPTIONS_RELEASE) containing all the specific flags for each Debug and Release based solutions configurations profiles.

set(FLAGS_COMPILE_DEBUG "/MTd /Od /Zi /RTC1 /Zc:wchar_t /GS /WX")
set(DEFS_COMPILE_DEBUG "-D_DEBUG")
set(LINKER_OPTIONS_DEBUG "/WX")

set(FLAGS_COMPILE_RELEASE "/Ox /Ob2 /Oi /Ot /GF /Zc:wchar_t /MT /WX")
set(DEFS_COMPILE_RELEASE "-D_NDEBUG")
set(LINKER_OPTIONS_RELEASE "/WX")

# Each one VS profile configurations
add_compile_options($<$<CONFIG:Debug>:${FLAGS_COMPILE_DEBUG}>)
add_definitions($<$<CONFIG:Debug>:${DEFS_COMPILE_DEBUG}>)
add_link_options($<$<CONFIG:Debug>:${LINKER_OPTIONS_DEBUG}>)

add_compile_options($<$<CONFIG:Release>:${FLAGS_COMPILE_RELEASE}>)
remove_definitions($<$<CONFIG:Release>:${DEFS_COMPILE_DEBUG}>)
add_definitions($<$<CONFIG:Release>:${DEFS_COMPILE_RELEASE}>)
add_link_options($<$<CONFIG:Release>:${LINKER_OPTIONS_RELEASE}>)

add_compile_options($<$<CONFIG:RelWithDebInfo>:${FLAGS_COMPILE_RELEASE}>)
add_definitions($<$<CONFIG:RelWithDebInfo>:${DEFS_COMPILE_DEBUG}>)
add_link_options($<$<CONFIG:RelWithDebInfo>:${LINKER_OPTIONS_DEBUG}>)

add_compile_options($<$<CONFIG:MinSizeRel>:${FLAGS_COMPILE_RELEASE}>)
remove_definitions($<$<CONFIG:MinSizeRel>:${DEFS_COMPILE_DEBUG}>)
add_definitions($<$<CONFIG:MinSizeRel>:${DEFS_COMPILE_RELEASE}>)
add_link_options($<$<CONFIG:MinSizeRel>:${LINKER_OPTIONS_RELEASE}>)

However, I noticed that I am not getting the desired effect for each Debug and Release based solutions configurations profiles. What is the correct way to use the CMake generator expressions in my case?

CodePudding user response:

add_definitions() and remove_definitions() don't support generator expressions. You want add_compile_definitions().

set(FLAGS_COMPILE_DEBUG /MTd /Od /Zi /RTC1 /Zc:wchar_t /GS /WX)
set(DEFS_COMPILE_DEBUG -D_DEBUG)
set(LINKER_OPTIONS_DEBUG /WX)

set(FLAGS_COMPILE_RELEASE /Ox /Ob2 /Oi /Ot /GF /Zc:wchar_t /MT /WX)
set(DEFS_COMPILE_RELEASE -D_NDEBUG)
set(LINKER_OPTIONS_RELEASE /WX)

add_compile_options(
    "$<$<CONFIG:Debug>:${FLAGS_COMPILE_DEBUG}>"
    "$<$<CONFIG:Release>:${FLAGS_COMPILE_RELEASE}>"
    "$<$<CONFIG:RelWithDebInfo>:${FLAGS_COMPILE_RELEASE}>"
    "$<$<CONFIG:MinSizeRel>:${FLAGS_COMPILE_RELEASE}>"
)
add_compile_definitions(
    "$<$<CONFIG:Debug>:${DEFS_COMPILE_DEBUG}>"
    "$<$<CONFIG:Release>:${DEFS_COMPILE_RELEASE}>"
    "$<$<CONFIG:RelWithDebInfo>:${DEFS_COMPILE_DEBUG}>"
    "$<$<CONFIG:MinSizeRel>:${DEFS_COMPILE_RELEASE}>"
)
add_link_options(
    "$<$<CONFIG:Debug>:${LINKER_OPTIONS_DEBUG}>"
    "$<$<CONFIG:Release>:${LINKER_OPTIONS_RELEASE}>"
    "$<$<CONFIG:RelWithDebInfo>:${LINKER_OPTIONS_DEBUG}>"
    "$<$<CONFIG:MinSizeRel>:${LINKER_OPTIONS_RELEASE}>"
)
  • Related