set output directory for your binaries

I want to define the output path in which my binary is going to be created. It should reflect the behaviour of CMake build in functions. Variants which exist only for historical reasons are not being considered.

here is my solution:

get_property( hasRunTimeDir TARGET ${targetName} PROPERTY RUNTIME_OUTPUT_DIRECTORY SET )
if( hasRunTimeDir )
	get_target_property( executableOutputPath ${targetName} RUNTIME_OUTPUT_DIRECTORY )
elseif( EXECUTABLE_OUTPUT_PATH )
	set( executableOutputPath ${EXECUTABLE_OUTPUT_PATH} )
else()
	set( executableOutputPath ${CMAKE_CURRENT_BINARY_DIR} )
endif()

this has been tested with GNU Makefile Generator.

Are you trying to change which directory the file gets built in, or get this information out of the target?

the former. It is a generic target which should place the binary on the same location like a target of type executable would do.

Take a look at the CMAKE_RUNTIME_OUTPUT_DIRECTORY variable. Setting it will put the executable in the directory you want. You can also use CMAKE_LIBRARY_OUTPUT_DIRECTORY and CMAKE_ARCHIVE_OUTPUT_DIRECTORY.

thanks. Using CMAKE_RUNTIME_OUTPUT_DIRECTORY directly is much better.

this is the corrected version:

if( CMAKE_RUNTIME_OUTPUT_DIRECTORY )
	set( executableOutputPath ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} )
elseif( EXECUTABLE_OUTPUT_PATH )
	set( executableOutputPath ${EXECUTABLE_OUTPUT_PATH} )
else()
	set( executableOutputPath ${CMAKE_CURRENT_BINARY_DIR} )
endif()

I’ll look into libraries and archives when I get there.