Howto install google-mock on Ubuntu 12.10
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
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/gmock
sudo mkdir build
sudo cmake ..
sudo make
sudo cp *.a /usr/lib
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.