Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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:

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
share|improve this answer
Confirmed that it works. Goog job! I just wonder why is the order of g++'s parameters important. –  Slazer Nov 28 '12 at 19:36

To give context to Pavel's answer, the compiled Google Mock binary is not distributed with the Ubuntu package because of the reason given here. This explanation is for Google Test, but the principle applies to any C++ library.

Specifically, it says:

In the early days, we said that you could install compiled Google Test libraries on *nix systems using make install. Then every user of your machine can write tests without recompiling Google Test.

This seemed like a good idea, but it has a got-cha: every user needs to compile his tests using the same compiler flags used to compile the installed Google Test libraries; otherwise he may run into undefined behaviors (i.e. the tests can behave strangely and may even crash for no obvious reasons).

Why? Because C++ has this thing called the One-Definition Rule: if two C++ source files contain different definitions of the same class/function/variable, and you link them together, you violate the rule. The linker may or may not catch the error (in many cases it's not required by the C++ standard to catch the violation). If it doesn't, you get strange run-time behaviors that are unexpected and hard to debug.

If you compile Google Test and your test code using different compiler flags, they may see different definitions of the same class/function/variable (e.g. due to the use of #if in Google Test). Therefore, for your sanity, we recommend to avoid installing pre-compiled Google Test libraries. Instead, each project should compile Google Test itself such that it can be sure that the same flags are used for both Google Test and the tests.

So your original problem was because installing the google-mock package only installed the source code, and when you tried to compile your sample application, no gmock library could be found.

share|improve this answer
I wonder why is this so. There are many C++ libraries that work precompiled inside /lib. –  Slazer Nov 28 '12 at 19:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.