使用g ++的符号可见性 [英] Symbol visibility using g++

查看:139
本文介绍了使用g ++的符号可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux / Mac下编译了一个C ++库,其中隐藏了符号。我为所有类使用了 _ 属性 _((visibility(hidden))),并使用选项进行编译(-c -O2 -fPIC -MMD -MP -MF)。在Mac下,使用MacDependencies(http://code.google.com/p/macdependency/),工作完成得很好,因为我只看到了我的导出(我实际上看到了前后的差异)。

I compiled a C++ library under Linux/Mac with its symbols hidden. I've used _attribute_ ((visibility("hidden"))) for all my classes and compiled with options (-c -O2 -fPIC -MMD -MP -MF). Under Mac, using MacDependencies (http://code.google.com/p/macdependency/), the job is done just fine as I see only my exports (I actually saw the difference before and after).

但是,我注意到使用 nm 我仍然可以看到所有符号的名称。这发生在Mac和Linux下。

However, I noticed that using nm I still see all the names of the symbols. This happens under both Mac and Linux.

这是为什么?有什么办法可以避免这种情况?

Why is that? Is there any way to avoid this?

最好的问候和感谢,
Joe

Best Regards and thanks, Joe

推荐答案

公开或隐藏,符号仍然存在。 nm 显示所有符号。不同的是,隐藏的符号不可用于动态链接器,即不导出,也不能插入。

Public or hidden, symbols are still there. nm shows all symbols. The difference is that hidden symbols are not available to the dynamic linker, i.e. not exported and can not be interposed.

您可能还喜欢以下 man gcc

   -fvisibility=default|internal|hidden|protected
       ...
       A good explanation of the benefits offered by ensuring ELF symbols
       have the correct visibility is given by "How To Write Shared
       Libraries" by Ulrich Drepper (which can be found at
       <http://people.redhat.com/~drepper/>)---however a superior solution
       made possible by this option to marking things hidden when the
       default is public is to make the default hidden and mark things
       public.  This is the norm with DLL's on Windows and with
       -fvisibility=hidden and "__attribute__ ((visibility("default")))"
       instead of "__declspec(dllexport)" you get almost identical
       semantics with identical syntax.  This is a great boon to those
       working with cross-platform projects.

这篇关于使用g ++的符号可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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