views:

178

answers:

3

I am building a shared library on Ubuntu 9.10. I want to export only a subset of my functions from the library. On the Windows platform, this would be done using a module definition (.def) file which would contain a list of the external and internal names of the functions exported from the library.

I have the following questions:

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

  2. Using .def files as an example, you can give a function an external name that is different from its internal name (useful for prevent name collisions and also redecorating mangled names etc)

  3. On windows I can use the EXPORT command (IIRC) to check the list of exported functions and addresses, what is the equivalent way to do this on Linux?

+1  A: 

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 simples in a shared object you can use the readelf command. readelf -Ds if I remember correctly.

Charles Bailey
You don't actually need to decorate them with visibility set to default, as that's the default...
Fredrik Ullner
@Fredrik Ullner: I don't understand your point. If you use `-fvisibility=hidden` then have to re-set the visibility to default of those symbols that you don't want hidden otherwise no symbols would be visible.
Charles Bailey
"then decorate the symbols that you want to be visible with __attribute__((visibility("default")))." You don't need to do that, since that is the default.
Fredrik Ullner
Charles Bailey
+1  A: 

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

Dmitry Yudakov
A: 

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

Alex