繁体   English   中英

如何通过 gcc/g++ 或 clang 构建和使用 googletest (gtest) 和 googlemock (gmock)?

[英]How do I build and use googletest (gtest) and googlemock (gmock) with gcc/g++ or clang?

Googletest (gtest)似乎是一个非常流行的单元测试框架,我想学习如何在 g++ 编译器上简单而轻松地独立构建它,这样我就可以用它测试小型库和一次性文件。

我在这里阅读了官方文档和自述文件:

  1. https://github.com/google/googletest
  2. 在这里: https://github.com/google/googletest/tree/main/googletest

……但我还是想不通。

我怎样才能使用 gcc/g++ 编译器或兼容 g++ 的 LLVM clang编译器构建和测试 gtest?


我知道我可以执行以下操作来使用 cmake,但它并没有给我所需的粒度控制级别,而且它仍然没有回答神秘的问题“完成后我如何使用这些 static 库文件” ?”。

来自: https://github.com/google/googletest/tree/main/googletest#generic-build-instructions

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.

time make            # Run those makefiles just autogenerated by cmake above.

您现在可以使用 cmake 文件中为您预先指定的任何构建设置构建以下 4 个库文件,但我仍然不知道如何使用它们:

googletest/build/lib/libgmock.a
googletest/build/lib/libgmock_main.a
googletest/build/lib/libgtest.a
googletest/build/lib/libgtest_main.a

我终于弄明白了,关键参考是这个:它有一些我研究过的优秀构建命令示例: https://ethz-adrl.github.io/ct/ct_core/doc/html/md__home_adrl_code_src_control- toolbox_ct_core_build_test_googletest-src_googletest_README.html

以下是步骤:

在 Linux Ubuntu 上测试。

我首先在我的主要C++自述文件中的 eRCaGuy_hello_world 存储库中记录了整个过程,以及更多信息: cpp/README.md

1.将gtest和gmock全部构建为static库存档*.a文件

# Clone the repo
git clone https://github.com/google/googletest.git

# Build all of gtest and gmock as static library archive `*.a` files

time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread -c \
    -I"googletest/googletest/include" -I"googletest/googletest" \
    -I"googletest/googlemock/include" -I"googletest/googlemock" \
    googletest/googletest/src/gtest-all.cc \
    googletest/googletest/src/gtest_main.cc \
    googletest/googlemock/src/gmock-all.cc \
    googletest/googlemock/src/gmock_main.cc

# move all of the object files just created to a "bin" dir
mkdir -p bin
mv -t bin gtest-all.o gtest_main.o gmock-all.o gmock_main.o

# Use the `ar` "archive" utility to create the *.a static library archive files
# from the 4 object files above
time ar -rv bin/libgtest.a bin/gtest-all.o
time ar -rv bin/libgtest_main.a bin/gtest_main.o
time ar -rv bin/libgmock.a bin/gmock-all.o
time ar -rv bin/libgmock_main.a bin/gmock_main.o

你现在有:

bin/libgtest.a
bin/libgtest_main.a
bin/libgmock.a
bin/libgmock_main.a

2.构建并运行googletest附带的一些示例

请在此处查看这些示例测试: https://github.com/google/googletest/tree/main/googletest/samples

  1. 对于googletest/googletest/samples/sample1_unittest.cc
     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 \ )
  2. 对于googletest/googletest/samples/sample2_unittest.cc
     time ( \ time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread \ -I"googletest/googletest/include" -I"googletest/googlemock/include" \ googletest/googletest/samples/sample2_unittest.cc \ googletest/googletest/samples/sample2.cc \ bin/libgtest.a bin/libgtest_main.a \ -o bin/a \ && time bin/a \ )

等等

上面构建sample1_unittest.cc的示例构建和运行命令和 output:

eRCaGuy_hello_world/cpp$ 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 \
> )

real    0m1.787s
user    0m1.375s
sys 0m0.165s
Running main() from 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

real    0m1.790s
user    0m1.375s
sys 0m0.166s

笔记

  1. Q:为什么构建googletest库时需要include dir -I"googletest/googletest"
    1. A:因为googletest/googletest/src/gtest-all.cc包括所有其他源文件作为src/name_of_file.cc ,这里: https://github.com/google/googletest/blob/main/googletest/src/gtest- all.cc#L41-L49 这意味着包含src目录的父目录必须是一个“包含文件夹”。 该父目录是googletest/googletest ,因此我们将其标记为带有-I"googletest/googletest"的包含目录。
  2. 您也可以使用 gtest 测试 C 代码,在 C++ 中包含 C 标头时使用extern "C" { }技巧来防止名称混淆。 然后链接到 C 构建的 object *.o文件,同时在 C++ googletest 单元测试中包含非名称损坏的标头。

快乐的建筑! 现在我/我们终于可以在自己的个人项目中轻松使用gtest了!

其他参考:

  1. 我自己的答案,我在其中找出time cmd 包装器来为更大的多行命令的子组件计时,以及整个多行命令: How to run time on multiple commands AND write the time output to file?

暂无
暂无

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

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