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

How to set up googleTest as a shared library on Linux

本文关键字:设置 共享 googleTest 上将 Linux      更新时间:2023-10-16

Debian 不再为 gTest 提供任何预编译包。他们建议您将框架集成到项目的生成文件中。但我想保持我的制作文件干净。如何像以前的版本(<1.6.0)一样设置 gTest,以便我可以链接到库?

在开始之前,请确保您已阅读并理解 这张来自谷歌的笔记!本教程使使用 gtest 变得容易,但可能会引入令人讨厌的错误。

1. 获取谷歌测试框架

wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz

或者用手拿。我不会维护这个小操作方法,所以如果你偶然发现了它并且链接已经过时,请随时编辑它。

2. 解压缩并构建谷歌测试

tar xf release-1.8.0.tar.gz
cd googletest-release-1.8.0
cmake -DBUILD_SHARED_LIBS=ON .
make

3. 在您的系统上"安装"标头和库。

此步骤可能因发行版而异,因此请确保将标头和库复制到正确的目录中。我通过检查 Debians 以前的 gtest 库的位置来实现这一点。但我相信有更好的方法可以做到这一点。

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. 更新链接器的缓存

。并检查 GNU 链接器是否知道库

sudo ldconfig -v | grep gtest

如果输出如下所示:

libgtest.so.0 -> libgtest.so.0.0.0
libgtest_main.so.0 -> libgtest_main.so.0.0.0

那么一切都很好。

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

从这里开始,你可能想去谷歌的Googletest Primer文档,以及关于框架的旧文档,以了解它是如何工作的。祝您编码愉快!

编辑:这也适用于OS X!请参阅"如何在OS X上正确设置googleTest">

让我专门为 ubuntu 用户回答这个问题。 首先从安装 gtest 开发包开始。

sudo apt-get install libgtest-dev

请注意,此包仅安装源文件。您必须自己编译代码才能创建必要的库文件。这些源文件应位于/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

现在要编译使用 gtest 的程序,您必须将其与以下链接:

-lgtest -lgtest_main -lpthread

这在 Ubuntu 14.04LTS 上对我来说非常有效。

我花了一段时间才弄清楚这一点,因为正常的"make install"已被删除,我不使用 cmake。以下是我的经验分享。在工作中,我在 Linux 上没有 root 访问权限,所以我在我的主目录下安装了 Google 测试框架:~/usr/gtest/

要将软件包安装在 ~/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

若要验证安装,请使用以下 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();
}

要编译:

$ 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 

如果您碰巧正在使用 CMake,则可以按照此处所述使用ExternalProject_Add

这样就避免了您必须将 gtest 源代码保存在存储库中或将其安装在任何地方。 它会自动下载并构建在您的构建树中。

Debian/Ubuntu 更新

Google Mock(包:google-mock)和谷歌测试(包:libgtest-dev)已经合并。新包称为googletest。这两个旧名称仍然可用于向后兼容,现在依赖于新的软件包googletest

因此,要从包存储库中获取库,您可以执行以下操作:

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

之后,您可以针对-lgmock(如果您不使用自定义 main 方法,则针对-lgmock_main)链接并-lpthread.至少在我的情况下,这足以使用谷歌测试。

如果您想要最新版本的Google Test,请从github下载。之后,步骤类似:

