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)
You have a couple of choices.
To change the default location of executables, set
CMAKE_RUNTIME_OUTPUT_DIRECTORYto the desired location. For example, if you addto your
Project/CMakeLists.txtbefore theadd_subdirectorycommand, your executable will end up inProject/buildfor Unix builds orbuild/<config type>for Win32 builds. For further details, run: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 ofProject/src/CMakeLists.txtto achieve the same output paths.However, there are a couple of further issues.
You probably want to avoid using
link_directoriesgenerally. For an explanation, runEven if you do use
link_directories, it’s unlikely that any libraries will be found in${SBSProject_BINARY_DIR}/srcAnother issue is that the
CMAKE_CXX_FLAGSapply to Unix builds, so should probably be wrapped in anif (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.txtcould look like: