CMake:将 GTest 添加到构建中
CMake: Adding GTest to Build
我正在尝试使用 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 .
到目前为止,我能够 link 我所有的源文件到我想要的 dll 中,我相当有信心我知道如何 link 它与测试 exe我想使用。我遇到的主要问题是使用 FetchContent
模块将 testgtest.cpp
构建到可执行文件中,并使用 googletest link 构建它。它一直无法构建,而失败的具体原因根本不明显。
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) link使用 googletest 测试我的可执行文件。
test/testgtest.cpp
#include "gtest/gtest.h"
TEST(sample_test_case, sample_test)
{
EXPECT_EQ(1, 1);
}
我在 testgtest.cpp
中具体应该 #include
什么并不明显,但我在我看到的例子中看到了这一点,所以我认为这是一些迟钝的、神秘的魔法,只是应该 工作。
这是我所看到的输出:
$ 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?
感谢阅读
你的问题似乎不是 cmake 本身,而是你似乎 运行 变成了 warning getting treated as an error (instructions for Visual Studio. Could be different on another platform). You could turn off this setting,但显然这并不总是可能的,甚至可能不是一个好的解决方案你的情况。
至于警告本身,它似乎是 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)。
无论如何,另一种可能的解决方法是 如警告所述:
#define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING 1
但同样,这只是一种解决方法。它最终仍然需要修复,但这显然取决于他们来修复它。
这就是我解决问题的方法。
感谢 Chipster 对该问题进行了更多研究并最终将我推向了正确的方向。
由于问题似乎源于 release-1.8.0,其中包含一些未合并到其他版本中的错误,我认为必须有一个更新的 googletest 版本,我可以在我的 CMake 文件中引用它这将解决这个问题。
你看,只是将其从 release-1.8.0 更改为 release-1.10.0(在撰写本文时最新版本)为我解决了这个特定问题:
test/CMakeLists.txt
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
)
对于那些想知道删除以下行是否适用于此解决方案和方案以便您可以构建 googlemock 和 googletest 的人:
test/CMakeLists.txt
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(BUILD_GTEST ON CACHE BOOL "" FORCE)
我也对此进行了测试,并且能够在删除这些行的情况下构建我的测试运行器。
我正在尝试使用 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 .
到目前为止,我能够 link 我所有的源文件到我想要的 dll 中,我相当有信心我知道如何 link 它与测试 exe我想使用。我遇到的主要问题是使用 FetchContent
模块将 testgtest.cpp
构建到可执行文件中,并使用 googletest link 构建它。它一直无法构建,而失败的具体原因根本不明显。
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) link使用 googletest 测试我的可执行文件。
test/testgtest.cpp
#include "gtest/gtest.h"
TEST(sample_test_case, sample_test)
{
EXPECT_EQ(1, 1);
}
我在 testgtest.cpp
中具体应该 #include
什么并不明显,但我在我看到的例子中看到了这一点,所以我认为这是一些迟钝的、神秘的魔法,只是应该 工作。
这是我所看到的输出:
$ 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?
感谢阅读
你的问题似乎不是 cmake 本身,而是你似乎 运行 变成了 warning getting treated as an error (instructions for Visual Studio. Could be different on another platform). You could turn off this setting,但显然这并不总是可能的,甚至可能不是一个好的解决方案你的情况。
至于警告本身,它似乎是 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)。
无论如何,另一种可能的解决方法是
#define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING 1
但同样,这只是一种解决方法。它最终仍然需要修复,但这显然取决于他们来修复它。
这就是我解决问题的方法。
感谢 Chipster 对该问题进行了更多研究并最终将我推向了正确的方向。
由于问题似乎源于 release-1.8.0,其中包含一些未合并到其他版本中的错误,我认为必须有一个更新的 googletest 版本,我可以在我的 CMake 文件中引用它这将解决这个问题。
你看,只是将其从 release-1.8.0 更改为 release-1.10.0(在撰写本文时最新版本)为我解决了这个特定问题:
test/CMakeLists.txt
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
)
对于那些想知道删除以下行是否适用于此解决方案和方案以便您可以构建 googlemock 和 googletest 的人:
test/CMakeLists.txt
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(BUILD_GTEST ON CACHE BOOL "" FORCE)
我也对此进行了测试,并且能够在删除这些行的情况下构建我的测试运行器。