简体   繁体   English

如何在 Linux 上将 googleTest 设置为共享库

[英]How to set up googleTest as a shared library on Linux

Debian does not provide any precompiled packages for gTest anymore. Debian 不再为 gTest 提供任何预编译包。 They suggest you integrate the framework into your project's makefile. But I want to keep my makefile clean.他们建议您将框架集成到项目的 makefile 中。但我想保持我的 makefile 干净。 How do I set up gTest like the former versions (<1.6.0), so that I can link against the library?如何像以前的版本(<1.6.0)一样设置 gTest,以便我可以链接到库?

Before you start make sure your have read and understood this note from Google !在开始之前,请确保您已阅读并理解Google 的这篇笔记 This tutorial makes using gtest easy, but may introduce nasty bugs .本教程使 gtest 的使用变得简单,但可能会引入令人讨厌的错误

1. Get the googletest framework 1.获取googletest框架

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 2. 解压并构建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. 3. 在您的系统上“安装”头文件和库。

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.我通过检查Debian 以前的 gtest 库所在的位置来实现这一点。 But I'm sure there are better ways to do this.但我相信有更好的方法来做到这一点。 Note: make install is dangerous and not supported注意: make install是危险的,不受支持

sudo cp -a googletest/include/gtest /usr/include
sudo cp -a googlemock/gtest/libgtest_main.so googlemock/gtest/libgtest.so /usr/lib/

4. Update the cache of the linker 4.更新链接器的缓存

... and check if the GNU Linker knows the libs ...并检查GNU链接器是否知道库

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. gTestframework 现在可以使用了。 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.只是不要忘记通过将-lgtest设置为链接器标志来将您的项目链接到库,如果您没有编写自己的测试主例程,则可以选择显式-lgtest_main标志。

From here on you might want to go to Googles documentation , and the old docs about the framework to learn how it works.从这里开始,您可能需要访问 Google 文档和有关该框架的旧文档以了解其工作原理。 Happy coding!快乐编码!

Edit: This works for OS X too!编辑:这也适用于 OS X! See "How to properly setup googleTest on OS X"请参阅“如何在 OS X 上正确设置 googleTest”

Let me answer this specifically for ubuntu users.让我专门为 ubuntu 用户回答这个问题。 First start by installing the gtest development package.首先安装 gtest 开发包。

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.这些源文件应该位于 /usr/src/gtest。 Browse to this folder and use cmake to compile the library:浏览到此文件夹并使用 cmake 编译库:

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:现在要编译使用 gtest 的程序,您必须将其链接到:

-lgtest -lgtest_main -lpthread

This worked perfectly for me on Ubuntu 14.04LTS.这在 Ubuntu 14.04LTS 上非常适合我。

It took me a while to figure out this because the normal "make install" has been removed and I don't use cmake.我花了一段时间才弄清楚这一点,因为正常的“make install”已被删除,而且我不使用 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/ .在工作中,我在 Linux 上没有 root 访问权限,所以我在我的主目录下安装了 Google 测试框架: ~/usr/gtest/

To install the package in ~/usr/gtest/ as shared libraries, together with sample build as well:要将软件包安装在 ~/usr/gtest/ 中作为共享库,以及示例构建:

$ 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:要验证安装,请使用以下 test.c 作为简单的测试示例:

    #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 

If you happen to be using CMake, you can use ExternalProject_Add as described here .如果你碰巧使用CMake的,你可以使用ExternalProject_Add描述这里

This avoids you having to keep gtest source code in your repository, or installing it anywhere.这避免了您必须将 gtest 源代码保存在您的存储库中,或将其安装在任何地方。 It is downloaded and built in your build tree automatically.它会自动下载并构建在您的构建树中。

Update for Debian/Ubuntu Debian/Ubuntu 更新

