How to Install Google Test on Ubuntu Without Root Access

How to install Google Test on Ubuntu without root access?

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

Browse to the googletest GitHub repo https://github.com/google/googletest. (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:-

/home/me/googletest/
lib/
libgmock.a
libgmock_main.a
libgtest.a
libgtest_main.a
include/
gmock/
# gmock header files
gtest/
# 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.

How to set up googleTest as a shared library on Linux

Before you start make sure your have read and understood
this 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.gz
cd googletest-release-1.8.0
cmake -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.

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

# The easiest/best way:
make install # Note: before v1.11 this can be dangerous and is not supported

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.0
libgtest_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"

How to reconfigure Google Test for a 32-bit embedded software?

To build 32-bit googletest using my answer that you have referred to
just follow the same procedure but instead of running:

cmake -DCMAKE_INSTALL_PREFIX:PATH=/home/me/googletest ../googletest

run:

cmake -DCMAKE_CXX_FLAGS=-m32 -DCMAKE_INSTALL_PREFIX:PATH=/home/me/googletest ../googletest

If you are making even a personal installation of 32-bit googletest on a 64-bit host
it would be a good idea to make it obvious that it is 32-bit, e.g. by using the like of:

-DCMAKE_INSTALL_PREFIX:PATH=/home/me/googletest_32

Running googletest with separated src- and test-folders

You are not linking yout test project against your library. So it doesn't use your library. Link it.

target_link_libraries(${This} addition)


Related Topics



Leave a reply



Submit