限制来自链接对象的全局符号的范围 [英] Limiting the scope of global symbols from linked objects

查看:183
本文介绍了限制来自链接对象的全局符号的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C库在一个档案文件, clib.a 。我为它写了一个C ++包装, cpp.o ,并希望将其用作静态库:

I have a C library in an archive file, clib.a. I've written a C++ wrapper for it, cpp.o, and would like to use this as a static library:

ar cTrvs cppwrap.a clib.a cpp.o

链接到此的代码将不能直接使用 clib.a 中的内容,除非包含正确的标题。然而,如果有人巧合地创建了适当的原型 - 例如。 void myCoincidentallyNamedGlobalFunction() - 我关心 定义 myCoincidentallyNamedGlobalFunction 将适用。

Code which links to this won't be able to use the stuff from clib.a directly unless the correct header is included. However, if someone coincidentally creates an appropriate prototype -- e.g. void myCoincidentallyNamedGlobalFunction() -- I'm concerned which definition of myCoincidentallyNamedGlobalFunction will apply.

由于 clib.a 中的符号只需要在 cpp.o ,而不是链接到 cppwrap.a 的任何东西,有没有办法完全隐藏它们,以便没有可能的碰撞(所以即使包括clib header会失败)

Since the symbols from clib.a only need to be accessed in cpp.o, and not anything linked to cppwrap.a, is there a way to completely hide them so that there is no possible collision (so even including the clib header would fail)?

推荐答案

您可以手动删除最终组合库上不需要的符号:

You can manually remove unneeded symbols on the final combined library:

$ objcopy -N foo cppwrap.a (删除符号)

如果你需要符号,但要确保外部用户不能得到他们:

Or, if you need the symbols but want to make sure that external users can't get to them:

$ objcopy -L bar cppwrap.a (localize符号)

$ objcopy -L bar cppwrap.a (localize symbol)

或者,如果 clib.a 必须 cpp.o 中显示,但您不希望其他人使用:

Or, if a symbol in clib.a must be visible by something in cpp.o but you don't want it to be used by anyone else:

$ objcopy -W baz cppwrap.a (削弱符号)

与来自其他目标文件/库的符号将延迟其使用,即使符号仍然可见。为了进一步掩盖事物或减少甚至是引用冲突的机会,您还可以使用:

In this case, collisions with symbols from other object files/libraries will defer to their usage, even though the symbol will still be visible. To obscure things further or to reduce chances of even a deferential collision, you can also use:

$ objcopy --redefine-sym old = new cppwrap.a

匿名命名空间在某些情况下可能会有帮助,但如果您的包装函式需要但试图隐藏外部用户。

An anonymous namespace may help in some cases, but not if there's functionality that your wrapper needs but is trying to hide from external users.

这篇关于限制来自链接对象的全局符号的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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