Google Mock (package: google-mock ) and Google Test (package: libgtest-dev ) have been merged. Google Mock(包: google-mock )和 Google Test(包: libgtest-dev )已合并。 The new package is called googletest .新包名为googletest Both old names are still available for backwards compatibility and now depend on the new package googletest .这两个旧名称仍可用于向后兼容,现在依赖于新包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 .之后,您可以链接到-lgmock (或链接-lgmock_main如果您不使用自定义 main 方法)和-lpthread This was sufficient for using Google Test in my cases at least.至少在我的情况下,这足以使用 Google Test。

If you want the most current version of Google Test, download it from github.如果您想要最新版本的 Google Test,请从 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 .您可以使用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 .此外,使用此命令时您无法控制目标位置,并且您可能不想污染/usr/lib

I was similarly underwhelmed by this situation and ended up making my own Ubuntu source packages for this.我同样对这种情况感到不知所措,最终为此制作了自己的 Ubuntu 源代码包。 These source packages allow you to easily produce a binary package.这些源包允许您轻松生成二进制包。 They are based on the latest gtest & gmock source as of this post.它们基于本文中最新的 gtest 和 gmock 源。

Google Test DEB Source Package谷歌测试 DEB 源码包

Google Mock DEB Source Package Google Mock DEB 源码包

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.它可能会告诉您需要一些先决条件包,在这种情况下,您只需要 apt-get 安装它们。 Apart from that, the built .deb binary packages should then be sitting in the parent directory.除此之外,构建的 .deb 二进制包应该位于父目录中。

For GMock, the process is the same.对于 GMock,过程是一样的。

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.作为旁注,虽然不是特定于我的源包,但在将 gtest 链接到您的单元测试时,请确保首先包含 gtest ( https://bbs.archlinux.org/viewtopic.php?id=156639 ) 这似乎是一个常见问题。

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:以防万一其他人昨天(2016 年 6 月 22 日)遇到和我一样的情况,并且使用已经发布的方法也没有成功 - 在Lubuntu 14.04它使用以下命令链对我Lubuntu 14.04

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.来自 askubuntu 的这个答案对我有用。 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似乎比其他选项更简单,更不容易出错,因为它使用包libgtest-dev从那里获取源和构建: https : libgtest-dev -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.之后,我可以gtest构建依赖于gtest项目。

The following method avoids manually messing with the /usr/lib directory while also requiring minimal change in your CMakeLists.txt file.以下方法避免了手动弄乱/usr/lib目录,同时还需要对CMakeLists.txt文件进行最少的更改。 It also lets your package manager cleanly uninstall libgtest-dev .它还可以让您的包管理器干净地卸载libgtest-dev

The idea is that when you get the libgtest-dev package via这个想法是,当你得到libgtest-dev通过包

sudo apt install libgtest-dev

The source is stored in location /usr/src/googletest源存储在位置/usr/src/googletest

You can simply point your CMakeLists.txt to that directory so that it can find the necessary dependencies您可以简单地将CMakeLists.txt指向该目录,以便它可以找到必要的依赖项

Simply replace FindGTest with add_subdirectory(/usr/src/googletest gtest)只需更换FindGTestadd_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:这将在基于 Ubuntu/Debian 的系统中安装 google 测试和模拟库:

sudo apt-get install google-mock

Tested in google cloud in debian based image.在基于 debian 的图像中在谷歌云中测试。

This will build and install both gtest and gmock 1.7.0:这将构建并安装 gtest 和 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:使用busterbullseye可以安装以下三个包而无需编译任何东西:

sudo apt-get install libgtest-dev libgmock-dev googletest

This includes gmock.这包括 gmock。

For 1.8.1 based on @ManuelSchneid3r 's answer I had to do:对于基于@ManuelSchneid3r 的回答的 1.8.1,我必须这样做:

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:然后我确实make install似乎适用于 1.8.1,但遵循@ManuelSchneid3r 意味着:

sudo cp -a googletest/include/gtest /usr/include
sudo cp -a googlemock/include/gmock /usr/include
sudo cp `find .|grep .so$` /usr/lib/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM