简体   繁体   English

CMake:将 GTest 添加到构建

[英]CMake: Adding GTest to Build

I'm trying to add googletest to my build using the FetchContent module, but I've come across many partial answers and have been unable to get where I want.我正在尝试使用FetchContent模块将 googletest 添加到我的构建中,但是我遇到了许多部分答案并且无法到达我想要的位置。 There seems to be an excess of dated examples that use the ExternalProject module, but very few helpful examples that use the FetchContent module.使用ExternalProject模块的过时示例似乎过多,但使用FetchContent模块的有用示例却很少。

Here's an incomplete list of references I've gone through today:这是我今天浏览的参考文献的不完整列表:

Here's the file structure that I want to use:这是我要使用的文件结构:

-root
    -build
        -googletest-build
        -googletest-src
        -googletest-test
        -src
        -test
    -src
        -CMakeLists.txt
        -AllOfMySourceFiles
    -test
        -CMakeLists.txt
        -testgtest.cpp
    -CMakeLists.txt

These are the commands that I'm trying to use to build:这些是我试图用来构建的命令:

cmake ..
cmake --build .

So far, I'm able to link all of my source files into the dll that I want and I'm fairly confident that I know how to link it with the test exe that I want to use.到目前为止,我能够将我的所有源文件链接到我想要的 dll 中,并且我相当有信心知道如何将它与我想要使用的测试 exe 链接。 The main issue I'm having is using the FetchContent module to build testgtest.cpp into an executable and link it with googletest.我遇到的主要问题是使用FetchContent模块将testgtest.cpp构建为可执行文件并将其与 googletest 链接。 It keeps failing to build and the specific reason why it's failing is not obvious at all.它一直无法构建,它失败的具体原因根本不明显。

root/CMakeLists.txt根/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})

src/CMakeLists.txt 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})

This one is working as I intend it to so far.到目前为止,这个正在按照我的意图工作。

test/CMakeLists.txt测试/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)

I'm unclear on what I should be doing specifically here for A) using the FetchContent module to add googletest to my build AND B) linking my test executable with googletest.我不清楚我应该在这里具体做什么 A)使用FetchContent模块将 googletest 添加到我的构建中,并且B)将我的测试可执行文件与 googletest 链接。

test/testgtest.cpp测试/testgtest.cpp

#include "gtest/gtest.h"

TEST(sample_test_case, sample_test)
{
    EXPECT_EQ(1, 1);
}

It's not obvious what I should be #include ing specifically in testgtest.cpp , but I saw that in the examples I've seen so I figured that this is some obtuse, arcane magic that's just supposed to work.#include中我应该具体testgtest.cpp什么并不明显,但我在我见过的示例中看到了这一点,所以我认为这是一些应该起作用的钝的、神秘的魔法。

Here's an output of what I've been seeing:这是我所看到的 output:

$ 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]

The warning C4996 will then continue to just repeat ad nauseam until it finishes. warning C4996将继续重复,直到结束。

I do want to fetch googletest from git, so what do I need to be doing differently here to successfully build and use googletest with my tests?我确实想从 git 获取 googletest,那么我需要在这里做些什么不同的事情才能成功构建和使用 googletest 与我的测试?

Thanks for reading谢谢阅读

This is what solved the issue for me.这就是为我解决问题的原因。

Thanks to Chipster for doing more research into the issue and ultimately pushing me into the right direction.感谢Chipster对这个问题进行了更多的研究,并最终将我推向了正确的方向。

Since the issue seemed to stem from release-1.8.0 containing some bugs that weren't merged into some other release, I figured that there must be a more recent release of googletest that I could reference in my CMake file that would have this issue resolved.由于该问题似乎源于 release-1.8.0 包含一些未合并到其他版本中的错误,因此我认为必须有更新的 googletest 版本,我可以在我的 CMake 文件中引用该版本会出现此问题解决。

Lo and behold, only changing this to release-1.10.0 (latest at the time of writing this) from release-1.8.0 solved this specific issue for me:瞧,仅将其从 release-1.8.0 更改为 release-1.10.0(在撰写本文时最新)为我解决了这个特定问题:

test/CMakeLists.txt测试/CMakeLists.txt

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

For those of you wondering if removing the following lines will work with this solution and scenario so that you may build googlemock & googletest:对于那些想知道删除以下行是否适用于此解决方案和场景的人,以便您可以构建 googlemock 和 googletest:

test/CMakeLists.txt测试/CMakeLists.txt

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

I tested this out also and I was able to build my test runner with these lines removed.我也对此进行了测试,并且能够在删除这些行的情况下构建我的测试运行器。

Your problem seems to not be with cmake itself, but rather you seem to be running into a warning getting treated as an error (instructions for Visual Studio. Could be different on another platform).您的问题似乎与 cmake 本身无关,而是您似乎遇到了被视为错误的警告(Visual Studio 的说明。在另一个平台上可能会有所不同)。 You could turn off this setting , but obviously that's not always possible, and might not even be a good solution for your situation.您可以关闭此设置,但显然这并不总是可行的,甚至可能不是适合您的情况的好解决方案。

As to the warning itself, it appears to be known , and even fixed, but possibly never merged properly (though the thread may point to them trying to merge it, but the error still seems to remain ).至于警告本身,它似乎是已知的,甚至是固定的,但可能从未正确合并(尽管线程可能指向他们试图合并它,但错误似乎仍然存在)。

Anyway, another possible workaround is to define the macro as the warning says:无论如何,另一种可能的解决方法是按照警告说明定义宏

 #define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING 1

But again, that's just a workaround.但同样,这只是一种解决方法。 It still ultimately needs to get fixed, but that's obviously up to them to fix it.它最终仍然需要修复,但这显然取决于他们来修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM