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

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

问题描述

我在 Linux/Mac 下编译了一个隐藏符号的 C++ 库.我已经为我的所有类使用了 _attribute_ ((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?

最好的问候和感谢,乔

推荐答案

无论是公开的还是隐藏的,符号都还在.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天全站免登陆