CMake错误:"add_subdirectory没有给出二进制目录"

Fil*_*lip 6 c++ build cmake googletest

我正在尝试将Google Test集成到更大项目的子项目中,但我找不到令我满意的解决方案.

我有两个限制: - Google Test的源代码已经在项目结构中的某个位置(因此使用URL从git存储库下载它不是一个选项) - Google Test的源代码不是我子项目的子目录(和永不)

所以,当我尝试做这样的事情时:

add_subdirectory( ${GOOGLETEST_PROJECT_LOCATION})
Run Code Online (Sandbox Code Playgroud)

我收到了:

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.
Run Code Online (Sandbox Code Playgroud)

另一方面,也许ExternalProject_Add可能是一个解决方案,但我不知道如果我不想下载源代码并使用项目中特定位置的源代码,我该如何使用它.

项目结构看起来更像或像这样:

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
Run Code Online (Sandbox Code Playgroud)

Tsy*_*rev 11

错误消息很明确 - 您还应该为googletest 指定构建目录.

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

当您提供相对路径(作为目录)进行add_subdirectory调用时,CMake会自动为构建目录使用相同的相对路径.

但是在绝对源路径的情况下(当此路径不在源树中时),CMake无法猜测构建目录,您需要明确地提供它:

另请参见文档add_subdirectory命令.