Я построил Google Test из источника на Windows 10. Теперь я пытаюсь создать приложение, которое использует библиотеку. Сборка программы с CMake по какой-то причине не удалась.
EDIT // Кажется, что CMake неправильно связывает Google Test. Я получаю те же результаты, когда удаляю строку target_link_libraries(tests GTest::GTest)
из папки тестов CMakeLists.txt.
Вот мой код (программа вызывает ошибку при вызове 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 ++.