11

I am using cmake 2.8.12.2. I have set CMAKE_DEBUG_POSTFIX, and it is automatically used with the add_library command. But it is not automatically used with add_executable command. I have discovered that I can set the DEBUG_POSTFIX target property to get a debug postfix into the executable name, but this requires using an additional command.

add_executable(${PROJECT_NAME} ${SOURCE_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})

Is the second command explicitly setting the DEBUG_POSTFIX target property required or is there an easier way?

Phil
  • 5,822
  • 2
  • 31
  • 60
  • Your variable names are confusing me: `PROJECT_NAME` is a bad name for a target, and `CMAKE_DEBUG_POSTFIX` should contain the postfix you want to add. Why do you use a variable? Type it in as a string, at least if you are only using one. And don't use variable names starting with CMAKE_ as it is reserved for CMake's own variables. Or are you mistaken and you think this variable is what you need to pass? Then: You have to pass the postfix you want to add to the debug target. – usr1234567 Mar 06 '15 at 06:56
  • I use a variable for every target and I have done so for a long long time on many projects. – drescherjm Mar 06 '15 at 12:10

2 Answers2

15

The current cmake documentation for set_target_properties states

There is also a <CONFIG>_OUTPUT_NAME that can set the output name on a per-configuration basis. <CONFIG>_POSTFIX sets a postfix for the real name of the target when it is built under the configuration named by (in upper-case, such as “DEBUG_POSTFIX”). The value of this property is initialized when the target is created to the value of the variable CMAKE_<CONFIG>_POSTFIX (except for executable targets because earlier CMake versions which did not use this variable for executables).

So it seems to highlight the fact that cmake does not use the value of CMAKE_DEBUG_POSTFIX in the name of executables. Therefore

add_executable(myexe ${SOURCE_FILES})
set_target_properties(myexe PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})

will use the value of the global variable ${CMAKE_DEBUG_POSTFIX} when building the myexe target for the DEBUG configuration.

Note that one of the commenters to this question was confused by the use of the variable ${PROJECT_NAME}. This variable is automatically set to myexe when using project(myexe). Using ${PROJECT_NAME} is equivalent to myexe and it makes it easier to copy/paste to new CMakeLists.txt.

Phil
  • 5,822
  • 2
  • 31
  • 60
2

Just to mention that, if you have in your project many sub-executables, it is worth having a look on CMAKE_EXECUTABLE_SUFFIX. The variable in this case would need to be changed when running CMake for a specific build (Release/Debug), but this suffix is auto-attached to every ADD_EXECUTABLE(...)-call executable name. Tested and verified with CMake 2.8.12.2 and 3.0.2

CKBergen
  • 108
  • 8