0

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
  • Mike Kinghan's answer in https://stackoverflow.com/questions/24145649/gtest-undefined-reference-to-testingtesttest-testingtesttest – dorKKnight Jan 26 '23 at 14:45
  • How did you installed gtest library? This can happen when gtest was build without C++11 (or higher) enabled and your code is using higher C++ standard. – Marek R Feb 03 '23 at 17:50

3 Answers3

1

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.

1

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.

273K
  • 29,503
  • 10
  • 41
  • 64
0

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