【发布时间】:2019-11-03 06:10:30
【问题描述】:
我正在尝试使用 FetchContent 模块将 googletest 添加到我的构建中,但我遇到了许多部分答案并且无法到达我想要的位置。使用ExternalProject 模块的过时示例似乎过多,但使用FetchContent 模块的有用示例很少。
这是我今天阅读的参考资料的不完整列表:
- https://crascit.com/2015/07/25/cmake-gtest/
- https://github.com/bast/gtest-demo
- https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project
- https://cmake.org/cmake/help/latest/module/FetchContent.html#declaring-content-details
- How to start working with GTest and CMake
这是我要使用的文件结构:
-root
-build
-googletest-build
-googletest-src
-googletest-test
-src
-test
-src
-CMakeLists.txt
-AllOfMySourceFiles
-test
-CMakeLists.txt
-testgtest.cpp
-CMakeLists.txt
这些是我试图用来构建的命令:
cmake ..
cmake --build .
到目前为止,我能够将所有源文件链接到我想要的 dll 中,并且我相当有信心知道如何将它与我想要使用的测试 exe 链接。我遇到的主要问题是使用FetchContent 模块将testgtest.cpp 构建为可执行文件并将其与googletest 链接。它一直无法构建,并且它失败的具体原因根本不明显。
root/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
# 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})
到目前为止,这个功能正在按照我的预期工作。
test/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)
我不清楚我应该在这里具体做什么 A) 使用 FetchContent 模块将 googletest 添加到我的构建中AND B) 将我的测试可执行文件与 googletest 链接。
test/testgtest.cpp
#include "gtest/gtest.h"
TEST(sample_test_case, sample_test)
{
EXPECT_EQ(1, 1);
}
在testgtest.cpp 中我具体应该是什么#include 并不明显,但我在我看到的示例中看到了这一点,所以我认为这是一些迟钝、神秘的魔法,只是 假设 em> 去工作。
这是我所看到的输出:
$ 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]
warning C4996 将继续重复,令人作呕,直到结束。
我确实想从 git 获取 googletest,那么我需要在这里做些什么不同的事情才能成功构建和使用 googletest 来进行我的测试?
感谢阅读
【问题讨论】:
-
this 能解决您的问题吗?另外,more discussion 解决这个问题,even more。长话短说:它似乎之前已修复,但从未合并。
-
所以它是固定的,你只是有错误的版本?无论如何,很高兴它对你有用。你可能想把它放在一个答案中,但你不必这样做。
-
是的,最终解决问题的方式非常可笑。我不敢相信我昨天没想到!感谢您的帮助
标签: c++ cmake googletest