ju.
ju.

Reputation: 344

CMAKE_CURRENT_BINARY_DIR returning the wrong directory

I have the following folder structure:

- SomeApp
  * CMakeLists.txt
  * source.cpp
  - cmake-build-debug
  - protobuf
    * CMakeLists.txt
    * file.proto

In my root CMakeLists.txt file is written:

cmake_minimum_required(VERSION 3.9)
project(SomeApp)

message("Root Dir: ${CMAKE_CURRENT_BINARY_DIR}")

ADD_SUBDIRECTORY(protobuf)

And in my ./protobuf/ CMakeLists.txt is for testing just written:

message("Protobuf Dir: ${CMAKE_CURRENT_BINARY_DIR}")

During compiling, I get as output:

Root Dir: /home/<...>/SomeApp/cmake-build-debug
Protobuf Dir: /home/<...>/SomeApp/cmake-build-debug

According to https://cmake.org/cmake/help/v3.0/variable/CMAKE_CURRENT_BINARY_DIR.html, the actual output should be:

Root Dir: /home/<...>/SomeApp/
Protobuf Dir: /home/<...>/SomeApp/protobuf/

What is going wrong with my cmake settings?

Upvotes: 2

Views: 4573

Answers (1)

Th. Thielemann
Th. Thielemann

Reputation: 2825

In your case the variable CMAKE_CURRENT_BINARY_DIR displays the path to a subdirectory of your build directory CMAKE_BINARY_DIR.

Assume your code is in /home/myname/SomeApp and your are in the sibling folder /home/myname/cmake-build-debug.

  • The value of CMAKE_BINARY_DIR is /home/myname/cmake-build-debug.
  • The value of CMAKE_CURRENT_BINARY_DIR is /home/myname/cmake-build-debug.
  • In this case CMAKE_CURRENT_BINARY_DIR is equal to CMAKE_CURRENT_DIR.

Extending the example: You have a CMakeLists.txt in the subfolder unittests.

  • CMAKE_BINARY_DIR is /home/myname/cmake-build-debug.
  • CMAKE_CURRENT_BINARY_DIR is /home/myname/cmake-build-debug/unittests.

On Linux you can perform the following commands to review this:

cd /home/myname/SomeApp
cd ..
mkdir cmake-build-debug
cmake ../SomeApp
make

Upvotes: 2

Related Questions