如何强制gcc链接来自库的未引用的静态C ++对象 [英] How to force gcc to link unreferenced, static C++ objects from a library

查看:100
本文介绍了如何强制gcc链接来自库的未引用的静态C ++对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的C ++库可以构建为共享或静态库。
这个库使用一种工厂技术,其中静态对象在程序启动时注册自身,并创建静态对象。

I'm using a C++ library that can be built as either a shared or a static library. This library uses a factory technique, where static objects register themselves when the program starts and the static objects get created.

这个工作正常,只要共享库。当使用静态版本时,没有一个静态对象被包含到最终的程序中(因为它们不是直接引用的),因此它们的功能不可用。

This works fine as long as the shared library is used. When the static version is used, none of the static objects get included into the final program (because they aren't referenced directly) and thus their functionality isn't available.

是否有一种方法强制gcc在链接时包括库中的所有静态对象?

Is there a way to force gcc to include all static objects from a library when linking?

库是开源,我可以修改它,如果这有帮助。 / p>

The library is Open Source and I could modify it, if that helps.

推荐答案

您可以使用 -Wl, - whole-archive -lyourlib ,请参阅 ld 的联机帮助页获取更多信息。

You can use -Wl,--whole-archive -lyourlib , see the manpage for ld for more info.

在-Wl之后提到的任何静态库,归档在命令行获得完全包括,你可以再次关闭这个,如果你需要,如在例如 -Wl, - whole-archive -lyourlib -Wl, - no-whole-archive -lotherlib

Any static libraries mentioned after -Wl,--whole-archive on the command line gets fully included, you can turn this off again too if you need to , as in e.g. -Wl,--whole-archive -lyourlib -Wl,--no-whole-archive -lotherlib

这篇关于如何强制gcc链接来自库的未引用的静态C ++对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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