How to Set Up Googletest as a Shared Library on Linux

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 build and use GoogleTest shared library (.so) using g++ on Linux?

Here is the solution I found.

step 1. build object file gtest-all.o

g++ -fPIC -isystem ../path/to/googletest/include -I../utils/third-party/googletest/ -pthread -c ../path/to/googletest/src/gtest-all.cc

It's recommended by GoogleTest's doc to add a macro flag -DGTEST_CREATE_SHARED_LIBRARY=1 to the command above.

step 2. compile shared library

g++ -fPIC -shared gtest-all.o -o libgtest.so

Then, to link against the shared library

Be sure to use -Wl,-rpath=./path/to/libgtest.so flag. There is no white space between -Wl, and -rpath (-Wl,option means passing option to linker. more on -Wl,option). It's recommended to add flag -DGTEST_LINKED_AS_SHARED_LIBRARY=1.

Build Google Test like shared library

Two possibilities: If you are not at liberty to change the gtest sources, set the BUILD_SHARED_LIBS option before the add_subdirectory call for gtest, and reset it back immediately after:

set(BUILD_SHARED_LIBS_OLD ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCED)
add_subdirectory(gtest)
set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_OLD} CACHE BOOL "" FORCED)

Alternatively, if you do want to change the gtest sources, replace the cxx_library() with calls to cxx_shared_library():

cxx_shared_library(gtest "${cxx_strict}" src/gtest-all.cc)   

This is required because the cxx_library_* macros set some additional stuff for the build that a plain add_library would miss.

Note that you might still want to keep gtest_main as a static library, as it defines the entry point (aka main()) for your program and moving that to a shared library might not have the effect that you intended.

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.



Related Topics



Leave a reply



Submit