Issue
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?
Solution
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
Answered By - Alexey Larionov