CMake Error: "add_subdirectory not given a binary directory"

19,757

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.

Share:
19,757
Admin
Author by

Admin

Updated on June 15, 2022

Comments

  • Admin
    Admin almost 2 years

    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