NOTE it's not a duplicate of this question or this question.
How to build Googletest from source using g++ into a shared library (.so)?
I tried the steps described in Google Test's document on how to build a shared library, but linking the produced libgtest.so
with my test programs didn't work - the linker throws tons of errors like:
"gtest-all.cc:(.text+0x19b50): multiple definition of `testing::internal::HasNewFatalFailureHelper::~HasNewFatalFailureHelper()'libgtest.so:gtest-all.cc:(.text+0x19b50): first defined here ./libgtest.so: In function `testing::internal::HasNewFatalFailureHelper::~HasNewFatalFailureHelper()':" gtest-all.cc:(.text+0x19bc4): multiple definition of `testing::internal::HasNewFatalFailureHelper::ReportTestPartResult(testing::TestPartResult const&)' libgtest.so:gtest-all.cc:(.text+0x19bc4): first defined here ./libgtest.so: In function `testing::internal::TypedTestCasePState::VerifyRegisteredTestNames(char const*, int, char const*)':
When building libgtest.so
, I used command:
g++ -fPIC -DGTEST_CREATE_SHARED_LIBRARY=1 -pthread -c $(GTEST)/src/gtest-all.cc -shared
(this macro is required in the aforementioned Google Test's document) and then mv gtest-all.o libgtest.so
When linking with libgtest.so
, I used load flag -DGTEST_LINKED_AS_SHARED_LIBRARY=1
, as recommended in the aforementioned Google Test's document. I also used -L
flag to tell g++ where the library is.
NOTE: if I choose to build a libgtest.a
archive from source and link my test program with this archive, everything is fine (instructions on how to build it is also in that Google Test document). The reason why I'd like to switch from libgtest.a
to libgtest.so
is I want the executables to load GoogleTest at runtime so linking might be faster.