CMake错误。"add_subdirectory没有给出一个二进制目录"

19 人关注

我试图将谷歌测试整合到更大的项目的子项目中,但我无法找到令我满意的解决方案。

我有两个限制。

  • 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.
    

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

    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
        
    c++
    build
    cmake
    googletest
    Filip
    Filip
    发布于 2018-05-18
    2 个回答
    Tsyvarev
    Tsyvarev
    发布于 2022-01-14
    已采纳
    0 人赞同

    错误信息很清楚--你还应该指定 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 相对路径(作为一个source directory) to add_subdirectory call, CMake automatically uses the same 相对 path for the build directory.

    但如果是在绝对来源路径(当这个路径不在你的源代码树中时),CMake无法猜到build目录,你需要提供它explicitly: