5

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 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

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?

Community
  • 1
  • 1
MattSom
  • 2,097
  • 5
  • 31
  • 53

1 Answers1

6

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.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • Will certainly try tomorrow, thanks for the explanation kind sir. – MattSom Jul 17 '16 at 10:51
  • After make install command this is what I get: **_make: *** No rule to make target `install'. Stop._** What does it mean? my home/me/googletest stayed empty. – MattSom Jul 18 '16 at 15:10
  • Did you get and use the latest googletest master source from https://github.com/google/googletest as the answer says? – Mike Kinghan Jul 18 '16 at 15:38
  • I did not use the master folder, just the newest release, 1.7.0. I think I misunderstood you there. However reading on since, I found this comment saying I should avoid make install command: http://stackoverflow.com/a/13513907/6560773 I'm a little confused now. – MattSom Jul 18 '16 at 15:55
  • 1
    @MattSom gtest-1.7.0 is 3 + 1/2 years old and so is the other SO answer you have read. Both are out of date. I well remember that `make install` was not supported on gtest-1.7.0. It is supported now. Delete any gtest-1.7.0 materials and start from scratch, following the steps in my answer. – Mike Kinghan Jul 18 '16 at 18:19
  • Just tried this morning. Works fine and dandy. I've been trying to get this done for days, and none of the other installation guides worked for me. Thank you very much, cant display vote yet but please accept my +1 here for the time! – MattSom Jul 19 '16 at 09:02
  • I saw only static library, how can we have both static lib(.a) and shared lib(.so) at the same time? – BruceSun Oct 27 '21 at 01:17