CppUnit を Ubuntu 18.04 LTS にインストールする
1. はじめに
CppUnit は,ユニットテストフレームワークとして有名な JUnit の C++ バージョンです。公式サイトに記載されているビルド手順に従って Ubuntu 18.04 LTS にインストールしようとしてもエラーが発生します。本記事では,CppUnit をマニュアルでコンパイルして,Ubuntu 18.04 LTS にインストールする手順について記述します。
2. コンパイル
まずは,公式サイトに記載されているビルド手順に従ってコンパイルします。
1$ git clone git://anongit.freedesktop.org/git/libreoffice/cppunit/2$ ls3cppunit4$ cd cppunit5$ ./autogen.sh6error: aclocal not found
まず初めに,error: aclocal not found というエラーが出力されます。これは,aclocal (Automake) パッケージが存在していないという意味なので,aclocal (GNU Automake) パッケージをインストールし,再度 autogen.sh スクリプトを実行します。
1$ sudo apt install automake -y2$ ./autogen.sh3./autogen.sh: need libtoolize tool to build cppunit
次に,./autogen.sh: need libtoolize tool to build cppunit というエラーが出力されます。これは,libtoolize (GNU Libtool) パッケージが存在していないという意味なので,libtoolize (GNU Libtool) パッケージをインストールし,再度 autogen.sh スクリプトを実行します。
1$ sudo apt install libtool -y2$ ./autogen.sh3libtoolize: putting auxiliary files in '.'.4libtoolize: copying file './ltmain.sh'5libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.6(略)7configure.ac:20: installing './missing'8examples/cppunittest/Makefile.am: installing './depcomp'9parallel-tests: installing './test-driver'
autogen.sh スクリプトが正常に実行されていれば,上記のような文字列が出力されます。次に,configure スクリプトを実行します。
1$ ./configure2checking for a BSD-compatible install... /usr/bin/install -c3checking whether build environment is sane... yes4checking for a thread-safe mkdir -p... /bin/mkdir -p5(略)6checking whether g++ supports C++11 features with -std=c++11... no7checking whether g++ supports C++11 features with -std=c++0x... no8configure: error: *** A compiler with support for C++11 language features is required.
configure: error: *** A compiler with support for C++11 language features is required. というエラーが出力されます。これは,C++ コンパイラが存在していないという意味なので,C++ コンパイラをインストールし,再度 configure スクリプトを実行します。
1$ sudo apt install g++ -y2$ ./configure3checking for a BSD-compatible install... /usr/bin/install -c4checking whether build environment is sane... yes5checking for a thread-safe mkdir -p... /bin/mkdir -p6(略)7==============================================================================8Build configuration:9 debug: no10 docs: no11 werror: yes12==============================================================================
configure スクリプトが正常に実行されていれば,上記のような文字列が出力されます。最後に,make コマンドを用いて CppUnit のソースコードをコンパイルします。
1$ make2make all-recursive3make[1]: ディレクトリ '/home/[USER_NAME]/cppunit' に入ります4Making all in src5(略)6make[2]: 'all-am' に対して行うべき事はありません.7make[2]: ディレクトリ '/home/[USER_NAME]/cppunit' から出ます8make[1]: ディレクトリ '/home/[USER_NAME]/cppunit' から出ます
make コマンドが正常に実行されていれば,上記のような文字列が出力されます。これで CppUnit のコンパイルは完了です。続いて,CppUnit のインストールに移ります。
3. インストール
管理者権限の make コマンドに引数 install を付与して実行します。
1$ sudo make install2Making install in src3make[1]: ディレクトリ '/home/[USER_NAME]/cppunit/src' に入ります4Making install in cppunit5(略)6 /usr/bin/install -c -m 644 cppunit.pc '/usr/local/lib/pkgconfig'7make[2]: ディレクトリ '/home/[USER_NAME]/cppunit' から出ます8make[1]: ディレクトリ '/home/[USER_NAME]/cppunit' から出ます
インストールが正常に完了していれば上記のような文字列が出力されます。これで CppUnit のインストールは完了です。続いて,CppUnit の動作確認に移ります。
4. 動作確認
公開されている CppUnit のサンプルコードを,お借りして動作確認を行います。サンプルコードを sample.cpp として任意の場所に保存し,コンパイル,実行します。
1$ ls2sample.cpp3$ g++ -std=c++14 -g sample.cpp -L /usr/local/lib -lstdc++ -lcppunit -ldl4$ ./a.out5./a.out: error while loading shared libraries: libcppunit-1.15.so.1: cannot open shared object file: No such file or directory
libcppunit.so が参照されていない状態なので,ネット記事を参考にしてパスを通し,再度 a.out を実行します。
1$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib2./a.out3FunctionTest::test_add : OK4FunctionTest::test_diff : assertion5FunctionTest::test_mul : OK6sample.cpp:69:Assertion7Test name: FunctionTest::test_diff8equality assertion failed9- Expected: 110- Actual : 10011
12Failures !!!13Run: 3 Failure total: 1 Failures: 1 Errors: 0
正常に実行されていれば引用元の実行結果と同様の実行結果が出力されます。これで CppUnit の動作確認が完了です。
5. おわりに
ここまで,CppUnit をマニュアルでコンパイルし,Ubuntu 18.04 LTS にインストールする手順について記述してきました。ここ数年で,CppUnit 以外の C++ 用ユニットテストフレームワークが多く登場してきたので CppUnit を使用する機会も減っています。それに伴い,CppUnit に関する新しい記事も減っています。本記事が CppUnit ユーザーの一助になれば幸いです。
環境情報
- CppUnit Ver.1.15.1
- g++ Ver.7.4.0
- Bash Ver.4.4.20
- Zorin OS 15 Core (Ubuntu 18.04 LTS)