Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
7
Help us understand the problem. What is going on with this article?
@medaka5

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

More than 3 years have passed since last update.

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
7
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
medaka5

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
7
Help us understand the problem. What is going on with this article?