我在Linux / Mac下编译了一个C ++库,其中隐藏了符号。我对所有类都使用了 _属性 _((visibility(“ hidden”))),并使用选项(-c -O2 -fPIC -MMD -MP -MF)进行了编译。在Mac上,使用MacDependencies(http://code.google.com/p/macdependency/),就可以很好地完成工作,因为我只看到我的出口(实际上我看到了之前和之后的区别)。
但是,我注意到使用 nm 仍然可以看到符号的所有名称。在Mac和Linux下都会发生这种情况。
这是为什么?有什么办法可以避免这种情况?
最好的问候和感谢,乔
公共或隐藏的符号仍然存在。nm显示所有符号。区别在于隐藏的符号对于动态链接器不可用,即,不能导出,也不能插入。
nm
您可能还会喜欢以下内容 man gcc:
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.