Issue
CMake 3.23
I'm trying to generate different C++ linker options for debug vs release. First, I setup some variables with the flags. The idea is that some flags pertain to all configs, and some only to debug or release.
It works with target_compile_options
, but not for set_target_properties
.
# Win32 all configs:
set(win32DynLinkerFlagsAllConfigs
/MAP /FORCE
)
# Win32 Debug only:
set(win32DynLinkerFlagsDebug
/DEBUG:FASTLINK
)
# Win32 Release only:
set(win32DynLinkerFlagsRelease
/FOOBAR
)
The following gives an error:
CMake Error at CMake/CommonTarget.cmake:82 (set_target_properties): set_target_properties called with incorrect number of arguments.
set_target_properties(common_target PROPERTIES
INTERFACE_LINK_OPTIONS
"$<${isWindows}:${win32DynLinkerFlagsAllConfigs}>"
"$<${isWindows}:$<$<CONFIG:Debug>:${win32DynLinkerFlagsDebug}>>"
"$<${isWindows}:$<$<CONFIG:Release>:${win32DynLinkerFlagsRelease}>>"
)
The following doesn't give error, but only uses the last one.
set_target_properties(common_target PROPERTIES
INTERFACE_LINK_OPTIONS
"$<${isWindows}:${win32DynLinkerFlagsAllConfigs}>"
INTERFACE_LINK_OPTIONS
"$<${isWindows}:$<$<CONFIG:Debug>:${win32DynLinkerFlagsDebug}>>"
INTERFACE_LINK_OPTIONS
"$<${isWindows}:$<$<CONFIG:Release>:${win32DynLinkerFlagsRelease}>>"
)
However, a similar thing DOES work for compiler options:
target_compile_options(common_target
INTERFACE
"$<${g_isWindows}:${win32CompilerOptionsAllConfigs}>"
"$<${g_isWindows}:$<$<CONFIG:Debug>:${win32CompilerOptionsDebug}>>"
"$<${g_isWindows}:$<$<CONFIG:Release>:${win32CompilerOptionsRelease}>>"
)
How can one accomplish the same thing being done for target_compile_options
above, but for set_target_properties
.
Solution
set_target_properties
expects one argument after each property.
set_target_properties(common_target PROPERTIES
INTERFACE_LINK_OPTIONS
"$<${isWindows}:${win32DynLinkerFlagsAllConfigs}>"
"$<${isWindows}:$<$<CONFIG:Debug>:${win32DynLinkerFlagsDebug}>>"
"$<${isWindows}:$<$<CONFIG:Release>:${win32DynLinkerFlagsRelease}>>"
)
Here, though, you have supplied three and since "$<${isWindows}:$<$<CONFIG:Debug>:${win32DynLinkerFlagsDebug}>>"
is not a property name, you get an error.
Write this instead:
set(link_options
"$<${isWindows}:${win32DynLinkerFlagsAllConfigs}>"
"$<${isWindows}:$<$<CONFIG:Debug>:${win32DynLinkerFlagsDebug}>>"
"$<${isWindows}:$<$<CONFIG:Release>:${win32DynLinkerFlagsRelease}>>"
)
set_target_properties(common_target PROPERTIES "${link_options}")
Answered By - Alex Reinking Answer Checked By - Timothy Miller (WPSolving Admin)