$<TARGET_PROPERTY:...> generator expression fails with Visual Studio generator
The following minimal example works with Ninja and NMake generators, but fails with Visual Studio 2017 generator:
cmake_minimum_required(VERSION 3.9)
add_library(lib source.c)
set_source_files_properties(source.c PROPERTIES
COMPILE_FLAGS "-DTARGET_FILENAME=\\\"$<TARGET_FILE_NAME:$<TARGET_PROPERTY:NAME>>\\\"")
Error message:
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:NAME>
$<TARGET_PROPERTY:prop> may only be used with binary targets. It may not
be used with add_custom_command or add_custom_target. Specify the target
to read a property from using the $<TARGET_PROPERTY:tgt,prop> signature
instead.
I share the same Win32 resource file across multiple libraries and that resource file needs to know its target filename, hence the above code.
The problem can be worked around by using target_compile_definitions()
.