building Gtest framework in LINUX - c++

I have downloaded the GTest sourcecode googletest-release-1.8.0.tar.gz (from : github ) and extracted it. (working on CentOS)
After that I ran following commands:
cd googletest-release-1.8.0\googletest.
cmake CMakeLists.txt.
Output:
-- Configuring done
-- Generating done
-- Build files have been written to: gtestframework/googletest-release-1.8.0/googletest
make.
Output:
[ 50%] Built target gtest
[100%] Built target gtest_main
After this I copied the file libgtest_main.a and libgtest.a into /usr/lib.
cd googletest-release-1.8.0\googlemock\gtest.
Copied libgtest_main.so libgtest.so libGTest.so in /usr/lib folder.
cd gtestframework\Testcode //{2 C++ files inside Testcode}
cmake CMakeLists.txt.
make.
I got this error:
Linking CXX executable runTests
CMakeFiles/runTests.dir/tests.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
tests.cpp:(.text+0x95a): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
tests.cpp:(.text+0xa14): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, double>(char const*, char const*, int const&, double const&)':
tests.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIidEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIidEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x6c): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::string const&, std::string const&, bool)'
CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<double, double>(char const*, char const*, double const&, double const&)':
tests.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIddEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIddEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x6c): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::string const&, std::string const&, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [runTests] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2
CMakeLists.txt file content:
cmake_minimum_required(VERSION 2.6)
# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests tests.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

Related

GTest linker errors on Ubuntu 20.10

I have the following errors on Ubuntu 20.04:
[1/1] Linking CXX executable bin/project_2022_matrix_tests
FAILED: bin/project_2022_matrix_tests
: && /usr/bin/c++ -g CMakeFiles/project_2022_matrix_tests.dir/CException.cpp.o CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o CMakeFiles/project_2022_matrix_tests.dir/tests/main_unit_tests.cpp.o -o bin/project_2022_matrix_tests -L/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/lib -Wl,-rpath,/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/lib -lgtest_main -lgmock_main -lgmock -lgtest -lpthread -lpthread && :
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult::AppendMessage(testing::Message const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:357 : undefined reference to « testing::Message::GetString[abi:cxx11]() const »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<CException*, decltype(nullptr)>(char const*, char const*, CException* const&, decltype(nullptr) const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : 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) »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<char*, decltype(nullptr)>(char const*, char const*, char* const&, decltype(nullptr) const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : 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) »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : 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) »
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(project_2022_matrix_tests)
set(CMAKE_CXX_STANDARD 14)
if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
else()
message(WARNING "The file conanbuildinfo.cmake doesn't exist, you have to run conan install first")
endif()
find_package(GTest REQUIRED)
include_directories(.)
enable_testing()
add_executable(project_2022_matrix_tests
CException.cpp
CException.h
CMatrix.cpp
CMatrix.h
CIndexableMatrix.h
main.cpp)
add_executable(project_2022_matrix_tests_tests
CException.cpp
CException.h
tests/CException_unit_tests.cpp tests/main_unit_tests.cpp tests/CMatrix_unit_tests.cpp)
target_link_libraries(project_2022_matrix_tests_tests gtest)
I have to say this works on CLion on macOS but for some reasons, not on Ubuntu...
If you need/want more information, please ask.
Thanks for your future reply.
To use the solution that was posted as a comment, just edit the .conan/profiles/default and change the line compiler.libcxx to set the value to libstdc++11.

Cmake finds packages but is unable to link them

