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?

4 Answers 4


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

libgmock-dev will be included in the default Ubuntu 18.10 repositories, Otherwise in earlier Ubuntu releases you have to manually download and install it.

sudo apt-get install libgmock-dev
cd /usr/src/googletest/googlemock/
sudo mkdir build
sudo cmake ..
sudo make
sudo cp googlemock/*.a /usr/lib
  • make sure you add detailed explanation of what you are suggesting the user tries. May 23, 2018 at 16:48

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.

  • 4
    I wonder why is this so. There are many C++ libraries that work precompiled inside /lib.
    – Slazer
    Nov 28, 2012 at 19:39

I tried to install on Ubuntu 18.04.3 LTS 'gmock' and everything related to its installation. I work with two folders '/tmp' and '/gmock', when absent 'gmock' i get:

Configuring done
Generating done

for install gmock:

# snap install --classic cmake
# apt install g++
# export PATH="/snap/bin:$PATH"
# VERSION='1.7.0'
# INSTALL_DIR='/usr/local'
# mkdir ~/.mydownload && DOWNLOAD_DIR='~/.mydownload'
# cd ~/.mydownload
# GTEST_TARFILE="gtest-${VERSION}.tar.gz"
# GTEST_DIR='gtest'
# wget -O $GTEST_TARFILE https://github.com/google/googletest/archive/release-${VERSION}.tar.gz
# tar xzvf $GTEST_TARFILE
# mv googletest-release-${VERSION} $GTEST_DIR
# GMOCK_TARFILE="gmock-${VERSION}.tar.gz"
# GMOCK_DIR="gmock"
# wget -O $GMOCK_TARFILE https://github.com/google/googlemock/archive/release-${VERSION}.tar.gz
# tar xzvf $GMOCK_TARFILE
# mv googlemock-release-${VERSION} $GMOCK_DIR
# cd ~/.mydownload/gmock
# cmake .
# make
# mkdir -p ${INSTALL_DIR}/include
# cp -r include/gmock ${INSTALL_DIR}/include
# cp -r ../${GTEST_DIR}/include/gtest ${INSTALL_DIR}/include
# mkdir -p ${INSTALL_DIR}/lib
# cp *.a ${INSTALL_DIR}/lib
# cp gtest/*.a ${INSTALL_DIR}/lib
# cd /home/user
# rm -r ~/.mydownload

after these commands i see what 'gmock' is found:

Found GMock: /usr/local/lib/libgmock.a
Configuring done
Generating done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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