google test と テストコードをリンク時に発生したエラーを対処した話
発生したリンクエラー
libgtest.a とテストコードを以下のようにリンクした。
g++ -I./test-code/inc -I./googletest/googletest/include -I./googletest/googlemock/include -coverage -std=c++11 -L./test-target/lib -L./googletest/lib -o bin/google-test-example1 test-code/obj/google-test-example1.o -lpthread -ltest_target -lgtest -lgmock -lgtest_main
すると、以下のようなエラーが発生。
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x24): `pthread_getspecific' に対する定義されていない参照です
対処
-lpthread を一番後ろに持っていくとうまくいった。1
g++ -I./test-code/inc -I./googletest/googletest/include -I./googletest/googlemock/include -coverage -std=c++11 -L./test-target/lib -L./googletest/lib -o bin/google-test-example1 test-code/obj/google-test-example1.o -ltest_target -lgtest -lgmock -lgtest_main -lpthread
-
恥ずかしながらライブラリの記述順序に対する知見がないので勉強したいと思った。 ↩