如何构建一个具有少量函数接口且隐藏其余对象的 C 库?
假设我有三个 C 目标文件:po、qo、ro,我想用它们创建一个库。第一个对象文件 po 有几个我想要导出的函数,这些函数是使用其他两个对象(qo、ro)实现的。现在我希望,当链接到这样的库时,只应该看到 po 中的函数,而看不到其他函数,因为它们的符号可能与主项目中的其他对象发生冲突。例如,qo 可能具有 po 需要的某些函数 f(),但这可能与主项目中实现的某些 f() 冲突,该主项目在其他库中与该库链接。我怎样才能使用 gcc 做到这一点?
Let's suppose I have three C object files: p.o, q.o, r.o, and I want to make a library with them. The first object file, p.o, has a couple of functions I want to export, which are implemented using the other two objects (q.o, r.o). Now I want that, when linking with such library, only the functions in p.o should be seen and none of the others, because their symbols may clash with other objects in the main project. For example, q.o may have some function f() that p.o needs, but that may clash with some f() implemented in the main project that links, among other libraries, with this library. How can I do that using gcc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 GCC 的 可见性 功能来隐藏不应该导出的符号。或者,重构库源代码,将函数声明为静态,因此不会导出到翻译单元之外。
You can use GCC`s visibility feature to hide symbols that are not supposed to be exported. Alternatively, re-factor your library source code in such a way that functions are declared as static and so are not exported beyond the translation unit.
您需要设置函数的可见性: 控制符号可见性
You need to set visibility for your functions: Controlling Symbol Visibility