如何将gcc -fvisibility选项应用于静态库中的符号? [英] How to apply gcc -fvisibility option to symbols in static libraries?

查看:539
本文介绍了如何将gcc -fvisibility选项应用于静态库中的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由4个静态库( .a )和一个对象( .o )文件。我正在尝试添加 -fvisibility = hidden 选项来将输出中的符号限制为仅在源中使用__attribute __标记的符号。



我已将 -fvisibility = hidden 选项添加到 .so 项目(涵盖 .o 文件)和 .a 项目。



对象文件中的符号从最终的 .so 中按预期被删除。但是, .a 项目中的符号仍然位于最后的 .so 文件中。将 -fvisibility = hidden 选项添加到 .so 链接命令中不起作用。



我做错了什么?



我的目的是从 .so 除了接口函数以外的所有符号。



编辑:我实际上使用了版本地图来解决这个问题。但是,随着外部符号的变化,它需要继续维护版本脚本。接受的答案有一个更好的主意。

解决方案

基本上,可见性是在链接过程中处理的,链接器似乎并不强加它静态档案。一个相关的问题(虽然不是重复的)在SO 这里上被询问。



我建议你做的是将你的链接阶段替换为两个阶段的过程,你可以将 gcc -shared -o mylib.so foo.o libbar.a 取回目标文件:


  • ar x libbar.a
  • gcc -fvisibility = hidden -shared -o mylib.so foo.o tempdir / *。o
  • / li>

I have a shared library project that is built from 4 static libraries (.a) and one object (.o) file. I am trying to add the -fvisibility=hidden option to restrict symbols in the output to only those that I mark in the source with an __attribute__.

I've added the -fvisibility=hidden option to the compile options for the .so project (which covers the .o file) and for the .a projects.

The symbols in the object file are removed as expected from the final .so. However the symbols from the .a projects are all still in the final .so file. Adding the -fvisibility=hidden option to the .so link command has no effect.

What am I doing wrong?

My purpose here is to remove from the .so all symbols except the interface functions to the library.

EDIT: I actually used a version map to solve this for now. However it requires continued maintenance of the version script as external symbols change. Accepted answer has a better idea.

解决方案

Basically, visibility is handled during linking, and the linker doesn't seem impose it on static archives. A related question (though not a duplicate) was asked on SO here.

What I would advise you to do is to replace your linking stage: gcc -shared -o mylib.so foo.o libbar.a into a two stages process where you get back the object files:

  • ar x libbar.a (possibly into a suitable, empty directory)
  • gcc -fvisibility=hidden -shared -o mylib.so foo.o tempdir/*.o

这篇关于如何将gcc -fvisibility选项应用于静态库中的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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