How to set up googleTest as a shared library on Linux
Solution 1
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"
Solution 2
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.
Solution 3
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
Solution 4
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.
Solution 5
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
.
ManuelSchneid3r
Updated on August 30, 2020Comments
-
ManuelSchneid3r over 3 years
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?
-
Shawn Chin over 11 yearsIs there no
make install
target that you can use instead of manually copying the library and headers? -
ManuelSchneid3r over 11 yearsCite 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.
-
ddelnano about 9 yearsYour 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. -
Nick Weedon about 9 years@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
-
Nick Weedon about 9 years@ddelnano, also if your test suite does not have a 'main' defined then don't forget to link against 'gtest_main'.
-
ddelnano about 9 yearsI 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(); }
-
ddelnano about 9 yearsnevermind I didn't read the blog post until after I posted that comment. It is now finally working!
-
user9869932 almost 9 yearsWith 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
-
jotadepicas over 7 yearsHmm, no, I think this only installs
googlemock
, but it does not installgoogletest
(gtest). At least that's what happened to me. -
Alexander Zinovyev over 6 yearsActually 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/
-
m4l490n about 6 years@AlexanderZinovyev I get "make: *** No rule to make target 'install'. Stop." when I execute the "sudo make install"
-
Nubcake about 6 yearsHave the files been renamed in the 1.8.0 version? There is no
include/gtest
as far as I can tell. -
Ahmed Nassar almost 6 years"sudo make install" worked on Ubuntu 18.04, but didn't work on Ubuntu 16.04.
-
amritkrs almost 6 years@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
-
Schütze over 5 yearsYour 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. -
Carlo Wood about 5 yearsI'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?
-
ntg about 5 yearsIts 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.
-
lingjiankong almost 5 yearsThere is no need to manually
sudo cp *.a /usr/lib
, just repalce it withsudo make install
should be fine. -
Ibrahim Mohamed over 4 yearsI had to compile with
-lgtest -lgtest_main -lpthread
to make it work. -
Gino Mempin almost 4 yearsIf linking against gtest_main (
lgtest_main
), there is no need to define your ownmain
in the test file. -
Maxxik CZ over 3 yearsyour solution didn't work for me. The correct output of
ldconfig
didn't show up.sudo make instal
worked for me. -
jaques-sam about 2 yearsJust run
make install
from 1.11 onward, this works on >=debian10