Debian does not provide any precompiled packages for gTest anymore. They suggest you integrate the framework into your project's makefile. But I want to keep my makefile clean. How do I set up gTest like the former versions (<1.6.0), so that I can link against the library?
13 Answers
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"
-
2Is there no
make install
target that you can use instead of manually copying the library and headers? Nov 22, 2012 at 13:57 -
16Cite of the output of the makefile:
'make install' is dangerous and not supported. Instead, see README for how to integrate Google Test into your build system.
Nov 22, 2012 at 14:08 -
Have the files been renamed in the 1.8.0 version? There is no
include/gtest
as far as I can tell.– NubcakeMar 30, 2018 at 21:01 -
4Your post is out-of-date. Please do not mislead people,
sudo cp -a libgtest_main.so libgtest.so /usr/lib/
does not work anymore. The file isn't even there to begin with.– SchützeNov 7, 2018 at 8:48 -
I'd also like to know why debian removed a pre-installed shared library (they did so as per upstreams recommendations: bugs.debian.org/cgi-bin/bugreport.cgi?bug=802587). The wiki link given there does no longer exist though. So why was this the case? Feb 23, 2019 at 11:36
Let me answer this specifically for ubuntu users. First start by installing the gtest development package.
sudo apt-get install libgtest-dev
Note that this package only install source files. You have to compile the code yourself to create the necessary library files. These source files should be located at /usr/src/gtest. Browse to this folder and use cmake to compile the library:
sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo make install
Now to compile your programs that uses gtest, you have to link it with:
-lgtest -lgtest_main -lpthread
This worked perfectly for me on Ubuntu 14.04LTS.
-
1Actually you don't have to copy libraries manually, there is a target for that in Makefile. You can just do it like that: sudo apt-get install cmake # install cmake cd /usr/src/gtest sudo cmake CMakeLists.txt sudo make install That should be built and copy /usr/local/lib/ Oct 20, 2017 at 18:51
-
1@AlexanderZinovyev I get "make: *** No rule to make target 'install'. Stop." when I execute the "sudo make install"– m4l490nMar 28, 2018 at 15:33
-
2"sudo make install" worked on Ubuntu 18.04, but didn't work on Ubuntu 16.04. Aug 10, 2018 at 17:57
-
1@AhmedNassar: "sudo make install" does just the same thing as "sudo cp *.a /usr/lib". So, if install option is not available in generated Makefile, you just copy them manually– amritkrsAug 16, 2018 at 11:17
-
There is no need to manually
sudo cp *.a /usr/lib
, just repalce it withsudo make install
should be fine. Jul 28, 2019 at 18:27
It took me a while to figure out this because the normal "make install" has been removed and I don't use cmake. Here is my experience to share. At work, I don't have root access on Linux, so I installed the Google test framework under my home directory: ~/usr/gtest/
.
To install the package in ~/usr/gtest/ as shared libraries, together with sample build as well:
$ 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
$ cp -r ../include/gtest ~/usr/gtest/include/
$ cp lib*.so ~/usr/gtest/lib
To validate the installation, use the following test.c as a simple test example:
#include <gtest/gtest.h>
TEST(MathTest, TwoPlusTwoEqualsFour) {
EXPECT_EQ(2 + 2, 4);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS();
}
To compile:
$ export GTEST_HOME=~/usr/gtest
$ export LD_LIBRARY_PATH=$GTEST_HOME/lib:$LD_LIBRARY_PATH
$ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cpp
-
1With the last line I get error: /usr/bin/ld: /tmp/cczG727X.o: undefined reference to symbol '_ZN7testing4TestC2Ev'. I fixed this placing test.cpp before the libraries. i.e: g++ test.cpp -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread Jun 11, 2015 at 16:08
-
If linking against gtest_main (
lgtest_main
), there is no need to define your ownmain
in the test file. Jun 15, 2020 at 2:59
If you happen to be using CMake, you can use ExternalProject_Add
as described here.
This avoids you having to keep gtest source code in your repository, or installing it anywhere. It is downloaded and built in your build tree automatically.
Update for Debian/Ubuntu
Google Mock (package: google-mock
) and Google Test (package: libgtest-dev
) have been merged. The new package is called googletest
. Both old names are still available for backwards compatibility and now depend on the new package googletest
.
So, to get your libraries from the package repository, you can do the following:
sudo apt-get install googletest -y
cd /usr/src/googletest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo cp googlemock/*.a googlemock/gtest/*.a /usr/lib
After that, you can link against -lgmock
(or against -lgmock_main
if you do not use a custom main method) and -lpthread
. This was sufficient for using Google Test in my cases at least.
If you want the most current version of Google Test, download it from github. After that, the steps are similar:
git clone https://github.com/google/googletest
cd googletest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo cp lib/*.a /usr/lib
As you can see, the path where the libraries are created has changed. Keep in mind that the new path might be valid for the package repositories soon, too.
Instead of copying the libraries manually, you could use sudo make install
. It "currently" works, but be aware that it did not always work in the past. Also, you don't have control over the target location when using this command and you might not want to pollute /usr/lib
.
I was similarly underwhelmed by this situation and ended up making my own Ubuntu source packages for this. These source packages allow you to easily produce a binary package. They are based on the latest gtest & gmock source as of this post.
Google Test DEB Source Package
Google Mock DEB Source Package
To build the binary package do this:
tar -xzvf gtest-1.7.0.tar.gz
cd gtest-1.7.0
dpkg-source -x gtest_1.7.0-1.dsc
cd gtest-1.7.0
dpkg-buildpackage
It may tell you that you need some pre-requisite packages in which case you just need to apt-get install them. Apart from that, the built .deb binary packages should then be sitting in the parent directory.
For GMock, the process is the same.
As a side note, while not specific to my source packages, when linking gtest to your unit test, ensure that gtest is included first (https://bbs.archlinux.org/viewtopic.php?id=156639) This seems like a common gotcha.
-
Your package gives me errors when I try to compile. Any reason why?? here is my log test.cpp:(.text+0x57): undefined reference to
testing::Message::Message()' test.cpp:(.text+0x84): undefined reference to
testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)' test.cpp:(.text+0x97): undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const' ... its so long that I can't post the entire thing. I did this in a brand new Ubuntu 14.04 VM so nothing else was installed except the necessary dependencies.– ddelnanoMar 28, 2015 at 19:51 -
@ddelnano Yeah i ran into this little google test gem too. Apparently the order of the shared libraries is important. When linking gtest to your unit test, try including gtest before other libraries. When i hit this problem, this link solved it for me: bbs.archlinux.org/viewtopic.php?id=156639 Mar 29, 2015 at 1:18
-
@ddelnano, also if your test suite does not have a 'main' defined then don't forget to link against 'gtest_main'. Mar 29, 2015 at 1:32
-
I didn't include any other libraries. this is all i had in my file
#include <gtest/gtest.h> TEST(MathTest, TwoPlusTwoEqualsFour) { EXPECT_EQ(2 + 2, 4); } int main(int argc, char **argv) { ::testing::InitGoogleTest( &argc, argv ); return RUN_ALL_TESTS(); }
– ddelnanoMar 29, 2015 at 15:11 -
nevermind I didn't read the blog post until after I posted that comment. It is now finally working!– ddelnanoMar 29, 2015 at 16:59
Just in case somebody else gets in the same situation like me yesterday (2016-06-22) and also does not succeed with the already posted approaches - on Lubuntu 14.04
it worked for me using the following chain of commands:
git clone https://github.com/google/googletest
cd googletest
cmake -DBUILD_SHARED_LIBS=ON .
make
cd googlemock
sudo cp ./libgmock_main.so ./gtest/libgtest.so gtest/libgtest_main.so ./libgmock.so /usr/lib/
sudo ldconfig
This answer from askubuntu is what worked for me. Seems simpler than other options an less error-prone, since it uses package libgtest-dev
to get the sources and builds from there: https://askubuntu.com/questions/145887/why-no-library-files-installed-for-google-test?answertab=votes#tab-top
Please refer to that answer, but just as a shortcut I provide the steps here as well:
sudo apt-get install -y libgtest-dev
sudo apt-get install -y cmake
cd /usr/src/gtest
sudo cmake .
sudo make
sudo mv libg* /usr/lib/
After that, I could build my project which depends on gtest
with no issues.
The following method avoids manually messing with the /usr/lib
directory while also requiring minimal change in your CMakeLists.txt
file. It also lets your package manager cleanly uninstall libgtest-dev
.
The idea is that when you get the libgtest-dev
package via
sudo apt install libgtest-dev
The source is stored in location /usr/src/googletest
You can simply point your CMakeLists.txt
to that directory so that it can find the necessary dependencies
Simply replace FindGTest
with add_subdirectory(/usr/src/googletest gtest)
At the end, it should look like this
add_subdirectory(/usr/src/googletest gtest)
target_link_libraries(your_executable gtest)
This will install google test and mock library in Ubuntu/Debian based system:
sudo apt-get install google-mock
Tested in google cloud in debian based image.
-
Hmm, no, I think this only installs
googlemock
, but it does not installgoogletest
(gtest). At least that's what happened to me. Dec 24, 2016 at 6:52
This will build and install both gtest and gmock 1.7.0:
mkdir /tmp/googleTestMock
tar -xvf googletest-release-1.7.0.tar.gz -C /tmp/googleTestMock
tar -xvf googlemock-release-1.7.0.tar.gz -C /tmp/googleTestMock
cd /tmp/googleTestMock
mv googletest-release-1.7.0 gtest
cd googlemock-release-1.7.0
cmake -DBUILD_SHARED_LIBS=ON .
make -j$(nproc)
sudo cp -a include/gmock /usr/include
sudo cp -a libgmock.so libgmock_main.so /usr/lib/
sudo cp -a ../gtest/include/gtest /usr/include
sudo cp -a gtest/libgtest.so gtest/libgtest_main.so /usr/lib/
sudo ldconfig
With buster
and bullseye
one can install the following three packages without compiling anything:
sudo apt-get install libgtest-dev libgmock-dev googletest
This includes gmock.
For 1.8.1 based on @ManuelSchneid3r 's answer I had to do:
wget github.com/google/googletar xf release-1.8.1.tar.gz
tar xf release-1.8.1.tar.gz
cd googletest-release-1.8.1/
cmake -DBUILD_SHARED_LIBS=ON .
make
I then did make install
which seemed to work for 1.8.1, but
following @ManuelSchneid3r it would mean:
sudo cp -a googletest/include/gtest /usr/include
sudo cp -a googlemock/include/gmock /usr/include
sudo cp `find .|grep .so$` /usr/lib/
-
Its been a while that I wrote this answer, let me know what is the problem with it if you think it has no value / is invalid.– ntgApr 30, 2019 at 15:13
./configure && make && make install
workflow should work just fine. I'm not sure if this warrants a post since it would be no different from compiling many other packages from source.