36

I have a very simple directory structure:

Project
Project/src
Project/build

Source files are in Project/src, and I do the out-of-src build in Project/build. After running cmake ../ ; make, I can run the executable thusly: Project/build$ src/Executable - that is, the Executable is created in the build/src directory.

How do I set the location of the executable in the CMakeLists.txt file? I've attempted to follow some of the examples found at cmake.org, but the links that work don't seem to show this behaviour.

My Project/src/CMakeLists.txt file is listed here.


include_directories(${SBSProject_SOURCE_DIR}/src)
link_directories(${SBSProject_BINARY_DIR}/src)

set ( SBSProject_SOURCES
    main.cpp
    )

add_executable( TIOBlobs ${SBSProject_SOURCES})

And the top-level Project/CMakeLists.txt:


cmake_minimum_required (VERSION 2.6)
project (SBSProject)

set (CMAKE_CXX_FLAGS "-g3 -Wall -O0") 

add_subdirectory(src)

3 Answers 3

43

You have a couple of choices.

To change the default location of executables, set CMAKE_RUNTIME_OUTPUT_DIRECTORY to the desired location. For example, if you add

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

to your Project/CMakeLists.txt before the add_subdirectory command, your executable will end up in Project/build for Unix builds or build/<config type> for Win32 builds. For further details, run:

cmake --help-property RUNTIME_OUTPUT_DIRECTORY

Another option for a project of this size is to have just one CMakeLists.txt. You could more or less replace add_subdirectory(src) with the contents of Project/src/CMakeLists.txt to achieve the same output paths.

However, there are a couple of further issues.

You probably want to avoid using link_directories generally. For an explanation, run

cmake --help-command link_directories

Even if you do use link_directories, it's unlikely that any libraries will be found in ${SBSProject_BINARY_DIR}/src

Another issue is that the CMAKE_CXX_FLAGS apply to Unix builds, so should probably be wrapped in an if (UNIX) ... endif() block. Of course, if you're not planning on building on anything other than Unix, this is a non-issue.

Finally, I'd recommend requiring CMake 2.8 as a minimum unless you have to use 2.6 - CMake is an actively-developed project and the current version has many significant improvements over 2.6

So a single replacement for Project/CMakeLists.txt could look like:

cmake_minimum_required (VERSION 2.8)
project (SBSProject)

if (UNIX)
  set (CMAKE_CXX_FLAGS "-g3 -Wall -O0")
endif ()

include_directories (${SBSProject_SOURCE_DIR}/src)

set (SBSProject_SOURCES
    ${SBSProject_SOURCE_DIR}/src/main.cpp
    )

add_executable (TIOBlobs ${SBSProject_SOURCES})
10

Another way of relocating the executable file location is via set(EXECUTABLE_OUTPUT_PATH Dir_where_executable_program_is_located)

1
6

build/'config type' for Win32 builds.

For MSVC, to avoid the default "/Debug" created folder

set_target_properties(my_target
PROPERTIES 
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.