Jan Hudec
Jan Hudec

Reputation: 76236

Pass target file name as compile definition in cmake

I have CMakeLists.txt with many targets and the targets have configuration-specific suffixes defined. Additionally I want to know the name of the executable inside the program.

Under MSVC++ I do this by defining

add_definitions("-DTARGET_FILE_NAME=\"$(TargetFileName)\"")

But this does not work in other generators. Now that CMake got generator expressions, is there a way that would work with any generator?

I tried something like

add_definitions(-DTARGET_FILE_NAME=$<TARGET_FILE_NAME:$<TARGET_PROPERTY:NAME>>)

but even

add_definitions(-DTARGET_FILE_NAME=$<TARGET_PROPERTY:NAME>)

just places the unescaped $<TARGET_PROPERTY:NAME> in the buildfile. I also tried with just$`, but no luck either.

Note that the compile build command does not know the name of the linker output in many build files, so there does not seem to be any generator-specific hack for some of them either.

Upvotes: 1

Views: 1985

Answers (1)

steveire
steveire

Reputation: 11074

compile_definitions does not support generator expressions. Use target_compile_definitions, which does:

target_compile_definitions(somelib PRIVATE NAME=$<TARGET_FILE_NAME:somelib>)

http://www.cmake.org/cmake/help/v3.1/command/target_compile_definitions.html

Upvotes: 1

Related Questions