I'm trying to compile a CMake project that requires the import of two external libraries. Both libraries have their .so file in "/usr/lib" and "/usr/include". And I've created the Find.cmake file for both of them; here is an example (of FindECDSA.cmake):
find_path(ECDSA_INCLUDE_DIR NAMES ecdsa.h DOC "ECDSA-lib include directory")
find_library(ECDSA_LIBRARY NAMES ECDSA DOC "ECDSA-lib library")
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ECDSA
REQUIRED_VARS ECDSA_INCLUDE_DIR ECDSA_LIBRARY)
if(ECDSA_FOUND AND NOT TARGET ECDSA::ECDSA)
add_library(ECDSA::ECDSA UNKNOWN IMPORTED)
set_target_properties(ECDSA::ECDSA PROPERTIES
IMPORTED_LOCATION "${ECDSA_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${ECDSA_INCLUDE_DIR}")
endif()
mark_as_advanced(ECDSA_INCLUDE_DIR ECDSA_LIBRARY)
set(ECDSA_INCLUDE_DIRS ${ECDSA_INCLUDE_DIR})
set(ECDSA_LIBRARIES ${ECDSA_LIBRARY})
Then, at the CMakeLists.txt they are found and linked to the target the following way:
cmake_minimum_required(VERSION 3.12)
project(GEONET VERSION 0.1)
set(CMAKE_CXX_STANDARD 11)
# project variables
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set(CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/its")
find_package(ECDSA REQUIRED)
find_package(v2xSe REQUIRED)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
add_executable(geonet
application.cpp
...
)
target_link_libraries(geonet PUBLIC v2xSe::v2xSe ECDSA::ECDSA ...)
install(TARGETS geonet EXPORT ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
The thing is that the CMake finds the libraries with no problem, but on the linking stage of the compilation is completely unable to find the references. Here is the error:
CMakeFiles/geonet.dir/main.cpp.o: In function `main':
/src/src/main.cpp:181: undefined reference to `boost::log::v2s_mt_posix::core::set_filter(boost::log::v2s_mt_posix::filter const&)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::compute_hash_sha256(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/src/src/ahd_connection.cpp:210: undefined reference to `ecdsa_sha256(void const*, unsigned int, unsigned char*)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::sign_with_hsm(unsigned char*)':
/src/src/ahd_connection.cpp:225: undefined reference to `v2xSe_createRtSign(unsigned short, TypeHash_t*, unsigned short*, TypeSignature_t*)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::compute_hash_sha256(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/src/src/ahd_connection.cpp:210: undefined reference to `ecdsa_sha256(void const*, unsigned int, unsigned char*)'
/src/src/ahd_connection.cpp:210: undefined reference to `ecdsa_sha256(void const*, unsigned int, unsigned char*)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::verify_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/src/src/ahd_connection.cpp:310: undefined reference to `ecdsa_verify_signature(int, ecdsa_point_t, unsigned char*, ecdsa_sig_t, int, void (*)(void*, int, int), void*)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::AHDConnection(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int const&)':
/src/src/ahd_connection.cpp:61: undefined reference to `v2xSe_generateRtEccKeyPair(unsigned short, unsigned char, unsigned short*, TypePublicKey_t*)'
/src/src/ahd_connection.cpp:83: undefined reference to `ecdsa_open()'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::~AHDConnection()':
/src/src/ahd_connection.cpp:89: undefined reference to `ecdsa_close()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/geonet.dir/build.make:472: bin/geonet] Error 1
make[1]: *** [CMakeFiles/Makefile2:116: CMakeFiles/geonet.dir/all] Error 2
make: *** [Makefile:150: all] Error 2
What I did do wrong?
I've finally solved the issue:
It happens to be that some of the imported libraries were just C libraries, so they had to be imported using the following lines:
extern "C" {
#include "v2xSe.h"
#include "ecdsa.h"
}
I discovered that they were possible to be compiled using GCC but not g++.

Building a C++ project with CMake on OSx: ld: symbol(s) not found for architecture x86_64

I got the source code of an old C++ project using Xerces-C++ that I am trying to build on CLion with CMake. OSx version: Catalina.
I started by preparing the CMakeLists.txt that was not available on the old project.
I arrived at this build exception:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug --target all -- -j 4 VERBOSE=1
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -S/Users/miloscuculovic/CLionProjects/Test2 -B/Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_progress_start /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/CMakeFiles /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make -f src/CMakeFiles/Test2.dir/build.make src/CMakeFiles/Test2.dir/depend
cd /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug && /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_depends "Unix Makefiles" /Users/miloscuculovic/CLionProjects/Test2 /Users/miloscuculovic/CLionProjects/Test2/src /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/src /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/src/CMakeFiles/Test2.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make -f src/CMakeFiles/Test2.dir/build.make src/CMakeFiles/Test2.dir/build
[ 50%] Linking CXX executable Test2
cd /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/src && /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/Test2.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++ -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Test2.dir/ComputeDelta.cpp.o -o Test2 -framework CoreFoundation -framework CoreFoundation -framework IOKit /usr/lib/libobjc.dylib /usr/lib/libcurl.dylib
Undefined symbols for architecture x86_64:
"xercesc_3_2::XMLAttDefList::serialize(xercesc_3_2::XSerializeEngine&)", referenced from:
vtable for xercesc_3_2::XMLAttDefList in ComputeDelta.cpp.o
"NodesManager::PrintStats()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::FullBottomUp(int)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::topdownMatch(int, int)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::setUniqueIdHandler(UniqueIdHandler*)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::registerResultDocument(XID_DOMDocument*)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::registerSourceDocument(XID_DOMDocument*)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::Optimize(int)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::MatchById(int)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::NodesManager()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::~NodesManager()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"DeltaConstructor::getDeltaDocument()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"DeltaConstructor::constructDeltaDocument()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"DeltaConstructor::DeltaConstructor(NodesManager*, char const*, XID_DOMDocument*, char const*, XID_DOMDocument*, bool)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"xercesc_3_2::XMLAttDefList::getProtoType() const", referenced from:
vtable for xercesc_3_2::XMLAttDefList in ComputeDelta.cpp.o
"xercesc_3_2::XMLAttDefList::isSerializable() const", referenced from:
vtable for xercesc_3_2::XMLAttDefList in ComputeDelta.cpp.o
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src/Test2] Error 1
make[1]: *** [src/CMakeFiles/Test2.dir/all] Error 2
make: *** [all] Error 2
There are two CMakeLists.txt files, one in the root of the project and the 2nd one in src:
CMakeLists.txt at root:
cmake_minimum_required(VERSION 3.15)
project(Test2)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
CMakeLists.txt at src:
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(/usr/local/Cellar/xerces-c/3.2.2/include)
include_directories(/usr/local/Cellar/opencascade/7.3.0p3/include)
include_directories(/usr/local/Cellar/libuv/1.31.0/include)
add_executable(Test2 ComputeDelta.cpp)
set(STLINK_LIB_SHARED ${PROJECT_NAME})
find_library(ObjC objc)
find_library(Curl curl)
find_library(CoreServices CoreServices)
find_library(CoreFoundation CoreFoundation)
find_library(IOKit IOKit)
target_link_libraries(${STLINK_LIB_SHARED} ${CoreServices} ${CoreFoundation} ${IOKit} ${ObjC} ${Curl})
Any idea how to solve this? I already installed opencascade, libuv, libev.
Both libuv and xerces-c have a pkg-config (.pc) file, so you can reduce your CMakeLists.txt to the following. This makes use of the FindPkgConfig module that converts a pkg-config file to an IMPORTED target (See "It's time to do CMake right". This IMPORTED target will automatically know which headers to include and which libraries to link, you only need to use target_link_libraries to link with it.
include(FindPkgConfig)
pkg_check_modules(Xerces REQUIRED IMPORTED_TARGET xerces-c)
pkg_check_modules(LibUv REQUIRED IMPORTED_TARGET libuv)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(/usr/local/Cellar/opencascade/7.3.0p3/include)
add_library(libxyDelta STATIC
convertUTF.cpp
StringPusher.cpp
ComputeDelta.cpp
Diff_DeltaConstructor.pp
Diff_NodesManager.cpp
Diff_UniqueIdHandler.cpp
DeltaApply.cpp
DeltaException.cpp
DeltaManager.cpp
DeltaReverse.cpp
DeltaSortOperations.cpp
easy_css.cpp
lcss.cpp
lookup2.cpp
Tools.cpp
XID_map.cpp
XID_DOMDocument.cpp
XyDeltaFileImpl.cpp
XyDeltaDomImpl.cpp
XyInt.cpp
XyLatinStr.cpp
XyStr.cpp
XyStrDiff.cpp
XyStrDelta.cpp
XyUTF8Str.cpp)
set(STLINK_LIB_SHARED ${PROJECT_NAME})
find_library(ObjC objc)
find_library(Curl curl)
find_library(CoreServices CoreServices)
find_library(CoreFoundation CoreFoundation)
find_library(IOKit IOKit)
target_link_libraries(libxyDelta ${CoreServices} ${CoreFoundation} ${IOKit} ${ObjC} ${Curl})
target_link_libraries(libxyDelta PkgConfig::LibUv PkgConfig::Xerces)
add_executable(xydiff execComputeDelta.cpp)
target_link_libraries(xydiff libxyDelta)
add_executable(xydelta execDeltaApply.cpp)
target_link_libraries(xydelta libxyDelta)
I also changed your add_executable to add_library.
Together, this will eliminate any errors about xerces and main. That just leaves you with the errors about NodesManager and DeltaConstructor, but I think these are in a separate .cpp file you forgot to add to the target.
You can probably get rid of most of those find_libraries calls as well, but I cannot do that without knowing what your source files look like.

compiling gtest and gmock examples

I have a book, Modern C++ programming with Test-Driven Development and i want to build the examples from the book, im using google mock 1.6.0 and google test 1.6.0 and i get the following error when i try to build
please help!!
[ 14%] Linking CXX executable test
CMakeFiles/test.dir/CharUtilTest.cpp.o: In function `AChar_IsAVowelForSixSpecificLetters_Test::TestBody()':
CharUtilTest.cpp:(.text+0x78): undefined reference to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)'
CharUtilTest.cpp:(.text+0x160): undefined reference to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)'
CharUtilTest.cpp:(.text+0x248): undefined reference to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)'
CharUtilTest.cpp:(.text+0x330): undefined reference to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)'
CharUtilTest.cpp:(.text+0x418): undefined reference to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)'
CMakeFiles/test.dir/CharUtilTest.cpp.o:CharUtilTest.cpp:(.text+0x500): more undefined references to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)' follow
CMakeFiles/test.dir/CharUtilTest.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
CharUtilTest.cpp:(.text+0x18fc): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CharUtilTest.cpp:(.text+0x1954): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CharUtilTest.cpp:(.text+0x19ac): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CharUtilTest.cpp:(.text+0x1a04): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CharUtilTest.cpp:(.text+0x1a5c): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CMakeFiles/test.dir/CharUtilTest.cpp.o:CharUtilTest.cpp:(.text+0x1ab4): more undefined references to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)' follow
/home/matthew/googlemock-release-1.6.0/mybuild/libgmock.a(gmock-all.cc.o): In function `testing::internal::Log(testing::internal::LogSeverity, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
gmock-all.cc:(.text+0xdc1): undefined reference to `testing::internal::GetCurrentOsStackTraceExceptTop(testing::UnitTest*, int)'
/home/matthew/googlemock-release-1.6.0/mybuild/libgmock.a(gmock-all.cc.o): In function `testing::internal::ParseGoogleMockFlagValue(char const*, char const*, bool)':
gmock-all.cc:(.text+0x44f2): undefined reference to `testing::internal::String::Format(char const*, ...)'
/home/matthew/googlemock-release-1.6.0/mybuild/libgmock.a(gmock-all.cc.o): In function `testing::internal::String::operator==(char const*) const':
gmock-all.cc:(.text._ZNK7testing8internal6StringeqEPKc[_ZNK7testing8internal6StringeqEPKc]+0x42): undefined reference to `testing::internal::String::Compare(testing::internal::String const&) const'
/home/matthew/googlemock-release-1.6.0/mybuild/libgmock.a(gmock-all.cc.o): In function `testing::Message::operator<<(wchar_t*)':
gmock-all.cc:(.text._ZN7testing7MessagelsEPw[_ZN7testing7MessagelsEPw]+0x2f): undefined reference to `testing::internal::String::ShowWideCString(wchar_t const*)'
collect2: error: ld returned 1 exit status
CMakeFiles/test.dir/build.make:158: recipe for target 'test' failed
make[2]: *** [test] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/test.dir/all' failed
make[1]: *** [CMakeFiles/test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
.
matthew#Matthew:~/code/c2/40/build$ make VERBOSE=1
/usr/local/bin/cmake -H/home/matthew/code/c2/40 -B/home/matthew/code/c2/40/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/matthew/code/c2/40/build/CMakeFiles /home/matthew/code/c2/40/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/matthew/code/c2/40/build'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/depend
make[2]: Entering directory '/home/matthew/code/c2/40/build'
cd /home/matthew/code/c2/40/build && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/matthew/code/c2/40 /home/matthew/code/c2/40 /home/matthew/code/c2/40/build /home/matthew/code/c2/40/build /home/matthew/code/c2/40/build/CMakeFiles/test.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/matthew/code/c2/40/build'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
make[2]: Entering directory '/home/matthew/code/c2/40/build'
[ 14%] Linking CXX executable test
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/c++ -Wall -rdynamic CMakeFiles/test.dir/CharUtilTest.cpp.o CMakeFiles/test.dir/SoundexTest.cpp.o CMakeFiles/test.dir/StringUtilTest.cpp.o CMakeFiles/test.dir/main.cpp.o CMakeFiles/test.dir/CharUtil.cpp.o CMakeFiles/test.dir/StringUtil.cpp.o -o test -L/home/matthew/googletest-release-1.8.0/googlemock/build -L/home/matthew/googletest-release-1.8.0/googlemock/gtest/mybuild -Wl,-rpath,/home/matthew/googletest-release-1.8.0/googlemock/build:/home/matthew/googletest-release-1.8.0/googlemock/gtest/mybuild -lpthread -lgmock -lgtest
/usr/bin/ld: cannot find -lgmock
collect2: error: ld returned 1 exit status
CMakeFiles/test.dir/build.make:158: recipe for target 'test' failed
make[2]: *** [test] Error 1
make[2]: Leaving directory '/home/matthew/code/c2/40/build'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/test.dir/all' failed
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make[1]: Leaving directory '/home/matthew/code/c2/40/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Error when linking gtest project

recently I start study unit testing , and I want test my program with gtest. I install all with this order :
$ git clone https://github.com/google/googletest
$ cd googletest
$ cmake -DBUILD_SHARED_LIBS=ON .
$ make
$ cd googlemock
$ sudo cp ./libgmock_main.so ./gtest/libgtest.so gtest/libgtest_main.so ./libgmock.so /usr/lib/
$ sudo ldconfig
and now write code :
#include "gtest/gtest.h"
class Add
{
private:
int element;
public:
Add():element(0){}
~Add(){}
void setElement(int e){ element = e; }
int getElement() { return element; }
int adder(int e) { return element += e; }
};
class AddTest : public ::testing::Test
{
protected:
int abc(int a){
return a;
}
virtual void SetUp(){
add1.setElement(1);
add2.setElement(20);
}
virtual void TearDown(){}
Add add1;
Add add2;
};
TEST_F(AddTest, getTest)
{
EXPECT_EQ(1, add1.getElement());
EXPECT_EQ(20, add2.getElement());
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
end when I run test i get this error :
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest_getTest_Test::TestBody()':
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::Message::Message()'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference totesting::Message::Message()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
CMakeFiles/mock2.dir/main.cpp.o: In functionmain':
/home/artem/CLionProjects/mock2/main.cpp:39: undefined reference to testing::InitGoogleTest(int*, char**)'
CMakeFiles/mock2.dir/main.cpp.o: In function__static_initialization_and_destruction_0(int, int)':
/home/artem/CLionProjects/mock2/main.cpp:31: undefined reference to testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CMakeFiles/mock2.dir/main.cpp.o: In functionRUN_ALL_TESTS()':
/usr/local/include/gtest/gtest.h:2235: undefined reference to testing::UnitTest::GetInstance()'
/usr/local/include/gtest/gtest.h:2235: undefined reference totesting::UnitTest::Run()'
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest::AddTest()':
/home/artem/CLionProjects/mock2/main.cpp:15: undefined reference totesting::Test::Test()'
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest::~AddTest()':
/home/artem/CLionProjects/mock2/main.cpp:15: undefined reference totesting::Test::~Test()'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::internal::scoped_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
/usr/local/include/gtest/internal/gtest-port.h:1172: undefined reference totesting::internal::IsTrue(bool)'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::internal::scoped_ptr<std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)':
/usr/local/include/gtest/internal/gtest-port.h:1172: undefined reference totesting::internal::IsTrue(bool)'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1394: undefined reference totesting::AssertionSuccess()'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1384: undefined reference totesting::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, bool)'
CMakeFiles/mock2.dir/main.cpp.o:(.rodata._ZTI7AddTest[_ZTI7AddTest]+0x10): undefined reference to `typeinfo for testing::Test'
collect2: error: ld returned 1 exit status
CMakeFiles/mock2.dir/build.make:94: recipe for target 'mock2' failed
make[3]: * [mock2] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mock2.dir/all' failed
make[2]: [CMakeFiles/mock2.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/mock2.dir/rule' failed
make[1]: [CMakeFiles/mock2.dir/rule] Error 2
Makefile:118: recipe for target 'mock2' failed
make: * [mock2] Error 2
but when use command
g++ main.cpp -o test -lgtest -lpthread
everytheng is good. How can I fix it and run it not in command line ?
If you are using CLion then you may have a CMakeLists.txt file you should add rules to link to the libraries. To do this add the following line to your CMakeLists.txt
enable_testing()
find_package(GTest REQUIRED) # Find the google testing framework on your system
include_directories(${GTEST_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES}) # Replace ${PROJECT_NAME} with your target name
For more details go here.