CMake Error: quot;add_subdirectory not given a binary directoryquot;(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 未给出二进制目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CMake 错误:“add_subdirectory 未给出二进制目录"
				
        
 
            
        基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
 - 常量变量在标题中不起作用 2021-01-01
 - 我有静态或动态 boost 库吗? 2021-01-01
 - 如何通过C程序打开命令提示符Cmd 2022-12-09
 - C++结构和函数声明。为什么它不能编译? 2022-11-07
 - 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
 - 如何在 C++ 中初始化静态常量成员? 2022-01-01
 - 如何检查GTK+3.0中的小部件类型? 2022-11-30
 - 这个宏可以转换成函数吗? 2022-01-01
 - 在 C++ 中计算滚动/移动平均值 2021-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				