Is there a .def file equivalent on Linux for controlling exported function names in a shared library?

こ雲淡風輕ζ 提交于 2019-12-02 17:41:42

The most common way to only make certain symbols visible in a shared object on linux is to pass the -fvisibility=hidden to gcc and then decorate the symbols that you want to be visible with __attribute__((visibility("default"))).

If your looking for an export file like solution you might want to look at the linker option --retain-symbols-file=FILENAME which may do what you are looking for.

I don't know an easy way of exporting a function with a different name from its function name, but it is probably possible with an elf editor. Edit: I think you can use a linker script (have a look at the man page for ld) to assign values to symbols in the link step, hence giving an alternative name to a given function. Note, I haven't ever actually tried this.

To view the visible symbols in a shared object you can use the readelf command. readelf -Ds if I remember correctly.

How can I restrict the exported functions of a shared library to those I want (i.e. a .def file equivalent)

Perhaps you're looking for GNU Export Maps or Symbol Versioning

g++ -shared spaceship.cpp -o libspaceship.so.1 -Wl,-soname=libspaceship.so.1 -Wl, --version-script=spaceship.expmap

gcc also supports the VC syntax of __declspec(dllexport). See this.

Another option is to use the strip command with this way:

strip --keep-symbol=symbol_to_export1 --keep-symbol=symbol_to_export2 ... \
     libtotrip.so -o libout.so
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!