我有一个使用 CMake 作为构建系统的 C++ 项目,我想使用
cucumber-cpp
。在这个项目中,我有一个 GTest/ GMock 依赖项,我按照 here 的说明解决该依赖项(FetchContent_Declare
和 FetchContent_MakeAvailable
的混合)。
然后我尝试使用以下内容添加
cucumber-cpp
依赖项:
ExternalProject_Add(
CucumberCpp
GIT_REPOSITORY "https://github.com/cucumber/cucumber-cpp.git"
GIT_TAG v0.7
CMAKE_ARGS
-DCUKE_ENABLE_GTEST=ON -DCUKE_TESTS_UNIT=OFF
-DCUKE_ENABLE_EXAMPLES=OFF -DCUKE_ENABLE_BOOST_TEST=OFF -DCUKE_ENABLE_QT=OFF
-DCUKE_TESTS_VALGRIND=OFF)
但是,我可以看到CMake在配置时抱怨找不到GTest
cucumber-cpp
:
[1/9] Creating directories for 'googletest-populate'
[1/9] Performing download step (git clone) for 'googletest-populate'
Cloning into 'googletest-src'...
HEAD is now at f8d7d77c Bump version to v1.14 in preparation for release
[2/9] Performing disconnected update step for 'googletest-populate'
[3/9] No patch_disconnected step for 'googletest-populate'
[5/9] No configure step for 'googletest-populate'
[6/9] No build step for 'googletest-populate'
[7/9] No install step for 'googletest-populate'
[8/9] No test step for 'googletest-populate'
[9/9] Completed 'googletest-populate'
...
[6/16] Performing configure step for 'CucumberCpp'
...
-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY) (Required is at least version "1.11.0")
当我继续使用
cucumber-cpp
构建源代码时,我遇到各种链接器失败,因为无法找到对 GTestStep
的引用。从cucumber-cpp
CMakeLists.txt
来看,似乎以下是有问题的:
if(TARGET GTest::gtest)
list(APPEND CUKE_EXTRA_PRIVATE_LIBRARIES GTest::gtest)
list(APPEND CUKE_SOURCES drivers/GTestDriver.cpp)
endif()
如果我只是
apt install libgtest-dev libgmock-dev
然后重建,我似乎能够解决这个问题,但我想继续在我的项目中从源代码构建GTest/GMock。不确定我是否在这里做了一些愚蠢的事情。
自从我参与 Cucumber-CPP 或 Cucumber 以来已经有很长的时间了,从那以后我就没有编写过任何 C++ 代码,所以这些主要是观察。
CUKE_ENABLE_GTEST
驱动了CMake中所需的GoogleTest库:https://github.com/cucumber/cucumber-cpp/blob/f468ce65f68e1560d22b4bc14e5e489b2651776e/CMakeLists.txt#L146C5-L146C17GTest::gtest
目标而不是CUKE_ENABLE_GTEST
:https://github.com/cucumber/cucumber-cpp/blob/f468ce65f68e1560d22b4bc14e5e489b2651776e/src/CMakeLists.txt#L25我想你没有单独从源代码编译GTest,或者你应该能够定义
GTEST_ROOT
(我相信)......因为这就是我过去构建它的方式。我相信您是在同一个 CMake 项目中构建 GTest。也许你可以探索这个...
-DCUKE_ENABLE_GTEST=OFF
导入 Cucumber-CPP(这样它就不会尝试通过 CMake 导入 required 库)GTest::gtest
目标,则将编译 GTestDriver 并链接 GTest 库。祝你好运!