GoogleTest導入
CMake
CMakeはクロスプラットフォーム上でのコンパイルを可能としたアプリケーションである。
Linuxはgcc、MacOSはclangなどC言語ではOSによって標準コンパイルが異なるケースが多いが、CMakeを使うことにより、その差分を埋めることができる。
CMakeのMakefile的なものはCMakeLists.txt
となる。
ここにコンパイルするファイルを指定したり、使うコンパイラのバージョンを指定したりする。
CMakeListsを書くときに最小限必要な記述の解説
cmake_minimum_required(VERSION 3.10)
# set the project name
project(Tutorial)
# add the executable
add_executable(Tutorial tutorial.cpp)
cmake_minimum_required
: 使用するcmakeのバージョンを指定
project
: プロジェクト名を指定
何に使うのか正直不明ではあるが、${CMAKE_PROJECT_NAME}
の変数にここで指定した値が保存される。オプションでコンパイルで使う言語を明示的に指定する事もできる。
add_executable
: 1個目が実行ファイル名 2個目以降がコンパイルするようのファイル。
上記の例だと、tutorial.cppをコンパイルした後の、実行可能ファイルがTutorialになるという関係。
projectのオプションの詳しい部分はここ
やってみる
こんな感じでフォルダ構成を作る。
├ build/
├ CMakeLists.txt
└ main.c
ちなみにbuildフォルダを作るのは、cmakeコマンドによってコンパイルを行うと複数ファイルが出てきて、
自分が作ったファイルのcmake実行によりできたファイルが混同しないようにするためである。
各ファイルの内容は以下
cmake_minimum_required(VERSION 3.20)
project(hello_world C)
add_executable(a.out main.c)
#include <stdio.h>
int main()
{
printf("hello world");
return (0);
}
ビルド方法としては少し特殊で、buildディレクトリに移動してから相対パスでCMakeLists.txtを参照する形にする。
これで作業ディレクトリに必要なファイルが揃う
❯ cd build
❯ cmake ..
ビルドオプションをつけて実行ファイルを作成
❯ cmake --build .
projectに指定したa.outが作成されているので実行
❯ ./a.out
hello world%
このブログがめちゃめちゃわかりやすかった。
次はGoogletest導入
以下コマンドにより導入
git clone https://github.com/google/googletest.git -b release-1.11.0
cd googletest # Main directory of the cloned repository.
mkdir build # Create a directory to hold the build output.
cd build
cmake .. # Generate native build scripts for GoogleTest.
make
sudo make install # Install in /usr/local/ by default
vscode上で#include "gtest/gtest.h"
を入れると、vscodeのintellisenseが怒ってくる。
Extensionsの C/C++ ConfigurationsのIntelliSense Configurations
にて、
IncludePath
に/usr/local/include
を追加することで解消する
c言語のファイルをGoogletestでテストしてみます。
フォルダ構成はこんな感じ。
先程はmain.cだったのですが、mainはGoogletest側で用意してくれるので不要としてます。
自分はテストフォルダの中にgtest.cppというファイルを入れてますが、この辺もコンパイルで指定する名前を変更するだけでいいので何でもいいです。
.
├── CMakeLists.txt
├── build
├── print.c
└── test
└── gtest.cpp
CMakeLists.txtをこのように編集
cmake_minimum_required(VERSION 3.20)
# 今回は自身が各ファイルにC, Googletestを書くファイルにC++をもってきてるので CとCXXを指定子に追加
project(hello_world C CXX)
# GTestパッケージを利用可能にする
find_package(GTest REQUIRED)
add_executable(
main
print.c
# コンパイルにgtestを含める
test/gtest.cpp
)
target_link_libraries(
main
# リンクするライブラリにGtestを追加 (これでMainを書かなくてもテストが動くようです。)
GTest::Main
)
残りのファイルは以下のように記述
標準出力のテストは少し難しそうなので、今回は省く
print.c
char *print()
{
return "hello world";
}
gtest.cpp
#include "gtest/gtest.h"
extern "C" char *print();
TEST(cmake_demo, eq_test)
{
ASSERT_STREQ("hello world", print());
}
記述としてはStringの比較をしている感じ
簡単なテストの記述はここが参考になりました。
これで前回やった方法と同じようにcmake→buildまで実行
❯ cd build
❯ cmake ..
❯ cmake --build .
Consolidate compiler generated dependencies of target main
[ 33%] Building C object CMakeFiles/main.dir/print.c.o
[ 66%] Building CXX object CMakeFiles/main.dir/test/gtest.cpp.o
[100%] Linking CXX executable main
[100%] Built target main
コンパイルが通れば build配下にmainという実行可能ファイルがあるので実行してみる。
❯ ./main
Running main() from Projects/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from cmake_demo
[ RUN ] cmake_demo.eq_test
[ OK ] cmake_demo.eq_test (0 ms)
[----------] 1 test from cmake_demo (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
こんな感じで実行されました!