Jump to content
  • Advertisement
Sign in to follow this  
Catafriggm

How to Export Symbols from a Unix Shared Library?

This topic is 4872 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
Advertisement
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.so myplugin.cpp


And something like


void * dlhandle = dlopen("./myplugin.so",RTLD_NOW);
foo = (footype) dlsym(dlhandle,"foo");
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.

Mark

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!