简体   繁体   English

关于Linux中的共享库,是否可以选择库中的导出功能?

[英]About shared library in Linux, is there a way to select export functions in a library?

I read the tutorial here: http://www.techytalk.info/c-cplusplus-library-programming-on-linux-part-two-dynamic-libraries/ 我在这里阅读了该教程: http : //www.techytalk.info/c-cplusplus-library-programming-on-linux-part-two-dynamic-libraries/

It looks to me there is no functionality like dllexport of DLL in Windows platform. 在我看来,在Windows平台中没有像DLL的dllexport的功能。

Is there some way to select certain functions within the library as export function and make the rest functions in the library remain invisible to external call? 有什么方法可以选择库中的某些函数作为导出函数,并使库中的其余函数对外部调用不可见?

You may want to use the visibility function attribute of GCC. 您可能要使用GCC的可见性功能属性

See GCC visibility wikipage and read Drepper's paper How To Write Shared Libraries 请参阅GCC可见性Wikipage,并阅读Drepper的论文 如何编写共享库

There are multiple ways to do this. 有多种方法可以做到这一点。

Either use the visibility function attribute as mentioned in Basiles answer or use a linker version script to do the job. 使用Basiles答案中提到的可见性功能属性,或使用链接器版本脚本来完成这项工作。

In a linker script you list all the functions that you want to export. 在链接器脚本中,列出要导出的所有功能。 Here is an example: 这是一个例子:

File: MyLinkerScript.exp 文件:MyLinkerScript.exp

{
global: 
  myExportedFunction1;
  myExportedFunction2;
  myExportedFunction3;
local: *;
};

During the link step of your shared library you just pass the following extra parameters to gcc: 在共享库的链接步骤中,您只需将以下额外参数传递给gcc:

-Wl,--version-script=MyLinkerScript.exp

Afterwards all symbols in the shared library will be private except for those listed in the global section of your version script. 之后,共享库中的所有符号将是私有的,但版本脚本的“全局”部分中列出的符号除外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM