8
8

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 5 years have passed since last update.

google test のインストールでハマったのでメモ

Posted at

GoogleTestをやろうとして、意外とハマったのでメモ。

ハマった原因

  • google mockがない時代?のドキュメントが多く、ディレクトリ構成が微妙に今と違うドキュメントが多い。
  • 環境変数名で悩む(定番がどれかわからず。。。) =>これは結局わからず。

事前準備

gitとcmakeは別途インストールなど、準備してください。

ビルド

$ git clone https://github.com/google/googletest.git
$ cd googletest
$ mkdir build
$ cd build
$ cmake ..
$ make

ビルド結果確認

できあがったライブラリはどこでしょう。。。

$ find ./ -name "*.a"
googlemock/gtest/libgtest.a
googlemock/gtest/libgtest_main.a
googlemock/libgmock.a
googlemock/libgmock_main.a

ありました。

gtest.hも探しておきます。

$ cd ..
$ find ./ -name "gtest.h"
.//googletest/include/gtest/gtest.h
.//googletest/include/gtest/internal/custom/gtest.h

今後使うのは、googletest/includeの方のinclude directoryです。

ここで環境変数設定しておく

export GTEST_ROOT=".gitのあるディレクトリ"
export GTEST_INCLUDEDIR=$GTESTROOT/googletest/include
export GMOCK_INCLUDEDIR=$GTESTROOT/googlemock/include
export GMOCK_LIBDIR=$GTESTROOT/build/googlemock
export GTEST_LIBDIR=$GTESTROOT/build/googlemock/gtest

適宜、.bashrcなどに書いておく。

環境変数のチェック

環境変数の設定間違えるとハマるのでチェック

 $ ls $GMOCK_INCLUDEDIR
gmock

$ ls $GTEST_INCLUDEDIR                                           
gtest

$ ls $GMOCK_LIBDIR                                               
CMakeFiles              cmake_install.cmake     libgmock_main.a
CTestTestfile.cmake     gtest
Makefile                libgmock.a

$ ls $GTEST_LIBDIR
CMakeFiles              Makefile                libgtest.a
CTestTestfile.cmake     cmake_install.cmake     libgtest_main.a

テストしてみる

main()をlibgtest_main.aに任せる場合

test1.cpp
#include "gtest/gtest.h"

TEST(Test1, test11)
{
    ASSERT_EQ(1, 1);
}
$ g++ test1.cpp -I$GTEST_INCLUDEDIR -L$GTEST_LIBDIR -lgtest -lgtest_main

$ ./a.out
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Test1
[ RUN      ] Test1.test11
[       OK ] Test1.test11 (0 ms)
[----------] 1 test from Test1 (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

main()を自分で書く場合

test2.cpp
#include "gtest/gtest.h"

TEST(Test1, test11)
{
    ASSERT_EQ(1, 1);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();

}
$ g++ test2.cpp -I$GTEST_INCLUDEDIR -L$GTEST_LIBDIR -lgtest
8
8
1

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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?