git clone https://github.com/google/googletest
cd googletest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo cp lib/*.a /usr/lib

如您所见,创建库的路径已更改。请记住,新路径也可能很快对包存储库有效。

您可以使用sudo make install代替手动复制库。它"当前"有效,但请注意,它在过去并不总是有效。此外,使用此命令时无法控制目标位置,并且可能不希望污染/usr/lib

我同样对这种情况感到不知所措,最终为此制作了自己的 Ubuntu 源代码包。这些源包允许您轻松生成二进制包。它们基于本文发布时最新的gtest和gmock来源。

谷歌测试 DEB 源包

谷歌模拟 DEB 源包

要构建二进制包,请执行以下操作:

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

它可能会告诉您需要一些先决条件的软件包,在这种情况下,您只需要安装它们即可。 除此之外,构建的.deb二进制包应该位于父目录中。

对于GMock来说,过程是一样的。

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

以防万一其他人昨天(2016-06-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

askubuntu的这个答案对我有用。似乎比其他选项更简单,更不容易出错,因为它使用包libgtest-dev从那里获取源代码和构建:https://askubuntu.com/questions/145887/why-no-library-files-installed-for-google-test?answertab=votes#tab-top

请参考该答案,但作为快捷方式,我也在此处提供步骤:

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/

之后,我可以毫无问题地构建依赖于gtest的项目。

这将构建并安装 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

以下方法避免手动弄乱/usr/lib目录,同时还要求对CMakeLists.txt文件进行最少的更改。 它还允许您的包管理器干净地卸载libgtest-dev

这个想法是,当您通过以下方式获得libgtest-dev包时

sudo apt install libgtest-dev

源存储在位置/usr/src/googletest

您只需将CMakeLists.txt指向该目录,以便它可以找到必要的依赖项

只需将FindGTest替换为add_subdirectory(/usr/src/googletest gtest)

最后,它应该看起来像这样

add_subdirectory(/usr/src/googletest gtest)
target_link_libraries(your_executable gtest)

这将在基于 Ubuntu/Debian 的系统中安装 google 测试和模拟库:

sudo apt-get install google-mock

在谷歌云中基于 debian 的图像中进行了测试。

使用busterbullseye可以安装以下三个软件包而无需编译任何内容:

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

这包括gmock。

看看这里和@amritkrs这里的@ManuelSchneid3r的出色答案,我终于能够(经过 1 年多的努力)整理出一套更新的说明,以及这里任何其他答案都没有提供的其他见解和信息,我一直试图弄清楚 5 年。

这包括很多关于 gcc/g++ 库、安装库、命名 .a 静态链接库文件、g++ 搜索包含等。

如何在Linux/Unix上将Google Test(gtest)和Google Mock(gmock)作为共享的静态.a库安装在Linux/Unix上

在 Ubuntu 20.04 上使用最新(未发布,git pulled)版本的 googletest(比 v1.13.0 更新)进行测试。

如果赶时间,只需运行安装部分中的几个命令,然后查看不久之后的"示例用法"。

注意:谷歌测试在过去几年中经历了各种变化。例如,gtest 和 gmock 现在打包在同一个存储库下,如下所示:https://github.com/google/googletest。因此,按照我的说明将为您安装两者

1. 从源代码安装最新的 gtest 和 gmock

如果您想要最新发布的版本,请在此处找到:https://github.com/google/googletest/releases。

您可以使用这样的wget下载它,例如:

# Download release v1.13.0
wget https://github.com/google/googletest/archive/refs/tags/v1.13.0.tar.gz

但是,我更喜欢从存储库本身获取最新内容。以下是我的完整说明,这样做:

sudo apt update
sudo apt install cmake
# You can find some of these instructions, here: 
# https://github.com/google/googletest/tree/main/googletest
time git clone https://github.com/google/googletest.git
cd googletest        # "Main directory of the cloned repository."
mkdir build          # "Create a directory to hold the build output."
cd build
time cmake ..        # "Generate native make build scripts for GoogleTest."
# Takes ~2 seconds.
time make            # Run those makefiles just autogenerated by cmake above.
# Takes ~10 seconds.
sudo make install    # Install the .a library files, and headers, into 
# /user/local/.

做!

您现在可以将必要的标头包含在文件中,如上所示。

注意:这是运行sudo make install时复制或"安装"到/usr/local中的完整输出:

googletest/build$ sudo make install
[ 25%] Built target gtest
[ 50%] Built target gmock
[ 75%] Built target gmock_main
[100%] Built target gtest_main
Install the project...
-- Install configuration: ""
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/gmock
-- Installing: /usr/local/include/gmock/gmock.h
-- Installing: /usr/local/include/gmock/gmock-actions.h
-- Installing: /usr/local/include/gmock/gmock-more-matchers.h
-- Installing: /usr/local/include/gmock/gmock-spec-builders.h
-- Installing: /usr/local/include/gmock/gmock-function-mocker.h
-- Installing: /usr/local/include/gmock/internal
-- Installing: /usr/local/include/gmock/internal/gmock-pp.h
-- Installing: /usr/local/include/gmock/internal/gmock-internal-utils.h
-- Installing: /usr/local/include/gmock/internal/gmock-port.h
-- Installing: /usr/local/include/gmock/internal/custom
-- Installing: /usr/local/include/gmock/internal/custom/gmock-port.h
-- Installing: /usr/local/include/gmock/internal/custom/gmock-matchers.h
-- Installing: /usr/local/include/gmock/internal/custom/gmock-generated-actions.h
-- Installing: /usr/local/include/gmock/internal/custom/README.md
-- Installing: /usr/local/include/gmock/gmock-cardinalities.h
-- Installing: /usr/local/include/gmock/gmock-more-actions.h
-- Installing: /usr/local/include/gmock/gmock-matchers.h
-- Installing: /usr/local/include/gmock/gmock-nice-strict.h
-- Installing: /usr/local/lib/libgmock.a
-- Installing: /usr/local/lib/libgmock_main.a
-- Installing: /usr/local/lib/pkgconfig/gmock.pc
-- Installing: /usr/local/lib/pkgconfig/gmock_main.pc
-- Installing: /usr/local/lib/cmake/GTest/GTestTargets.cmake
-- Installing: /usr/local/lib/cmake/GTest/GTestTargets-noconfig.cmake
-- Installing: /usr/local/lib/cmake/GTest/GTestConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/GTest/GTestConfig.cmake
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/gtest
-- Installing: /usr/local/include/gtest/gtest-param-test.h
-- Installing: /usr/local/include/gtest/gtest-printers.h
-- Installing: /usr/local/include/gtest/gtest-test-part.h
-- Installing: /usr/local/include/gtest/internal
-- Installing: /usr/local/include/gtest/internal/gtest-port.h
-- Installing: /usr/local/include/gtest/internal/gtest-death-test-internal.h
-- Installing: /usr/local/include/gtest/internal/gtest-filepath.h
-- Installing: /usr/local/include/gtest/internal/custom
-- Installing: /usr/local/include/gtest/internal/custom/gtest-printers.h
-- Installing: /usr/local/include/gtest/internal/custom/gtest-port.h
-- Installing: /usr/local/include/gtest/internal/custom/gtest.h
-- Installing: /usr/local/include/gtest/internal/custom/README.md
-- Installing: /usr/local/include/gtest/internal/gtest-string.h
-- Installing: /usr/local/include/gtest/internal/gtest-type-util.h
-- Installing: /usr/local/include/gtest/internal/gtest-param-util.h
-- Installing: /usr/local/include/gtest/internal/gtest-port-arch.h
-- Installing: /usr/local/include/gtest/internal/gtest-internal.h
-- Installing: /usr/local/include/gtest/gtest_pred_impl.h
-- Installing: /usr/local/include/gtest/gtest-assertion-result.h
-- Installing: /usr/local/include/gtest/gtest-typed-test.h
-- Installing: /usr/local/include/gtest/gtest-spi.h
-- Installing: /usr/local/include/gtest/gtest_prod.h
-- Installing: /usr/local/include/gtest/gtest.h
-- Installing: /usr/local/include/gtest/gtest-message.h
-- Installing: /usr/local/include/gtest/gtest-death-test.h
-- Installing: /usr/local/include/gtest/gtest-matchers.h
-- Installing: /usr/local/lib/libgtest.a
-- Installing: /usr/local/lib/libgtest_main.a
-- Installing: /usr/local/lib/pkgconfig/gtest.pc
-- Installing: /usr/local/lib/pkgconfig/gtest_main.pc

2.背景:以上安装说明仅:

  1. 安装了 gtest 和 gmock*.a静态库文件到/usr/local/lib中,以便您可以使用这些简单的g++构建标志(由链接器使用,ld)构建和运行 gtest 和 gmock 程序:

    1. -lgtest链接到/usr/local/lib/libgtest.a静态库文件,该文件提供常规的googletest功能。
    2. -lgtest_main链接到/usr/local/lib/libgtest_main.a静态库文件,该文件提供了 googletest 所需的默认main()函数。
    3. -lgmock链接到/usr/local/lib/libgmock.a静态库文件,该文件提供了一般的googlemock功能。
    4. -lgmock_main链接到/usr/local/lib/libgmock_main.a静态库文件,该文件提供了googlemock所需的默认main()函数。
    5. -pthread谷歌测试需要,因为它在引擎盖下使用 POSIX 线程。

    笔记:

    1. 顺序很重要。确保上面的标志位于需要它们的文件之后
    2. 如果要设置必要的 CMake 定义来构建共享的动态链接的 .so 库,而不是我们正在构建的静态 .a 库,则上面的-l标志将导致运行时链接器在运行时链接到这些库。有关详细信息,请参阅 @amritkrs 的答案。关于这一点。但是,按照我的说明,将导致这些相同的-l标志在构建时链接,将整个预构建的 .a 静态库文件二进制文件放入可执行文件中。
  2. 已将需要包含在程序中的所有 gtest 和 gmock 头文件分别安装到/usr/local/include/gtest/usr/local/include/gmock中。

    这允许您在程序中包含必要的文件,如下所示:

    // Include in your unit tests
    #include <gtest/gtest.h>
    #include <gmock/gmock.h>
    // Include in your production code to give gtest access to private members
    // in your classes, as friends, via the 
    // `FRIEND_TEST(TestSuiteName, TestName);` macro, for instance.
    #include <gtest/gtest_prod.h>
    

3.用法示例:

# Build and run an example googletest unit test that comes in the repo:
# - required in this case: `-pthread`, `-lgtest`, and `-lgtest_main`
mkdir -p bin
time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread 
googletest/googletest/samples/sample1_unittest.cc 
googletest/googletest/samples/sample1.cc 
-lgtest -lgtest_main -o bin/a && time bin/a

4. 运行示例单元测试

手动测试生成并运行存储库中包含的示例测试:

# (run while still in the googletest/build dir)
time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread 
../googletest/samples/sample1_unittest.cc 
../googletest/samples/sample1.cc 
-lgtest -lgtest_main -o bin/a && time bin/a

示例运行和输出:

googletest/build$ time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread 
>     ../googletest/samples/sample1_unittest.cc 
>     ../googletest/samples/sample1.cc 
>     -lgtest -lgtest_main -o bin/a && time bin/a
real    0m1.879s
user    0m1.740s
sys 0m0.135s
Running main() from /home/gabriel/GS/dev/temp/googletest/googletest/src/gtest_main.cc
[==========] Running 6 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)
[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)
[----------] Global test environment tear-down
[==========] 6 tests from 2 test suites ran. (0 ms total)
[  PASSED  ] 6 tests.
real    0m0.003s
user    0m0.000s
sys 0m0.002s

5. 卸载

要删除/卸载 gtest 和 gmock,只需手动删除sudo make install复制的所有文件。野兔是执行此操作的命令:

# remove the main libraries
sudo rm /usr/local/lib/libgtest.a 
/usr/local/lib/libgtest_main.a 
/usr/local/lib/libgmock.a 
/usr/local/lib/libgmock_main.a
# remove the include dirs
sudo rm -r /usr/local/include/gtest 
/usr/local/include/gmock
# extras
# remove the package config (whatever that is)
sudo rm /usr/local/lib/pkgconfig/gtest.pc 
/usr/local/lib/pkgconfig/gtest_main.pc 
/usr/local/lib/pkgconfig/gmock.pc 
/usr/local/lib/pkgconfig/gmock_main.pc
# remove cmake GTest stuff
sudo rm -r /usr/local/lib/cmake/GTest

更进一步:关于库的一般信息;调试;gcc/g++ 编译器标志;等等。

  1. 编译时,g++在包含目录中搜索代码包含的所有头文件。系统范围的包含路径的默认C++可以通过运行$(gcc -print-prog-name=cc1plus) -v然后在一秒钟后按 Ctrl + C 杀死它来找到(请参阅此处:gcc 在哪里查找 C 和C++头文件?它们主要包括:

    /usr/include        # system-added includes
    /usr/local/include  # user-added includes
    # as well as some more include paths to pull in compiler includes
    

    包含目录还包括通过-I标志传递给g++的任何目录,例如-I"path/to/some/personal/include_dir1" -I"path/to/include_dir2"等。

    将 gtest 的包含直接存储在/usr/local/include中意味着您永远不必传递自定义-I标志来指定它们的位置。编译器现在总是有可用的!

  2. 与上述"包含"概念类似,库文件(例如静态链接的 .a 文件(或动态链接的 .so 文件))可以通过将它们作为输入传递给可执行文件来链接到可执行文件g++

    示例:从我在这里的回答来看,此命令手动指定gtestgmock包含目录以及预构建的 .a 文件,并构建googletest/googletest/samples/sample1_unittest.cc示例程序:

    # Note: the specified .a files here must actually be locally stored in
    # a local "bin" path, as specified in this command
    time ( 
    time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread 
    -I"googletest/googletest/include" -I"googletest/googlemock/include" 
    googletest/googletest/samples/sample1_unittest.cc 
    googletest/googletest/samples/sample1.cc 
    bin/libgtest.a bin/libgtest_main.a 
    -o bin/a 
    && time bin/a 
    )
    

    或者,通过将包含复制到/usr/local/include并将 .a 静态库文件复制到/usr/local/lib,您可以大大简化该命令并改为运行此命令!包含目录根本不需要指定,您只需分别传递-lgtest-lgtest_main,而不是传递path/to/libgtest.apath/to/libgtest_main.a

    time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread 
    googletest/googletest/samples/sample1_unittest.cc 
    googletest/googletest/samples/sample1.cc 
    -lgtest -lgtest_main -o bin/a && time bin/a
    
  3. 如果需要,您可以通过像这样复制头文件和库
  4. 手动安装头文件和库,而不是运行sudo make install将 gtest 和 gmock 标头和库分别安装到/usr/local/include/usr/local/lib中:

    # manually install the .a libraries, and header files
    # give access to g++ flags `-lgtest`, `-lgtest_main`, `-lgmock`, and
    # `-lgmock_main`, by copying over the .a static library files
    sudo cp -i -t /usr/local/lib 
    lib/libgtest.a lib/libgtest_main.a lib/libgmock.a lib/libgmock_main.a
    # give access to include header files `<gtest/gmock.h>` `<gtest/gtest.h>`, 
    # `<gtest/gtest_prod.h>`, etc.
    # 1. gtest header file includes
    sudo cp -a path/to/googletest/googletest/include/gtest /usr/local/include
    # 2. gmock header file includes
    sudo cp -a path/to/googletest/googlemock/include/gmock /usr/local/include 
    

    我第一次了解到这个概念是在@ManuelSchneid3r的回答中。我自己测试了一下。

  5. 如果没有安装头文件或为其指定-I路径,您将看到包含所需标头的失败,例如此处fatal error: gtest/gtest.h: No such file or directory

    eRCaGuy_hello_world/cpp$ time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread  googletest/googletest/samples/sample1_unittest.cc     googletest/googletest/samples/sample1.cc     -lgtest -lgtest_main     -o bin/a      && time bin/a
    googletest/googletest/samples/sample1_unittest.cc:46:10: fatal error: gtest/gtest.h: No such file or directory
    46 | #include "gtest/gtest.h"
    |          ^~~~~~~~~~~~~~~
    compilation terminated.
    real    0m0.046s
    user    0m0.039s
    sys 0m0.007s
    
  6. 如果没有安装库(.a 或 .so 文件),并且如果您尝试使用-lgtest而不是直接传递path/to/libgtest.a,您将看到通过ld链接器链接失败,例如此处cannot find -lgtest

    eRCaGuy_hello_world/cpp$ time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread  googletest/googletest/samples/sample1_unittest.cc     googletest/googletest/samples/sample1.cc     -lgtest -lgtest_main     -o bin/a      && time bin/a
    /usr/bin/ld: cannot find -lgtest
    /usr/bin/ld: cannot find -lgtest_main
    collect2: error: ld returned 1 exit status
    real    0m1.895s
    user    0m1.776s
    sys 0m0.119s
    
  7. 如果将库文件复制到已知的g++系统范围的库路径,例如/usr/local/lib(建议用于用户安装的 .a 和 .so 库)或/usr/lib(对于系统安装的 .a 和 .so 库),则 .a 文件必须命名为libwhatever.a,例如,才能用作-lwhatever

    你不能称它们为lwhatever.alibwhatever。它们必须命名为libwhatever.a

    因此,具有文件/usr/local/lib/libgtest.a启用-lgtest库链接器标志,但/usr/local/lib/lgtest.a不会。

    如果您的名字错误,然后尝试使用-lgtest标志,您将看到/usr/bin/ld: cannot find -lgtest链接器错误,如上所示,因为此库未正确命名和"安装"。

    同样,这意味着如果您将其称为/usr/local/lib/libwhatever.a,则必须使用-lwhatever作为该库的链接器标志。

    请注意,根据ld --help-l(小写"L")标志显然被传递给ld链接器,大概l代表library。在这里看我的问题:gcc/g++ 中-l(小写"L")标志的含义。从ld --help

    -l LIBNAME, --library LIBNAME  
    Search for library LIBNAME
    
  8. 查看/usr/local/lib/usr/lib中的库文件,库的链接器命名约定,无论是静态 .a 还是动态 .so 文件,似乎都要求库文件名必须lib为前缀!

  9. Gtest 在引擎盖下使用 POSIX 线程(pthreads),因此您也必须始终将-pthread标志传递给g++

  10. 谷歌搜索g++ "-lpthread" vs "-pthread"揭示了这个答案:编译时-pthread-lpthread的区别,它说-lpthread-pthread之间的差异是历史性的,所以在今天的 gcc/g++ 和 clang 编译器上,你应该总是只使用-pthread来引入 POSIX 线程。

    -lpthread库现在显然是一个空的二进制文件,除了满足古老的要求之外,什么都不做,因为该库仍然必须包含在某些地方。但是,-pthread为您处理这一切,所以只需单独使用-pthread即可完成!

那大约涵盖了它。我现在对 g++、库和 gtest 有了更多的了解。

引用:

  1. @ManuelSchneid3r回答
  2. @amritkrs回答
  3. https://github.com/google/googletest
    1. https://github.com/google/googletest/tree/main/googletest
  4. 我的问答:如何构建和使用 googletest (gtest) 和 googlemock (gmock) 与 gcc/g++ 或 clang?
  5. MY Q:gcc/g++ 中-l(小写"L")标志的含义

对于 1.8.1 基于 @ManuelSchneid3r 的答案 我不得不做:

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

然后我做了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/