Wednesday, May 1, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 35119  / 1 Year ago, fri, march 24, 2023, 3:57:32

I am having hard time trying to install Google C++ Mocking Framework. I have successfully run sudo apt-get install google-mock. Then I tried to compile this sample file



#include "gmock/gmock.h"
int main(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}


with g++ -lgmock main.cpp and these errors have shown



main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleMock(int*, char**)'
main.cpp:(.text+0x23): undefined reference to `testing::UnitTest::GetInstance()'
main.cpp:(.text+0x2b): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status


I guess the linker can not find the library files. Does anybody know how to fix this?


More From » installation

 Answers
5

OK, I've now successfully started using gmock by building my own version as per the README provided with the source download from googlemock project website.



Get the download zip from the website:
http://code.google.com/p/googlemock/downloads/list



Unzip this to a directory, say ${GMOCK_ROOT}. Then, as per README instructions:



cd ${GMOCK_ROOT}
mkdir build
cd build
g++ -I../gtest/include -I../gtest -I../include -I.. -c ../gtest/src/gtest-all.cc
g++ -I../gtest/include -I../gtest -I../include -I.. -c ../src/gmock-all.cc
ar -rv libgmock.a gtest-all.o gmock-all.o


Thus you have your own libgmock.a in ${GMOCK_ROOT}/build. You actually also need pthreads to compile, so your compile command after that becomes:



g++ -I${GMOCK_ROOT}/include/ main.cpp -L${GMOCK_ROOT}/build -lgmock -lpthread

[#34176] Saturday, March 25, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
needstar

Total Points: 268
Total Questions: 108
Total Answers: 117

Location: Seychelles
Member since Mon, Jun 28, 2021
3 Years ago
;