c++ 我如何用gcc/g++或clang来构建和使用googletest(gtest)和googleock(gmock)?

bhmjp9jg  于 2023-03-14  发布在  Go
关注(0)|答案(2)|浏览(256)

Googletest (gtest)似乎是一个非常受欢迎的单元测试框架,我想学习如何在g++编译器上简单轻松地独立构建它,这样我就可以用它测试小型库和一次性文件。
我已经阅读了官方文档和自述文件:

  1. https://github.com/google/googletest
    1.这里是:https://github.com/google/googletest/tree/main/googletest
    ......但我还是想不明白。

如何使用gcc/g编译器或与g兼容的LLVM clang编译器通过gtest进行构建和测试?

我知道我可以做以下事情来使用cmake,但是它没有给予我想要的粒度控制级别,而且它仍然没有回答“完成后我如何使用这些.a静态库文件?"的神秘问题。
来自: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
vyu0f0g1

vyu0f0g11#

一年后我的全新答案,请看这里:The "easy" way: install gtest's headers and .a static library files system-wide into /usr/local/include and /usr/local/lib , respectively

“硬”方法:直接使用g++out 构建系统,从头开始手动构建所有内容

我终于想通了!关键的参考资料是这一篇,里面有一些我研究过的优秀的构建命令示例来弄清楚这一切: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上测试。
我首先在我的eRCaGuy_hello_world repo主C++自述文件中记录了这整个过程以及更多内容,如下所示:cpp/README.md .

1.将所有gtest和gmock构建为静态库归档*.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 \
)

1.对于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的示例构建和运行命令及输出:

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.问:为什么在构建googletest库时需要include dir -I"googletest/googletest"
1.答:因为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"将其标记为包含目录。
1.你也可以用gtest测试C代码,当在C中包含C头文件时使用extern "C" { }技巧来防止名称损坏,然后链接到C构建的对象*.o文件,同时在C googletest单元测试中包含非名称损坏的头文件。
快乐构建!现在我/我们终于可以在我们自己的个人项目中轻松使用gtest了!

其他参考文献:

1.我自己的答案,我计算出了time cmd Package 器的事情,以计时一个更大的多行命令的子组件,以及整个多行命令:How to run time on multiple commands AND write the time output to file?

xuo3flqw

xuo3flqw2#

简单的方法:分别在/usr/local/include/usr/local/lib中安装gtest的头文件和.a静态库文件

又经过了一年的努力,总共花了5年时间,我终于找到了“简单”的方法。
我最初只是想知道运行什么g++命令来构建我自己的单元测试文件。
1.将gtest和gmock作为静态的.a共享库安装在/usr/local/lib/中,并将它们的头文件安装到/usr/local/include/中。

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/.

1.根据需要使用直接传递给g++-pthread-lgtest-lgtest_main-lgmock-lgmock_main链接器标志。
示例:

# 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

要了解更多细节、更长的解释和更多信息,请参阅我的完整答案:快!
有关-l标志的更多信息,另请参见我的其他回答:Meaning of -l (lowercase "L") flags in gcc/g++

相关问题