如何在 Linux 上将 googleTest 设置为共享库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13513905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up googleTest as a shared library on Linux
提问by ManuelSchneid3r
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?
Debian 不再为 gTest 提供任何预编译包。他们建议您将框架集成到项目的 makefile 中。但我想保持我的 makefile 干净。我如何像以前的版本(<1.6.0)一样设置 gTest,以便我可以链接到库?
采纳答案by ManuelSchneid3r
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.
在开始之前,请确保您已阅读并理解 Google 的这篇笔记!本教程使 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 libswere located. But I'm sure there are better ways to do this. Note: make install
is dangerous and not supported
此步骤可能因发行版而异,因此请确保将标头和库复制到正确的目录中。我通过检查Debian 以前的 gtest 库所在的位置来实现这一点。但我相信有更好的方法来做到这一点。注意:make install
是危险的,不支持
sudo cp -a include/gtest /usr/include
sudo cp -a libgtest_main.so 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
, 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.
gTestframework 现在可以使用了。只是不要忘记通过设置-lgtest
为链接器标志来将您的项目与库链接起来,如果您没有编写自己的测试主程序,则可以选择显式-lgtest_main
标志。
From here on you might want to go to Googles documentation, and the old docsabout the framework to learn how it works. Happy coding!
从这里开始,您可能需要访问 Google文档和有关该框架的旧文档以了解其工作原理。快乐编码!
Edit:This works for OS X too! See "How to properly setup googleTest on OS X"
编辑:这也适用于 OS X!请参阅“如何在 OS X 上正确设置 googleTest”
回答by Fraser
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. It is downloaded and built in your build tree automatically.
这避免了您必须将 gtest 源代码保存在您的存储库中,或将其安装在任何地方。它会自动下载并构建在您的构建树中。
回答by user3061373
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/
.
我花了一段时间才弄清楚这一点,因为正常的“make install”已被删除,而且我不使用 cmake。这里分享一下我的经验。在工作中,我在 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
回答by Nick Weedon
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.
我同样对这种情况感到不知所措,最终为此制作了自己的 Ubuntu 源代码包。这些源包允许您轻松生成二进制包。它们基于本文中最新的 gtest 和 gmock 源。
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.
它可能会告诉您需要一些先决条件包,在这种情况下,您只需要 apt-get 安装它们。除此之外,构建的 .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) 这似乎是一个常见问题。
回答by Tobias Hermann
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
使用以下命令链对我有用:
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
回答by Shubham Chaudhary
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 的图像中在谷歌云中测试。
回答by jotadepicas
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
来自 askubuntu 的这个答案对我有用。似乎比其他选项更简单,更不容易出错,因为它使用包libgtest-dev
从那里获取源和构建: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.
在那之后,我可以构建我的项目,这取决于gtest
没有问题。
回答by Bl00dh0und
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
回答by amritkrs
Let me answer this specifically for ubuntu users. First start by installing the gtest development package.
让我专门为 ubuntu 用户回答这个问题。首先安装 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. Browse to this folder and use cmake to compile the library:
注意这个包只安装源文件。您必须自己编译代码以创建必要的库文件。这些源文件应该位于 /usr/src/gtest。浏览到此文件夹并使用 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 上非常适合我。
回答by ntg
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/