Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake executable location

Tags:

cmake

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) 
like image 629
simont Avatar asked Feb 19 '12 01:02

simont


People also ask

Where is my CMake executable?

Run cmake-gui.exe, which should be in your Start menu under Program Files, there may also be a shortcut on your desktop, or if you built from source, it will be in the build directory.

Does CMake create executable?

txt files) are used to generate standard build files (e.g., makefiles on Unix and projects/workspaces in Windows MSVC) which are used in the usual way. CMake can generate a native build environment that will compile source code, create libraries, generate wrappers and build executables in arbitrary combinations.

How do I change my path in CMake?

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .

Where is CMakeLists TXT located?

CMakeLists. txt is placed at the root of the source tree of any application, library it will work for. If there are multiple modules, and each module can be compiled and built separately, CMakeLists. txt can be inserted into the sub folder.


2 Answers

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}) 
like image 173
Fraser Avatar answered Sep 21 '22 15:09

Fraser


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

like image 24
feelfree Avatar answered Sep 21 '22 15:09

feelfree