Reputation: 1784
I built Google Test from source on Windows 10. Now I'm trying to create an application that uses the library. Building the program with CMake fails for some reason.
EDIT // It seems that CMake is not linking Google Test properly. I get the same results when I remove the target_link_libraries(tests GTest::GTest)
line from the tests folder CMakeLists.txt.
Here's my code (the program fails at the call to 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();
Here's the tests subfolder CMakeLists.txt:
include(GoogleTest)
add_executable(tests CanTest.cpp)
target_link_libraries(tests GTest::GTest)
gtest_discover_tests(tests)
Here's the project 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()
Here are the error messages:
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)'
I'd appreciate any help, as I'm not a C++ expert.
Upvotes: 1
Views: 8321
Reputation: 9
I encountered this same issue using eclipse. Mine was due to not linking the google test libraries which can be found in the lib folder of google test build directory.
Upvotes: 1
Reputation: 718
You can incorporate the building of the GTest library (from source) directly into your CMake
configuration, as suggested by the official Google documentation: https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project.
In other words, create a new file called 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 ""
)
And then in your existing CMakeLists.txt
file, include this snippet:
# 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...
Placing your test code in example.cpp
should make them executable via example.exe
once built.
Upvotes: 2
Reputation: 1784
I use Eclipse as the IDE. I had simply forgotten to specify the builder in the project settings, so Eclipse tried to build the project with g++
.
This helped:
Upvotes: 0