単体テスト環境をWSL Ubuntuに構築したい。
GoogleTestを入れたいが、静的ライブラリだとビルドのリンク時に時間がかかるため、
動的ライブラリとしてリンクするようにしたかった。
手順および、遭遇したエラーについて記録
手順概要
インストール手順
$ mkdir workdir
$ git clone https://github.com/google/googletest.git
$ cd googletest
$ mkdir build && cd build
$ cmake .. -DBUILD_SHARED_LIBS=ON
$ cmake --build . --config Release
$ sudo cmake --install .
確認手順
$ cat -- > sample_test.cpp << __EOT__
// sample_test.cpp
#include <gtest/gtest.h>
TEST(SampleTest, SimpleAssertion) {
EXPECT_EQ(1 + 1, 2);
}
__EOT__
$ g++ --std=c++17 -o test_sample sample_test.cpp -lgtest -lgtest_main -pthread
$ ls
sample_test.cpp test_sample
$ LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH ./test_sample
Running main() from /home/odagiri/ws/gtest/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from SampleTest
[ RUN ] SampleTest.SimpleAssertion
[ OK ] SampleTest.SimpleAssertion (0 ms)
[----------] 1 test from SampleTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (3 ms total)
[ PASSED ] 1 test.
遭遇したエラー
cmakeのバージョン
現象
googletestのビルドで要求されるcmakeのバージョンは、3.16以上
しかし、apt-getでインストールできるcmakeは3.10.2
$ sudo apt-cache showpkg cmake
:
:
Provides:
3.10.2-1ubuntu2.18.04.2 -
3.10.2-1ubuntu2 -
Reverse Provides:
なのでcmakeの実行でエラーとなった
解決方法
手動インストールした。
参考:Linuxに特定のバージョンのcmakeをインストール
スクリプトでインストールする方法を実施
cmakeのコンパイラの指定
現象
cmakeのバージョンが解決後、コンパイラの指定が必要になった。
$ cmake .. -DBUILD_SHARED_LIBS=ON
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:6 (project):
No CMAKE_CXX_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
解決方法
build-essentialをインストールした。
sudo apt-get update && sudo apt-get install build-essential
その後、buildディレクトリを削除し、手順をやり直した。
$ rm -rf build/
$ mkdir build && cd build
build$ cmake .. -DBUILD_SHARED_LIBS=ON
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- 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 (4.1s)
-- Generating done (0.1s)
-- Build files have been written to: /home/odagiri/ws/gtest/googletest/build
コンパイルエラー
現象
C++のバージョン指定がないとコンパイルエラーになる
$ g++ -o test_sample sample_test.cpp -lgtest -lgtest_main -pthread
In file included from /usr/local/include/gtest/gtest-message.h:57:0,
from /usr/local/include/gtest/gtest-assertion-result.h:46,
from /usr/local/include/gtest/gtest.h:63,
from sample_test.cpp:2:
/usr/local/include/gtest/internal/gtest-port.h:273:2: error: #error C++ versions less than C++17 are not supported.
#error C++ versions less than C++17 are not supported.
^~~~~
In file included from /usr/local/include/gtest/gtest.h:67:0,
from sample_test.cpp:2:
/usr/local/include/gtest/gtest-param-test.h:483:56: error: missing template arguments before ‘(’ token
typename StdFunction = decltype(std::function(std::declval<Func>()))>
^
/usr/local/include/gtest/gtest-param-test.h:493:56: error: missing template arguments before ‘(’ token
typename StdFunction = decltype(std::function(std::declval<Func>()))>
^
解決方法
--std=c++17 を指定する
g++ --std=c++17 -o test_sample sample_test.cpp -lgtest -lgtest_main -pthread
実行時エラー
現象
そのまま実行すると、ライブラリが見つからない
$ ./test_sample
./test_sample: error while loading shared libraries: libgtest.so.1.16.0: cannot open shared object file: No such file or directory
解決方法
LD_LIBRARY_PATH にライブラリパスを追加する
$ LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH ./test_sample
Running main() from /home/odagiri/ws/gtest/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from SampleTest
[ RUN ] SampleTest.SimpleAssertion
[ OK ] SampleTest.SimpleAssertion (0 ms)
[----------] 1 test from SampleTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (3 ms total)
[ PASSED ] 1 test.