0

I run cmake in the path "${PROJECT_NAME}/build", and I want generate the executable file in "${PROJECT_NAME}/bin" not "${PROJECT_NAME}/build".

When I used "ADD_EXECUTABLE(../bin/${PROJECT_NAME} ${sources})", I got the error: The target name "../bin/xxxx" is reserved or not valid for certain CMake features, such as generator expressions, and may result in undefined behavior.

Then I changed to "ADD_EXECUTABLE(${PROJECT_NAME} ${sources})", it passed, but it's generated in "${PROJECT_NAME}/build".

What can I do?

xieqin
  • 3
  • 1

2 Answers2

0

Try using custom command post build to copy the executable:

set(COPY_TO_PATH ${PROJECT_NAME}/bin)
add_custom_command(TARGET xxx POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy
                       ${PROJECT_NAME}/bin/xxx
                       ${COPY_TO_PATH})
harry
  • 970
  • 6
  • 25
0

add_executable, as well as add_library and other functions like target_include_directories expect a name of a target, not path.

To change location of output files you're suggested to change a few properties for each target

set_target_properties(my_target PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../bin"
        PDB_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../bin"

There are also a few properties to control where libraries and archives go to: LIBRARY_OUTPUT_DIRECTORY and ARCHIVE_OUTPUT_DIRECTORY, you can set them to something too

Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37