CMake Error: "add_subdirectory not given a binary directory"
Asked Answered
C

2

22

I am trying to integrate Google Test into the subproject of bigger project and I cannot find the solution that would be satisfying for me.

I have two constraints:

  • the source code of Google Test is already somewhere in the project structure (thus using URL to download it from git repository is not an option)
  • the source code of Google Test is not a subdirectory of my subproject (and never will)

So when I tried to do something like this:

add_subdirectory( ${GOOGLETEST_PROJECT_LOCATION})

I received:

CMake Error at unit_tests/CMakeLists.txt:10 (add_subdirectory):
  add_subdirectory not given a binary directory but the given source
  directory "${GOOGLETEST_PROJECT_LOCATION}" is not a subdirectory of
  "${UNIT_TEST_DIRECTORY}".  When
  specifying an out-of-tree source a binary directory must be explicitly
  specified.

On the other hand maybe ExternalProject_Add could be a solution but I do not know how shall I use it when I do not want to download sources at all and use sources from specific location in the project.

Project structure looks more or like like this:

3rdparty 
    |--googletest 
...
subproject
   |--module1
      |--file1.cpp
      |--CMakeLists.txt
   |--module2
      |--file2.cpp
      |--CMakeLists.txt
   |--include
      |--module1
          |--file1.h
      |--module2
          |--file2.h
   |--unit_test
       |--module1
           |--file1test.cpp
       |--module2
           |--file2test.cpp
       |--CMakeLists.txt
   |--CMakeLists.txt
CMakeLists.txt
Crayfish answered 18/5, 2018 at 9:32 Comment(0)
W
35

The error message is clear - you should also specify build directory for googletest.

# This will build googletest under build/ subdirectory in the project's build tree
add_subdirectory( ${GOOGLETEST_PROJECT_LOCATION} build)

When you give relative path (as a source directory) to add_subdirectory call, CMake automatically uses the same relative path for the build directory.

But in case of absolute source path (and when this path isn't in your source tree), CMake cannot guess build directory, and you need to provide it explicitly:

See also documentation for add_subdirectory command.

Welby answered 18/5, 2018 at 9:51 Comment(2)
Is ../Lib1 considered absolute to CMake? since having this kind of relative path gives this error as well. I thought its ok with relative path not to include a bin dir!Whitewood
In my answer post there is another condition for the source path - "when this path isn't in your source tree" - when CMake requires explicit specification of the build directory. That is, if you call add_subdirectory(../Lib1) from the top CMakeLists.txt, CMake will emit error about "not given a binary directory". Note also, using a build directory outside of the top-level build directory is something which should be avoided.Welby
H
-1

I feel obligated to comment on this because this was the top search result when I was googling this error.

For me, I'm apparently an idiot: I had modified the CMakeLists.txt file in the src directory of my project, but I didn't realize the file was locked and VS Code wasn't actually saving even when I hit Ctrl+S. Check the file tab in VS Code and see if there's a white dot there, indicating the file isn't saved. Hit Ctrl+S and see if you get a pop-up in the lower-right corner prompting you to try again as superuser.

I've still got errors, but they're new errors that make sense for my project.

Hurryscurry answered 13/1, 2022 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.