gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:33 51 4
gpt4 key购买 nike

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

我有两个约束:

  • Google Test 的源代码已经在项目结构中的某处(因此无法使用 URL 从 git 存储库下载它)
  • Google Test 的源代码不是我的子项目的子目录(永远不会)

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

add_subdirectory( ${GOOGLETEST_PROJECT_LOCATION})

我收到了:

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.

另一方面,也许 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

最佳答案

错误消息很清楚 - 您还应该为 googletest 指定构建目录

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

当您为 add_subdirectory 调用提供相对路径(作为目录)时,CMake 会自动为构建目录。

但是如果是绝对源路径(并且当这个路径不在你的源树中时),CMake 无法猜测build 目录,你需要提供它明确地:

另见 documentation用于 add_subdirectory 命令。

关于c++ - CMake 错误 : "add_subdirectory not given a binary directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50408169/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com