gtest 编译安装

第一步:获取源码

git clone https://github.com/google/googletest.git

第二步:安装cmake工具

yum install cmake

第三步:配置

cd googletest

配置工程:cmake .

编译:生成静态库

[root@xuperior-pc googletest]# make

[ 25%] Linking CXX static library ../lib/libgtest.a
[ 50%] Linking CXX static library ../lib/libgmock.a
[ 75%] Linking CXX static library ../lib/libgmock_main.a
[100%] Linking CXX static library ../lib/libgtest_main.a

编译例子testcase:

cd googletest/samples/

g++ sample1.cc sample1_unittest.cc -lgtest -lgtest_main -lpthread -o test1 -L/home/xuperior/code/googletest/lib -I/home/xuperior/code/googletest/googletest/include

wait for a moment ......

./test1 

[root@xuperior-pc samples]# ./test1
Running main() from /home/xuperior/code/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.

Done!

ps : 如果你存在多个版本gcc,可能需要添加对应的libstdc++路径到 LD_LIBRARY_PATH, 不然会提示c++库版低,用例无法运行。

 

posted @ 2021-03-18 21:39  Xuperior  阅读(251)  评论(0)    收藏  举报