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.