【问题标题】:Why does "#include <gtest/gtest.h>" fail even after having used "FetchContent_MakeAvailable" in my CMake file?为什么即使在我的 CMake 文件中使用了“FetchContent_MakeAvailable”之后,“#include <gtest/gtest.h>”也会失败?
【发布时间】:2021-08-21 05:07:13
【问题描述】:

这是我第一次在 C++ 中尝试 Google Test API,也是我第一次使用 CMake 的经历。如果有用,我正在使用 CLion。如下所示,我的 CMake 文件表面上应该允许 #include &lt;gtest/gtest.h&gt; 的文件:

cmake_minimum_required(VERSION 3.19)
project(chord)

# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 11)

include(FetchContent)
FetchContent_Declare(
        googletest
        URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread")
add_executable(
    chord
    main.cpp
    src/node.h
    src/node.cpp
    src/log.h
    src/log.cpp
    src/sha1.h
    src/sha1.cpp
    src/messageParser.h
    src/messageParser.cpp
    src/fingerTable.h
    src/fingerTable.cpp
    src/key.h
    src/key.cpp)
include(GoogleTest)

enable_testing()

add_executable(
    unitTests
    test/fingerTableTests.cc
    test/keyTests.cc
    test/logTests.cc
    test/messageParserTests.cc
    test/nodeTests.cc
    test/messageParserTests.cc
)
target_link_libraries(
    unitTests
    gtest_main
)

include(GoogleTest)
gtest_discover_tests(unitTests)

但是,当我从 main.cpp 尝试 #include &lt;gtest/gtest.h&gt; 时,我收到错误 gtest/gtest.h: No such file or directory。有人可以查明我的 CMake 文件中的错误吗?感谢并为您带来的麻烦感到抱歉。

【问题讨论】:

  • 你有没有通过这个?

标签: c++ cmake googletest clion


【解决方案1】:

尝试也链接gtest:

target_link_libraries(
    unitTests
    gtest_main
    gtest
)

【讨论】:

    猜你喜欢
    • 2013-09-05
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2017-10-20
    • 2014-11-06
    • 1970-01-01
    相关资源
    最近更新 更多