Googletest (gtest)似乎是一个非常流行的单元测试框架,我想学习如何在g++编译器上独立构建它,这样我就可以用它来测试小型库和一次性文件。
我在这里阅读了正式文档和重新阅读:
...but,我还是搞不明白。
如何使用gtest构建和测试?使用gcc/g++编译器,或g++兼容的LLVM 嘎吱声 编译器?。
我知道我可以使用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发布于 2022-05-04 05:30:01
我终于想出来了!关键的参考是这个,它有一些出色的构建命令示例,我学习了这些示例来解决所有问题:README.html。
以下是几个步骤:
在上进行测试。
我首先在我的主要世界自述文件( 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.a2.建立并运行googletest附带的一些示例。
参见这里的示例测试:https://github.com/google/googletest/tree/main/googletest/samples。
googletest/googletest/samples/sample1_unittest.cc:googletest/googletest/samples/sample1_unittest.cc:time (\ time g++ -Wall -Wextra -Werror -O3 -std=c++17 -pthread \-I)“googletest/googletest/include”googletest/googlemock/include“\g++\googletest/googletest/sample1.cc\ bin/libgtest.a bin/libgtest_main.a \ -o bin/a & time bin/a \)googletest/googletest/samples/sample2_unittest.cc: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”\g++\googletest/googletest/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备注
-I"googletest/googletest"?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 dir的父dir必须是“包含文件夹”。这个父dir是googletest/googletest,所以我们用-I"googletest/googletest"标记它为包含dir。*.o文件,同时在C++ googletest单元测试中包含非名称损坏的标头。快乐的建筑!现在我/我们终于可以轻松地在我们自己的个人项目中使用gtest了!
其他参考资料:
time cmd包装器对大型多行命令的子组件以及整个多行命令:如何在多个命令上运行时并将时间输出写入文件?的时间性。https://stackoverflow.com/questions/72108314
复制相似问题