Linux下编译googletest

56 篇文章 3 订阅
5 篇文章 0 订阅

下载

下载地址为https://github.com/google/googletest/tags,以版本release-1.10.0tar.gz包为例,下载完成后的文件为 googletest-release-1.10.0.tar.gz

安装

googletest使用CMake来进行构建。

使用命令tar -zxvf googletest-release-1.10.0.tar.gz解压缩,解压缩完成后,目录googletest-release-1.10.0结构如下:

[root@instance-1apocjsh googletest-release-1.10.0]# tree -L 1 -C
.
├── appveyor.yml
├── BUILD.bazel
├── ci
├── CMakeLists.txt
├── CONTRIBUTING.md
├── googlemock
├── googletest
├── library.json
├── LICENSE
├── platformio.ini
├── README.md
└── WORKSPACE

使用命令mkdir build创建构建目录,然后进入build(命令cd build),使用命令build ../googletest构建Makefile

[root@instance-1apocjsh build]# cmake ../googletest/
CMake Warning at CMakeLists.txt:57 (project):
  VERSION keyword not followed by a value or was followed by a value that
  expanded to nothing.

-- The CXX compiler identification is GNU 4.8.5
-- The C compiler identification is GNU 4.8.5
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Deprecation Warning at CMakeLists.txt:59 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.

-- Found PythonInterp: /usr/bin/python (found version "2.7.5") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- 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  
-- Configuring done
-- Generating done
-- Build files have been written to: /root/googletest-release-1.10.0/build

然后使用命令make -j8进行编译。

[root@instance-1apocjsh build]# make -j8
[ 25%] Building CXX object CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 50%] Linking CXX static library lib/libgtest.a
[ 50%] Built target gtest
[ 75%] Building CXX object CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library lib/libgtest_main.a
[100%] Built target gtest_main

注意,如果编译有报错,需要指定使用C++11语法进行编译,
修改googletest/CMakeLists.txt,在其中添加使用C++11语法的命令set (CMAKE_CXX_STANDARD 11),再重新进行操作。

编译完成后,生成的静态库文件位于build/lib中,头文件位于googletest/include中。
将静态库和头文件一起整理放到新文件夹googletest中,方便使用。googletest目录结构如下:

[root@instance-1apocjsh googletest]# tree
.
├── include
│   └── gtest
│       ├── gtest-death-test.h
│       ├── gtest.h
│       ├── gtest-matchers.h
│       ├── gtest-message.h
│       ├── gtest-param-test.h
│       ├── gtest_pred_impl.h
│       ├── gtest-printers.h
│       ├── gtest_prod.h
│       ├── gtest-spi.h
│       ├── gtest-test-part.h
│       ├── gtest-typed-test.h
│       └── internal
│           ├── custom
│           │   ├── gtest.h
│           │   ├── gtest-port.h
│           │   ├── gtest-printers.h
│           │   └── README.md
│           ├── gtest-death-test-internal.h
│           ├── gtest-filepath.h
│           ├── gtest-internal.h
│           ├── gtest-param-util.h
│           ├── gtest-port-arch.h
│           ├── gtest-port.h
│           ├── gtest-string.h
│           ├── gtest-type-util.h
│           └── gtest-type-util.h.pump
└── lib
    ├── libgtest.a
    └── libgtest_main.a

5 directories, 26 files

参考

googletest安装与使用
GoogleTest测试框架介绍(一)
GoogleTest测试框架介绍(二)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值