CMake Error: "add_subdirectory not given a binary directory"
CMake Error: "add_subdirectory not given a binary directory"
我正在尝试将 Google Test 集成到更大项目的子项目中,但找不到令我满意的解决方案。
我有两个约束:
- Google 测试的源代码已经在项目结构中的某处(因此使用 URL 从 git 存储库下载它不是一个选项)
- Google 测试的源代码不是我的子项目的子目录(永远不会)
所以当我尝试做这样的事情时:
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)
当您将 relative 路径(作为 source 目录)提供给 add_subdirectory
调用时,CMake 会自动使用相同的 relative build 目录的路径。
但是在 绝对源 路径的情况下(并且当此路径不在您的源树中时),CMake 无法猜测 build目录,您需要明确地:
提供它
另请参阅 documentation 以获取 add_subdirectory
命令。
我觉得有义务对此发表评论,因为当我用谷歌搜索这个错误时,这是最热门的搜索结果。
对我来说,我显然是个白痴:我修改了项目 src
目录中的 CMakeLists.txt
文件,但我没有意识到该文件已被锁定并且 VS Code即使我按下 Ctrl+S,实际上也没有保存。检查 VS Code 中的文件选项卡,看看那里是否有一个白点,表示文件未保存。按 Ctrl+S 并查看右下角是否弹出提示您以超级用户身份重试。
我仍然有错误,但它们是对我的项目有意义的新错误。
我正在尝试将 Google Test 集成到更大项目的子项目中,但找不到令我满意的解决方案。
我有两个约束:
- Google 测试的源代码已经在项目结构中的某处(因此使用 URL 从 git 存储库下载它不是一个选项)
- Google 测试的源代码不是我的子项目的子目录(永远不会)
所以当我尝试做这样的事情时:
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)
当您将 relative 路径(作为 source 目录)提供给 add_subdirectory
调用时,CMake 会自动使用相同的 relative build 目录的路径。
但是在 绝对源 路径的情况下(并且当此路径不在您的源树中时),CMake 无法猜测 build目录,您需要明确地:
提供它另请参阅 documentation 以获取 add_subdirectory
命令。
我觉得有义务对此发表评论,因为当我用谷歌搜索这个错误时,这是最热门的搜索结果。
对我来说,我显然是个白痴:我修改了项目 src
目录中的 CMakeLists.txt
文件,但我没有意识到该文件已被锁定并且 VS Code即使我按下 Ctrl+S,实际上也没有保存。检查 VS Code 中的文件选项卡,看看那里是否有一个白点,表示文件未保存。按 Ctrl+S 并查看右下角是否弹出提示您以超级用户身份重试。
我仍然有错误,但它们是对我的项目有意义的新错误。