Reputation: 51
I am new to gtest/gmock. I am trying to implement unit testing for C++ Program.but when i try to compile it. i got linking error with gmock. this specific error is related testing::internal::GetCurrentOsStackTraceExceptTop
.
Installation of gtest/gmock:
Step 1: sudo apt-get install libgtest-dev
Step 2: download the git repo of google test from here: https://github.com/google/googletest
Step 3: sudo apt install cmake
Step 4: run these commands {~googletest$}
sudo cmake CMakeLists.txt
sudo make
Step 5: copy files in /usr/lib
Step 6: copy gtest/gmock include folder into /usr/local/include
version of the Google Test library that is installed on my system is 1.10.0-2.
test.cpp
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <gmock/gmock-actions.h>
#include <iostream>
#include "timeEvent.h"
using ::testing::_;
using ::testing::Return;
class MockStat {
public:
MOCK_METHOD2(stat, int(const char* pathname, struct stat* buf));
};
TEST(MyTest, PositiveCase) {
MockStat mock_stat;
struct stat fileStat;
fileStat.st_mtime = 111;
fileStat.st_ctime = 222;
EXPECT_CALL(mock_stat, stat("/var/lib/systemd/timesync/clock", &fileStat))
.WillOnce(Return(0));
auto latest_clock = get_latest_clock_entry();
EXPECT_EQ(latest_clock.tv_sec, 111);
EXPECT_EQ(latest_clock.tv_nsec, 0);
}
compile: g++ test.cpp library.cpp -lgtest -lgtest_main -lpthread -lboost_thread -pthread -lboost_filesystem -lgtest -lgmock -lgmock_main
Error:
g++ update.cpp library.cpp -lgtest -lgtest_main -lpthread -lboost_thread -pthread -lboost_filesystem -lgtest -lgmock -lgmock_main -lstdc++
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib/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+0x121a): undefined reference to `testing::internal::GetCurrentOsStackTraceExceptTop[abi:cxx11](int)'
collect2: error: ld returned 1 exit status
Upvotes: 0
Views: 1379
Reputation: 1
Agree with Marcin Osypka. also thanks to 273K. encountered the same problem after make.
libgtest-dev and github repository were found on my machine. then I removed libgtest-dev.
g++ -g -fno-access-control test_main.cpp test_http.cpp ../../http.cpp -o test -lpthread -lgmock -lgtest && ./test
g++ -g -fno-access-control test_main.cpp test_http.cpp ../../http.cpp -o test -lpthread -lgtest -lgmock && ./test
p.s. both of them work well for me.
run
sudo apt list --installed | grep gtest
libgtest-dev/focal,now 1.10.0-2 arm64 [installed]
sudo apt remove libgtest-dev
Upvotes: 0
Reputation: 1
Agree with Marcin Osypka
Make sure you don't have libgmock-dev
and libgtest-dev
installed if you use github repository installed manually
sudo apt-get remove libgtest-dev
fixed my problem with compatibility
Upvotes: 0
Reputation: 38539
testing::internal::GetCurrentOsStackTraceExceptTop[abi:cxx11](int)
is defined in libgtest
. libgmock
depends on libgtest
. Therefore -lgtest
should follow -lgmock
:
g++ update.cpp library.cpp -lboost_thread -pthread -lboost_filesystem -lgmock -lgtest -lgmock_main
Don't use -lstdc++
with g++. It's by default. And a proper way is -stdlib=stdc++
.
See Why does the order in which libraries are linked sometimes cause errors in GCC? for more info. Your question can be considered as a dupe.
Upvotes: 1
Reputation: 32
Looks like you are using gmock/gtest libraries from 2 different sources. The first source is libgtest-dev
you installed and the second is github repository. You should use either libgtest-dev
or the one from github repo.
Upvotes: 1