How to set up googleTest as a shared library on Linux How to set up googleTest as a shared library on Linux linux linux

How to set up googleTest as a shared library on Linux


Before you start make sure your have read and understoodthis note from Google! This tutorial makes using gtest easy, but may introduce nasty bugs.

1. Get the googletest framework

wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz

Or get it by hand. I won't maintain this little How-to, so if you stumbled upon it and the links are outdated, feel free to edit it.

2. Unpack and build google test

tar xf release-1.8.0.tar.gzcd googletest-release-1.8.0cmake -DBUILD_SHARED_LIBS=ON .make

3. "Install" the headers and libs on your system.

This step might differ from distro to distro, so make sure you copy the headers and libs in the correct directory. I accomplished this by checking where Debians former gtest libs were located. But I'm sure there are better ways to do this. Note: make install is dangerous and not supported

sudo cp -a googletest/include/gtest /usr/includesudo cp -a googlemock/gtest/libgtest_main.so googlemock/gtest/libgtest.so /usr/lib/

4. Update the cache of the linker

... and check if the GNU Linker knows the libs

sudo ldconfig -v | grep gtest

If the output looks like this:

libgtest.so.0 -> libgtest.so.0.0.0libgtest_main.so.0 -> libgtest_main.so.0.0.0

then everything is fine.

gTestframework is now ready to use. Just don't forget to link your project against the library by setting -lgtest as linker flag and optionally, if you did not write your own test mainroutine, the explicit -lgtest_main flag.

From here on you might want to go to Googles documentation, and the old docs about the framework to learn how it works. Happy coding!

Edit:This works for OS X too! See "How to properly setup googleTest on OS X"


Let me answer this specifically for ubuntu users.First start by installing the gtest development package.

sudo apt-get install libgtest-dev

Note that this package only install source files. You have to compile the code yourself to create the necessary library files. These source files should be located at /usr/src/gtest. Browse to this folder and use cmake to compile the library:

sudo apt-get install cmake # install cmakecd /usr/src/gtestsudo mkdir buildcd buildsudo cmake ..sudo makesudo make install

Now to compile your programs that uses gtest, you have to link it with:

-lgtest -lgtest_main -lpthread

This worked perfectly for me on Ubuntu 14.04LTS.


It took me a while to figure out this because the normal "make install" has been removed and I don't use cmake. Here is my experience to share. At work, I don't have root access on Linux, so I installed the Google test framework under my home directory: ~/usr/gtest/.

To install the package in ~/usr/gtest/ as shared libraries, together with sample build as well:

$ mkdir ~/temp$ cd ~/temp$ unzip gtest-1.7.0.zip $ cd gtest-1.7.0$ mkdir mybuild$ cd mybuild$ cmake -DBUILD_SHARED_LIBS=ON -Dgtest_build_samples=ON -G"Unix Makefiles" ..$ make$ cp -r ../include/gtest ~/usr/gtest/include/$ cp lib*.so ~/usr/gtest/lib

To validate the installation, use the following test.c as a simple test example:

    #include <gtest/gtest.h>    TEST(MathTest, TwoPlusTwoEqualsFour) {        EXPECT_EQ(2 + 2, 4);    }    int main(int argc, char **argv) {        ::testing::InitGoogleTest( &argc, argv );        return RUN_ALL_TESTS();    }

To compile:

    $ export GTEST_HOME=~/usr/gtest    $ export LD_LIBRARY_PATH=$GTEST_HOME/lib:$LD_LIBRARY_PATH    $ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cpp