CMake 错误:“add_subdirectory 未给出二进制目录"
我正在尝试将 Google Test 集成到更大项目的子项目中,但找不到令我满意的解决方案.
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.
我有两个限制:
- Google Test 的源代码已经存在于项目结构中(因此不能使用 URL 从 git 存储库下载它)
- Google Test 的源代码不是我的子项目的子目录(并且永远不会)
所以当我尝试做这样的事情时:
So when I tried to do something like this:
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
可能是一个解决方案,但当我根本不想下载源代码并使用项目中特定位置的源代码时,我不知道该如何使用它.
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
推荐答案
错误信息很明确 - 您还应该为 googletest 指定构建目录.
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)
当您将 relative 路径(作为 source 目录)提供给 add_subdirectory
调用时,CMake 会自动为 使用相同的相对路径>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.
但是在绝对源路径的情况下(当这个路径不在你的源代码树中时),CMake无法猜测build目录,你需要提供它明确:
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:
另见文档,了解add_subdirectory
命令.
相关文章