Advertisement Jump to content
Sign in to follow this  

How to Export Symbols from a Unix Shared Library?

This topic is 4965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The title pretty much says it all. I see how to to find and get the addresses of symbols at run time, but I don't know how to specify which symbols are to be exported, and what name to identify them with.

Share this post

Link to post
Share on other sites
As far as I'm aware, normally most non-static global variables and functions will be accessible in a shared library.

The thing to remember is that if you use C++, you need to be concerned with name mangling (this is true on win32 also of course).

Name mangling is where void foo(void) { } gets changed into foo__Fv or something similar.

You can't easily work out what the name will be, so instead, use extern "C" when declaring your function (you don't need to do anything special when defining it).

extern "C" {
void foo();

// .. somewhere else

void foo() {
std::cout << "Hello, world" << std::endl;

Then you can use

g++ -shared -o myplugin.cpp

And something like

void * dlhandle = dlopen("./",RTLD_NOW);
foo = (footype) dlsym(dlhandle,"foo");

Declare footype to be the type of your function as a function pointer.

If you're wondering how you declare a class in your main program (exe) and then subclass it, overriding virtual functions in your .so, then the answer is definitely, that you should make a "factory" function (with C linkeage as above) which calls new(thing) and returns a pointer.

You can then cast the pointer to the base class and you're done.

However, this typically only works if you use -Wl,--export-dynamic when compiling your main exe, because this enables you to call functions in your exe from your dll (for example, the base class's constructor). You'll need this even if you don't have an explicit calls back.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!