如何编译google test的例子?

本文详细介绍了如何编译和运行Google Test源码中的样例。首先通过在make目录下执行`make`命令编译sample1_unittest,并展示了其运行结果。接着,利用cmake构建所有样本,包括生成Makefile并执行`make`,从而编译gtest_unittest及所有样例程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本博客 http://blog.youkuaiyun.com/livelylittlefish 贴出作者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!

How to build samples of Google Test?

 

In the source of google test, it provides some samples to show how to use google test. In this article, we describe how to build these samples.

 

1. an example to build a sample

 

The source of google test provides a sample to build its exmaple. It is in ../make.

This example is to build sample1_unittest, it is in ../samples.

The source code to be tested is in file sample1.cc & sample1.h.

The unit test code is in file sample1_unittest.cc.

# /usr/src/gtest-1.5.0

# cd make

# make

# ./sample1_unittest

The result of build is in ../make, then, we can run sample1_unittest, as follows.

root@yu29:/usr/src/gtest-1.5.0/make# ./sample1_unittest

Running main() from gtest_main.cc

[==========] Running 6 tests from 2 test cases.

[----------] 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 cases ran. (0 ms total)

[  PASSED  ] 6 tests.

2. how to build all samples by using cmake?

 

Cmake generates native build scripts. 先建立mybuild目录

# cd /usr/src/gtest-1.5.0

# mddir mybuild

# cd mybuild

(1) only generate gtest_unittest

# cmake /usr/src/gtest-1.5.0  //generate Makefile

# make                        //generate executable file gtest_unittest

(2) generate gtest_unittest and all samples

//generate Makefile with Google Test's samples to be compiled

# cmake -Dbuild_gtest_samples=ON /usr/src/gtest-1.5.0


//generate executable files gtest_unittest, sample1_unittest, sample2_unittest, ...

# make

Reference

http://code.google.com/p/googletest

../readme

 

 

Appendix. 使用CMake编译gtest_unittestsamples

 

//生成Makefile文件

root@yu29:/usr/src/gtest-1.5.0/mybuild# cmake -Dbuild_gtest_samples=ON /usr/src/gtest-1.5.0

-- The CXX compiler identification is GNU

-- The C compiler identification is GNU

-- Check for working CXX compiler: /usr/bin/c++

-- Check for working CXX compiler: /usr/bin/c++ -- works

-- Detecting CXX compiler ABI info

-- Detecting CXX compiler ABI info - done

-- Check for working C compiler: /usr/bin/gcc

-- Check for working C compiler: /usr/bin/gcc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

-- Looking for include files CMAKE_HAVE_PTHREAD_H

-- Looking for include files CMAKE_HAVE_PTHREAD_H - found

-- Looking for pthread_create in pthreads

-- Looking for pthread_create in pthreads - not found

-- Looking for pthread_create in pthread

-- Looking for pthread_create in pthread - found

-- Found Threads: TRUE

-- Found PythonInterp: /usr/bin/python2.6

-- Configuring done

-- Generating done

-- Build files have been written to: /usr/src/gtest-1.5.0/mybuild

root@yu29:/usr/src/gtest-1.5.0/mybuild# ls

CMakeCache.txt  CMakeFiles  cmake_install.cmake  CTestTestfile.cmake  Makefile

//编译

root@yu29:/usr/src/gtest-1.5.0/mybuild# make

Scanning dependencies of target gtest

[  5%] Building CXX object CMakeFiles/gtest.dir/src/gtest-all.cc.o

Linking CXX static library libgtest.a

[  5%] Built target gtest

Scanning dependencies of target gtest_main

[ 11%] Building CXX object CMakeFiles/gtest_main.dir/src/gtest_main.cc.o

...

//生成了所有samples的可执行程序

root@yu29:/usr/src/gtest-1.5.0/mybuild# ls

CMakeCache.txt       libgtest.a         sample2_unittest  sample7_unittest

CMakeFiles           libgtest_main.a    sample3_unittest  sample8_unittest

cmake_install.cmake  Makefile           sample4_unittest  sample9_unittest

CTestTestfile.cmake  sample10_unittest  sample5_unittest

gtest_unittest       sample1_unittest   sample6_unittest

//看看sample2的执行结果

root@yu29:/usr/src/gtest-1.5.0/mybuild# ./sample2_unittest

Running main() from gtest_main.cc

[==========] Running 4 tests from 1 test case.

[----------] Global test environment set-up.

[----------] 4 tests from MyString

[ RUN      ] MyString.DefaultConstructor

[       OK ] MyString.DefaultConstructor (0 ms)

[ RUN      ] MyString.ConstructorFromCString

[       OK ] MyString.ConstructorFromCString (0 ms)

[ RUN      ] MyString.CopyConstructor

[       OK ] MyString.CopyConstructor (0 ms)

[ RUN      ] MyString.Set

[       OK ] MyString.Set (0 ms)

[----------] 4 tests from MyString (0 ms total)

 

[----------] Global test environment tear-down

[==========] 4 tests from 1 test case ran. (1 ms total)

[  PASSED  ] 4 tests.

//gtest_unittest的执行结果较长,此处不再显示。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值