如何在Linux上使用g ++构建和使用GoogleTest共享库(.so)? [英] How to build and use GoogleTest shared library (.so) using g++ on Linux?

查看:341
本文介绍了如何在Linux上使用g ++构建和使用GoogleTest共享库(.so)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意,它不是这个问题. >

NOTE it's not a duplicate of this question or this question.

如何使用g ++从源代码构建Googletest到共享库(.so)中?

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):testing :: internal :: HasNewFatalFailureHelper :: ~~ HasNewFatalFailureHelper()'libgtest.so:gtest-all.cc:(.text+0x19b50)的多个定义):首先在这里定义 ./libgtest.so:在函数testing :: internal :: HasNewFatalFailureHelper ::〜HasNewFatalFailureHelper()中: gtest-all.cc:(.text+0x19bc4):`testing :: internal :: HasNewFatalFailureHelper :: ReportTestPartResult(testing :: TestPartResult const&)'的多个定义 libgtest.so:gtest-all.cc:(.text+0x19bc4):首先在这里定义 ./libgtest.so:在函数testing :: internal :: TypedTestCasePState :: VerifyRegisteredTestNames(char const *,int,char const *)':

"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*)':

构建libgtest.so时,我使用了命令:

When building libgtest.so, I used command:

g++ -fPIC -DGTEST_CREATE_SHARED_LIBRARY=1 -pthread -c $(GTEST)/src/gtest-all.cc -shared(在上述Google测试的文档中需要此宏),然后再mv gtest-all.o libgtest.so

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

libgtest.so链接时,按照上述Google测试文档中的建议,我使用了加载标志-DGTEST_LINKED_AS_SHARED_LIBRARY=1.我还使用了-L标志来告诉g ++库在哪里.

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.

注意::如果我选择从源代码构建libgtest.a归档文件并将我的测试程序与此归档文件链接,则一切正常(有关如何构建它的说明,请参见 Google测试文档).我想从libgtest.a切换到libgtest.so的原因是我希望可执行文件在运行时加载GoogleTest,因此链接可能会更快.

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.

推荐答案

这是我找到的解决方案.

Here is the solution I found.

第1步.建立目标文件gtest-all.o

g ++ -fPIC -isystem ../path/to/googletest/include -I ../utils/third-party/googletest/-pthread -c ../path/to/googletest/src/gtest-all. cc

g++ -fPIC -isystem ../path/to/googletest/include -I../utils/third-party/googletest/ -pthread -c ../path/to/googletest/src/gtest-all.cc

推荐GoogleTest的文档,将宏标志-DGTEST_CREATE_SHARED_LIBRARY=1添加到上述命令.

It's recommended by GoogleTest's doc to add a macro flag -DGTEST_CREATE_SHARED_LIBRARY=1 to the command above.

第2步.编译共享库

g ++ -fPIC-共享的gtest-all.o -o libgtest.so

g++ -fPIC -shared gtest-all.o -o libgtest.so

然后,以链接到共享库

确保使用-Wl,-rpath=./path/to/libgtest.so标志. -Wl,-rpath之间没有空格(-Wl,option表示将option传递给链接器.

Be sure to use -Wl,-rpath=./path/to/libgtest.so flag. There is no white space between -Wl, and -rpath (-Wl,option means passing option to linker. more on -Wl,option). It's recommended to add flag -DGTEST_LINKED_AS_SHARED_LIBRARY=1.

这篇关于如何在Linux上使用g ++构建和使用GoogleTest共享库(.so)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