CMake:添加 GTest 进行构建

jcr*_*izk 6 c++ cmake googletest

我正在尝试使用该FetchContent模块将 googletest 添加到我的构建中,但我遇到了许多部分答案,并且无法到达我想要的位置。使用该模块的过时示例似乎过多ExternalProject,但使用该模块的有用示例却很少FetchContent

这是我今天浏览过的不完整的参考文献列表:

这是我要使用的文件结构:

-root
    -build
        -googletest-build
        -googletest-src
        -googletest-test
        -src
        -test
    -src
        -CMakeLists.txt
        -AllOfMySourceFiles
    -test
        -CMakeLists.txt
        -testgtest.cpp
    -CMakeLists.txt
Run Code Online (Sandbox Code Playgroud)

这些是我尝试用来构建的命令:

cmake ..
cmake --build .
Run Code Online (Sandbox Code Playgroud)

到目前为止,我能够将所有源文件链接到我想要的 dll 中,并且我相当有信心知道如何将它与我想要使用的测试 exe 链接。我遇到的主要问题是使用该FetchContent模块构建testgtest.cpp可执行文件并将其与 googletest 链接。它一直无法构建,而且失败的具体原因根本不明显。

根/CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_FLAGS_RELEASE "/MT")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
set(CMAKE_CXX_STANDARD 11)

set(LIBNAME "WTP")
set(TESTNAME "TestRunner")

set(LIB_MAJOR_VERS "0")
set(LIB_MINOR_VERS "0")
set(LIB_PATCH_VERS "0")

#build source
project(${LIBNAME} 
    VERSION  ${LIB_MAJOR_VERS}.${LIB_MINOR_VERS}.${LIB_PATCH_VERS})
add_subdirectory(src)

#build tests
project(${TESTNAME})

add_subdirectory(test)
enable_testing()
add_test(${TESTNAME} ${TESTNAME})
Run Code Online (Sandbox Code Playgroud)

src/CMakeLists.txt

# Build output setup
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/src/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/src/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/src/bin)

#get all of the source files
set(SRCS Export.c
    Regions/ReferenceConstants.h
    Regions/Region1.h
    Regions/Region2.h
    Regions/Region3.h
    Regions/Boundaries/RegionBoundaries/R2andR3.h
    Regions/Boundaries/SubregionBoundaries/Region3/Boundaries_vTP.h)

add_library(${LIBNAME} SHARED ${SRCS})
Run Code Online (Sandbox Code Playgroud)

到目前为止,这个正在按我的预期工作。

测试/CMakeLists.txt

# Build output setup
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test/bin)

################################
# GTest
################################
project(googletest-git NONE)

include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG        release-1.8.0
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(BUILD_GTEST ON CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(googletest)

################################
# Tests
################################
# Add test cpp file
add_executable(${TESTNAME} testgtest.cpp)
# Link test executable against gtest & gtest_main
target_link_libraries(${TESTNAME} gtest gtest_main)
Run Code Online (Sandbox Code Playgroud)

我不清楚我应该在这里具体做什么 A)使用FetchContent模块将 googletest 添加到我的构建中B)将我的测试可执行文件与 googletest 链接。

测试/testgtest.cpp

#include "gtest/gtest.h"

TEST(sample_test_case, sample_test)
{
    EXPECT_EQ(1, 1);
}
Run Code Online (Sandbox Code Playgroud)

目前还不清楚我应该#include具体在 中做什么testgtest.cpp,但我在我见过的示例中看到了这一点,所以我认为这是一些迟钝的、神秘的魔法,只是应该起作用。

这是我所看到的输出:

$ cmake --build .
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  gtest-all.cc
D:\root\build\_deps\googletest-src\googletest\include\gtest/internal/gtest-port.h(996,34): error C2220:  warning treated as error - no 'object' file generated [D:\root\build\_deps\googletest-build\googletest\gtest.vcxproj]
D:\root\build\_deps\googletest-src\googletest\include\gtest/internal/gtest-port.h(996,34): warning C4996:  'std::tr1': warning STL4002: The non-Standard std::tr1 namespace and TR1-only machinery are deprecated and will be REMOVED. You can define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING to acknowledge that you have received this warning. [D:\root\build\_deps\googletest-build\googletest\gtest.vcxproj]
Run Code Online (Sandbox Code Playgroud)

然后将warning C4996继续令人作呕地重复直到完成。

我确实想从 git 获取 googletest,那么我需要在这里做些什么不同的事情才能在我的测试中成功构建和使用 googletest 呢?

谢谢阅读

jcr*_*izk 6

这就是为我解决了这个问题的原因。

感谢Chipster对这个问题进行了更多的研究,并最终将我推向了正确的方向。

由于该问题似乎源于release-1.8.0,其中包含一些未合并到其他版本中的错误,因此我认为必须有一个更新版本的googletest,我可以在我的CMake文件中引用该版本,该版本会出现此问题解决。

你瞧,只有将其从release-1.8.0更改为release-1.10.0(撰写本文时最新)才为我解决了这个特定问题:

测试/CMakeLists.txt

FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG        release-1.10.0
)
Run Code Online (Sandbox Code Playgroud)

对于那些想知道删除以下行是否适用于此解决方案和场景的人,以便您可以构建 googlemock 和 googletest:

测试/CMakeLists.txt

set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(BUILD_GTEST ON CACHE BOOL "" FORCE)
Run Code Online (Sandbox Code Playgroud)

我也对此进行了测试,并且我能够在删除这些行的情况下构建我的测试运行程序。