MattSom MattSom - 1 year ago 246
Linux Question

How to install Google Test on Ubuntu without root access?

I am trying to install Google Test according to this answer on Ubuntu without root access, as I need learn and use it at work.

Managed to get these done in my own user folder:

$ mkdir ~/temp
$ cd ~/temp
$ unzip
$ cd gtest-1.7.0
$ mkdir mybuild
$ cd mybuild
$ cmake -DBUILD_SHARED_LIBS=ON -Dgtest_build_samples=ON -G"Unix Makefiles" ..
$ make

It seems I already have gtest in /usr/src/gtest altough I don't want to use this, because it was not me who installed it and I'm not sure about its version, nor in its availability. Can't even delete it without permission.

Still the instruction ends as:

$ cp -r ../include/gtest ~/usr/gtest/include/
$ cp lib*.so ~/usr/gtest/lib

What am I missing here?

Answer Source

Let's say you want to install googletest in /home/me/googletest.

Browse to the googletest GitHub repo (Do not use a possibly -out-of-date version you may have got elsewhere.)

Using the Clone or Download link, either clone or download-and-extract the source as (let's say) ./googletest under your current directory CWD (where CWD is not /home/me/).

Then in CWD:-

$ mkdir googletest_build
$ cd googletest_build
$ cmake -DCMAKE_INSTALL_PREFIX:PATH=/home/me/googletest ../googletest
$ make
$ make install

After this, you will find:-

                            # gmock header files
                            # gtest header files

You can then use gtest/gmock headers in your source code like:

#include <gtest/gtest.h>
#include <gmock/gmock.h>

and compile and link a gtest/gmock program like:

g++ -pthread -I/home/me/googletest/include -c -o my-unit-tester.o my-unit-tester.cpp
g++ -o my-unit-tester my-unit-tester.o -L/home/me/googletest/lib -lgtest -lgmock -pthread

using the -I... option to tell the compiler where gtest/gmock headers reside and using the -L... option to tell the linker where gtest/gmock libraries reside.

Pass -pthread to both compiler and linker because the gtest/gmock libraries are built multi-threading by default.

After installing you no longer need either CWD/googletest or CWD/googletest_build.

You may wish to pass additional options to cmake, in which case the build products will differ as per the meaning of those additional options.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download