本文介绍了CMake错误:“未给add_subdirectory二进制目录”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google Test集成到较大项目的子项目中,但找不到适合我的解决方案。


我有两个约束:



  • Google Test的源代码已经在项目结构中(因此,不能使用URL从git存储库中下载它)

  • Google Test的源代码不是我的子项目的子目录(并且永远不会)


因此,当我尝试执行以下操作时:

  add_subdirectory($ {GOOGLETEST_PROJECT_LOCATION})

我收到了:

  CMake在unit_tests / CMakeLists.txt:10(add_subdirectory)处出错:
add_subdirectory没有给出二进制目录,但是给定的源
目录 $ {GOOGLETEST_PROJECT_LOCATION}不是
$ {UNIT_TEST_DIRECTORY}的子目录。当
指定树外源时,必须显式指定二进制目录

另一方面,也许 ExternalProject_Add 可能是一个解决方案,但我当我根本不想下载源代码并从项目中的特定位置使用源代码时,我不知道该怎么用。


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

  3rdparty 
| --googletest
...
子项目
| --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


解决方案

错误消息明确-您还应为构建目录指定谷歌测试。构建)

当您给出相对路径时(作为目录)调用 add_subdirectory ,CMake自动为 build 目录使用相同的相对路径。



但是如果使用绝对源路径(并且该路径不在您的源树中),则CMake无法猜测 build 目录,您需要明确地提供:



另请参见用于 add_subdirectory 命令。


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.

I have two constraints:

  • the source code of Google Test is already somewhere in the project structure (thus using URL to download it from git repository is not an option)
  • the source code of Google Test is not a subdirectory of my subproject (and never will)

So when I tried to do something like this:

add_subdirectory( ${GOOGLETEST_PROJECT_LOCATION})

I received:

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.

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
解决方案

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)

When you give relative path (as a source directory) to add_subdirectory call, CMake automatically uses the same relative path for the build directory.

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 explicitely:

See also documentation for add_subdirectory command.

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

09-01 23:45