Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't binaries placed in CMAKE_CURRENT_BINARY_DIR?

Tags:

c++

cmake

It is my understanding that CMAKE_CURRENT_BINARY_DIR should point to the directory where binaries for the current CMakeLists.txt file will be placed. However, this doesn't seem to be the case.

Consider this file structure:

CMakeTest
+- CMakeLists.txt
+- main.cpp

CMakeLists.txt

cmake_minimum_required(VERSION 3.2)
add_executable(CMakeTest main.cpp)
message(STATUS "CMAKE_CURRENT_BINARY_DIR = ${CMAKE_CURRENT_BINARY_DIR}")

main.cpp

#include <iostream>

int main() {
    std::cout << "Hello, World!";
    return 0;
}

On the (Windows) command line, I run the following commands:

md build
cd build
cmake .. -G "Visual Studio 14 2015"
cmake --build .

The first cmake command prints (among other things) the line

CMAKE_CURRENT_BINARY_DIR = X:/dev/projects/CMakeTest/build

So I'd expect the resulting binary file CMakeTest.exe to end up there. Really, however, it is placed in X:/dev/projects/CMakeTest/build/Debug.

Why isn't the binary file placed into CMAKE_CURRENT_BINARY_DIR, but in a sub-directory? And is there any CMake variable that tells me what that subdirectory is?

Edit:

I'm not trying to change the directory where binaries are placed. I'm trying to determine it. The reason is this:

During build, a number of additional resource files are created in the same directory as the executable file. (This part works.) I'd like to use the install(FILES, ...) command to then add these files to the resulting package. So I need to pass the actual path where the binaries are placed to install(FILES, ...).

like image 564
Daniel Wolf Avatar asked Sep 22 '17 18:09

Daniel Wolf


1 Answers

Variable CMAKE_CURRENT_BINARY_DIR denotes "binary directory currently being processed" by CMake. Usually, this directory and its subdirectories contains build artifacts, like executables, libraries or other generated files.

If you want to control location of executable being built, you need to set variable CMAKE_RUNTIME_OUTPUT_DIRECTORY.

Note, that multiconfiguration build tools, like Visual Studio, for each specific configuration will create subdirectory (named as configuration itself) under CMAKE_RUNTIME_OUTPUT_DIRECTORY. Otherwise, executables created for different configurations would overwrite themselves.

For precise control of per-configuration directory used for built executables, use variable CMAKE_RUNTIME_OUTPUT_DIRECTORY_<CONFIG>. (Instead of <CONFIG> name of specific configuration should be inserted, so CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG variable will affect Debug builds).


For just determine directory with executable, use $<TARGET_FILE_DIR:tgt> generator expression (instead of tgt a name of the target created the executable should be used).

Note, that generator expressions can be used only in specific places. E.g., list of files for install(FILES) command can use generator expression, but message() command cannot.

like image 175
Tsyvarev Avatar answered Sep 18 '22 22:09

Tsyvarev