Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

c++ - CMake Error: "add_subdirectory not given a binary directory"

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...