Google 测试 - 对 testing::internal::AssertHelper::AssertHelper 的未定义引用
Google Test - undefined reference to testing::internal::AssertHelper::AssertHelper
我在 Windows 10 上从源代码构建了 Google 测试。现在我正在尝试创建一个使用该库的应用程序。由于某种原因,使用 CMake 构建程序失败。
编辑 // 看来 CMake 没有正确链接 Google 测试。当我从测试文件夹 CMakeLists.txt.
中删除 target_link_libraries(tests GTest::GTest)
行时,我得到了相同的结果
这是我的代码(程序在调用 EXPECT_EQ
时失败):
#include "gtest/gtest.h"
namespace {
class CanTest: public ::testing::Test {};
TEST_F(CanTest, CanReallyTest) {
EXPECT_EQ(0, 0);
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
这是测试子文件夹 CMakeLists.txt:
include(GoogleTest)
add_executable(tests CanTest.cpp)
target_link_libraries(tests GTest::GTest)
gtest_discover_tests(tests)
这是项目 CMakeLists.txt:
cmake_minimum_required(VERSION 3.10.0)
project(CxxTemplate
VERSION 0.1.0.0
LANGUAGES C CXX
DESCRIPTION "A project containing the essentials for other projects"
)
set(CxxTemplate_VENDOR "Aron Rubin")
set(PROJECT_VENDOR "${CxxTemplate_VENDOR}")
set(CMAKE_DEBUG_POSTFIX d)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()
find_package(GTest)
if(GTEST_FOUND)
set(Gtest_FOUND TRUE)
endif()
if(GTest_FOUND)
include(GoogleTest)
endif()
enable_testing()
add_subdirectory(tests)
include(CheckIncludeFiles)
CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H)
find_package(Threads REQUIRED)
add_executable(CxxTemplate
src/CxxTemplate.cpp
)
target_link_libraries(CxxTemplate
PUBLIC Threads::Threads
)
include(FeatureSummary)
feature_summary(WHAT ALL)
# include(DumpProps)
# print_target_properties(diva)
# include(DumpVariables)
# dump_all_variables()
错误信息如下:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:18: multiple definition of `main'; src\CxxTemplate.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../src/CxxTemplate.cpp:32: first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:11: undefined reference to `testing::Test::Test()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:11: undefined reference to `testing::Test::~Test()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::Message::Message()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `TestBody':
C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:19: undefined reference to `testing::InitGoogleTest(int*, char**)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:516: undefined reference to `testing::internal::IsTrue(bool)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `GetSetUpCaseOrSuite':
C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:516: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:516: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:516: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:531: undefined reference to `testing::internal::IsTrue(bool)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `GetTearDownCaseOrSuite':
C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:531: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:531: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:531: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:13: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:CanTest.cpp:(.rdata+0x210): undefined reference to `testing::Test::SetUp()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:CanTest.cpp:(.rdata+0x218): undefined reference to `testing::Test::TearDown()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:CanTest.cpp:(.rdata+0x278): undefined reference to `testing::Test::SetUp()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:CanTest.cpp:(.rdata+0x280): undefined reference to `testing::Test::TearDown()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `RUN_ALL_TESTS()':
C:/msys64/mingw64/include/gtest/gtest.h:2473: undefined reference to `testing::UnitTest::GetInstance()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/gtest.h:2473: undefined reference to `testing::UnitTest::Run()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
C:/msys64/mingw64/include/gtest/gtest.h:1528: undefined reference to `testing::AssertionSuccess()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
C:/msys64/mingw64/include/gtest/gtest.h:1511: undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
非常感谢任何帮助,因为我不是 C++ 专家。
我使用 Eclipse 作为 IDE。我只是忘记在项目设置中指定构建器,所以 Eclipse 尝试使用 g++
.
构建项目
这有帮助:
您可以按照官方 Google 文档的建议,将 GTest 库(从源代码)的构建直接合并到您的 CMake
配置中:https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project.
换句话说,创建一个名为 CMakeLists.txt.in
:
的新文件
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
然后在您现有的 CMakeLists.txt
文件中,包括以下代码段:
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(example example.cpp)
target_link_libraries(example gtest_main)
add_test(NAME example_test COMMAND example)
## Remainder of your project config...
将您的测试代码放在 example.cpp
中应该使它们在构建后通过 example.exe
可执行。
我在使用 Eclipse 时遇到了同样的问题。我的是由于没有链接 google 测试库,它可以在 google 测试构建目录的 lib 文件夹中找到。
Image Description
我在 Windows 10 上从源代码构建了 Google 测试。现在我正在尝试创建一个使用该库的应用程序。由于某种原因,使用 CMake 构建程序失败。
编辑 // 看来 CMake 没有正确链接 Google 测试。当我从测试文件夹 CMakeLists.txt.
中删除target_link_libraries(tests GTest::GTest)
行时,我得到了相同的结果
这是我的代码(程序在调用 EXPECT_EQ
时失败):
#include "gtest/gtest.h"
namespace {
class CanTest: public ::testing::Test {};
TEST_F(CanTest, CanReallyTest) {
EXPECT_EQ(0, 0);
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
这是测试子文件夹 CMakeLists.txt:
include(GoogleTest)
add_executable(tests CanTest.cpp)
target_link_libraries(tests GTest::GTest)
gtest_discover_tests(tests)
这是项目 CMakeLists.txt:
cmake_minimum_required(VERSION 3.10.0)
project(CxxTemplate
VERSION 0.1.0.0
LANGUAGES C CXX
DESCRIPTION "A project containing the essentials for other projects"
)
set(CxxTemplate_VENDOR "Aron Rubin")
set(PROJECT_VENDOR "${CxxTemplate_VENDOR}")
set(CMAKE_DEBUG_POSTFIX d)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()
find_package(GTest)
if(GTEST_FOUND)
set(Gtest_FOUND TRUE)
endif()
if(GTest_FOUND)
include(GoogleTest)
endif()
enable_testing()
add_subdirectory(tests)
include(CheckIncludeFiles)
CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H)
find_package(Threads REQUIRED)
add_executable(CxxTemplate
src/CxxTemplate.cpp
)
target_link_libraries(CxxTemplate
PUBLIC Threads::Threads
)
include(FeatureSummary)
feature_summary(WHAT ALL)
# include(DumpProps)
# print_target_properties(diva)
# include(DumpVariables)
# dump_all_variables()
错误信息如下:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:18: multiple definition of `main'; src\CxxTemplate.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../src/CxxTemplate.cpp:32: first defined here
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:11: undefined reference to `testing::Test::Test()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:11: undefined reference to `testing::Test::~Test()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::Message::Message()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `TestBody':
C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:14: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:19: undefined reference to `testing::InitGoogleTest(int*, char**)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:516: undefined reference to `testing::internal::IsTrue(bool)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `GetSetUpCaseOrSuite':
C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:516: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:516: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:516: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:531: undefined reference to `testing::internal::IsTrue(bool)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `GetTearDownCaseOrSuite':
C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:531: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:531: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/internal/gtest-internal.h:531: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:C:\Users\mkkek\Documents\koodi\cppkirja\Advanced-CPlusPlus\Lesson1\Exercise01\project\CxxTemplate\Debug/../tests/CanTest.cpp:13: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:CanTest.cpp:(.rdata+0x210): undefined reference to `testing::Test::SetUp()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:CanTest.cpp:(.rdata+0x218): undefined reference to `testing::Test::TearDown()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:CanTest.cpp:(.rdata+0x278): undefined reference to `testing::Test::SetUp()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o:CanTest.cpp:(.rdata+0x280): undefined reference to `testing::Test::TearDown()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `RUN_ALL_TESTS()':
C:/msys64/mingw64/include/gtest/gtest.h:2473: undefined reference to `testing::UnitTest::GetInstance()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/gtest/gtest.h:2473: undefined reference to `testing::UnitTest::Run()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
C:/msys64/mingw64/include/gtest/gtest.h:1528: undefined reference to `testing::AssertionSuccess()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: tests\CanTest.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
C:/msys64/mingw64/include/gtest/gtest.h:1511: undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
非常感谢任何帮助,因为我不是 C++ 专家。
我使用 Eclipse 作为 IDE。我只是忘记在项目设置中指定构建器,所以 Eclipse 尝试使用 g++
.
这有帮助:
您可以按照官方 Google 文档的建议,将 GTest 库(从源代码)的构建直接合并到您的 CMake
配置中:https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project.
换句话说,创建一个名为 CMakeLists.txt.in
:
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
然后在您现有的 CMakeLists.txt
文件中,包括以下代码段:
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(example example.cpp)
target_link_libraries(example gtest_main)
add_test(NAME example_test COMMAND example)
## Remainder of your project config...
将您的测试代码放在 example.cpp
中应该使它们在构建后通过 example.exe
可执行。
我在使用 Eclipse 时遇到了同样的问题。我的是由于没有链接 google 测试库,它可以在 google 测试构建目录的 lib 文件夹中找到。
Image Description