Google Test - неопределенная ссылка на тестирование :: internal :: AssertHelper :: AssertHelper - PullRequest
0 голосов
/ 18 апреля 2020

Я построил 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 ++.

Ответы [ 2 ]

1 голос
/ 18 апреля 2020

Вы можете включить сборку библиотеки GTest (из исходного кода) непосредственно в вашу CMake конфигурацию, как это предусмотрено официальной документацией Google: https://github.com/google/googletest/tree/master/googletest#incorporating -into-существующий-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 после сборки.

0 голосов
/ 18 апреля 2020

Я использую Eclipse в качестве IDE. Я просто забыл указать конструктор в настройках проекта, поэтому Eclipse попытался построить проект с помощью g++.

. Это помогло:

enter image description here

...