CMake 错误:“add_subdirectory 未给出二进制目录"

2023-08-27C/C++开发问题
6

本文介绍了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 命令.

这篇关于CMake 错误:“add_subdirectory 未给出二进制目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6