2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Google TestでC++の単体テストをやってみた①

Last updated at Posted at 2022-01-23

はじめに

久しぶりにC++でちょっとしたプログラムを書く機会を得たので、googleのテストフレームワークについて勉強することにした。
CentOS7環境にgoogletestをインストールしようとして、少し詰まったところを備忘録的に。要約すると、

  • gccはversion 5以上でないといけない
  • cmakeする際に、コンパイラ指定とC++11の有効化

が必要だった。結局、今回はインストールするところまで。

インストール

元の環境を利用

  • CentOS Linux release 7.6.1810 (Core)
  • gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
  • cmake version 2.8.12.2

Google TestのリポジトリのREADMEに従ってインストール。
https://github.com/google/googletest/tree/main/googletest

$ git clone https://github.com/google/googletest.git -b release-1.11.0
$ cd googletest
$ mkdir build
$ cd build
$ cmake ..
$ make
Scanning dependencies of target gtest
[ 25%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
In file included from /usr/include/c++/4.8.2/type_traits:35:0,
                 from /home/server/tmp/googletest/googletest/include/gtest/gtest.h:59,
                 from /home/server/tmp/googletest/googletest/src/gtest-all.cc:38:
/usr/include/c++/4.8.2/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

cmakeするときにフラグを指定する。

$ cmake -DCMAKE_CXX_FLAGS="-std=c++11" ..
$ make
Scanning dependencies of target gtest
[ 25%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
In file included from /home/server/tmp/googletest/googletest/include/gtest/internal/gtest-death-test-internal.h:39:0,
                 from /home/server/tmp/googletest/googletest/include/gtest/gtest-death-test.h:41,
                 from /home/server/tmp/googletest/googletest/include/gtest/gtest.h:64,
                 from /home/server/tmp/googletest/googletest/src/gtest-all.cc:38:
/home/server/tmp/googletest/googletest/include/gtest/gtest-matchers.h: In static member function ‘static constexpr bool testing::internal::MatcherBase<T>::IsInlined()’:
/home/server/tmp/googletest/googletest/include/gtest/gtest-matchers.h:414:12: error: ‘is_trivially_copy_constructible’ is not a member of ‘std’
            std::is_trivially_copy_constructible<M>::value &&

is_trivially_copy_constructibleはgcc ver.5以上でないとだめらしい。

gccを5.2.0にして再度インストール

  • gcc version 5.2.0 (GCC)
$ cmake -DCMAKE_CXX_FLAGS="-std=c++11" ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- 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
-- Found PythonInterp: /usr/bin/python (found version "2.7.5")
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/server/tmp/googletest/build

$PATHの先頭にgcc-5.2.0を加えてみたけど、依然4.8.5が指定されているっぽい。
-DCMAKE_CXX_COMPILERオプションをつけることで、コンパイラを明示的に指定できるらしい。

$ cmake -DCMAKE_CXX_FLAGS="-std=c++11" -DCMAKE_C_COMPILER="/usr/local/gcc/5.2.0/bin/gcc" -DCMAKE_CXX_COMPILER="/usr/local/gcc/5.2.0/bin/g++" ..
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= /usr/local/gcc/5.2.0/bin/gcc

-- The C compiler identification is GNU 5.2.0
-- The CXX compiler identification is GNU 5.2.0
-- Check for working C compiler: /usr/local/gcc/5.2.0/bin/gcc
-- Check for working C compiler: /usr/local/gcc/5.2.0/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/local/gcc/5.2.0/bin/c++
-- Check for working CXX compiler: /usr/local/gcc/5.2.0/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found PythonInterp: /usr/bin/python (found version "2.7.5")
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/server/tmp/googletest/build

これでどうだ。

$ make
Scanning dependencies of target gtest
[ 25%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
Linking CXX static library ../lib/libgtest.a
[ 25%] Built target gtest
Scanning dependencies of target gmock
[ 50%] Building CXX object googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
Linking CXX static library ../lib/libgmock.a
[ 50%] Built target gmock
Scanning dependencies of target gmock_main
[ 75%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
Linking CXX static library ../lib/libgmock_main.a
[ 75%] Built target gmock_main
Scanning dependencies of target gtest_main
[100%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
Linking CXX static library ../lib/libgtest_main.a
[100%] Built target gtest_main

とりあえずここまで。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?