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

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

问题描述

我有一个共享库项目,该项目由4个静态库(.a)和一个对象(.o)文件构建.我试图添加-fvisibility=hidden选项以将输出中的符号限制为仅使用__attribute__在源中标记的符号.

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__.

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

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.

目标文件中的符号已从最终的.so中删除.但是,来自.a项目的符号都仍在最终的.so文件中.将-fvisibility=hidden选项添加到.so链接命令无效.

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.

我在做什么错了?

我的目的是从.so中删除除库的接口功能以外的所有符号.

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

我实际上使用了版本映射来解决此问题现在.但是,随着外部符号的更改,它需要继续维护版本脚本.接受的答案有一个更好的主意.

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.

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

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(可能进入适当的空目录)
  • gcc -fvisibility=hidden -shared -o mylib.so foo.o tempdir/*.o
  • ar x libbar.a (possibly into a suitable, empty directory)
  • gcc -fvisibility=hidden -shared -o mylib.so foo.o tempdir/*.o

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

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