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