How to install Google Test on Ubuntu without root access?

纵饮孤独 提交于 2019-12-05 05:35:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38395984/how-to-install-google-test-on-ubuntu-without-root-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!