Reputation: 41
I am writing a plugin for an application. The application will load the plugin using dlopen()
. The platform is Fedora Linux 11. I have all the source code for the application.
I have successfully added my code into the application, and it linked fine because the whole application was being linked at once. However, ultimately I want my code to be a separate plugin.
I am having trouble building my code as a plugin. I am building it as a shared (.so) library. My code naturally refers to symbols in the application, so I need to specify libraries that the plugin links to using the g++
-L
and -l
options. However, the application that accepts my plugin is a single executable file, not a set of .so libraries.
I'm missing some basic point here. How do I link my plugin?
Note that I am still trying to just build my plugin--I am not yet to the point where I am trying to load it using dlopen()
.
SOLUTION: Thank you all for your answers. I used the --just-symbols method suggested by nemo, and it worked great. Thanks!
Upvotes: 4
Views: 310
Reputation: 3481
Is the application designed to use plugins? If so I would argue that it's interface (and lack thereof) is poorly designed. The application should provide a library that plugins like yours can leverage without having to resort to link-time workarounds and hacks.
Upvotes: 1
Reputation: 71525
I think you want the "-R" (aka. "--just-symbols") option for the linker.
So if myexe
is the master exectuable:
g++ -Wl,--just-symbols=myexe -L... -l... -shared -o plugin.so plugin.o
P.S. This presupposes that myexe
itself was linked with --export-dynamic
. Otherwise, your .so cannot refer to symbols inside the executable because they are not exported.
Upvotes: 1
Reputation: 5054
One approach is to not let the plugin link directly to the symbols. dlopen() is capable of loading the current process as a "library", using a path of NULL, but this requires -rdynamic flag to be set in GCC. This is i.e. how Glade works, as you set the callbacks in an XML file of sorts and it just "magically" works.
A different method is to pass a set of function pointers to the plugin so it doesn't have to link to them. This of course requires the plugin architecture to be designed for this kind of thing.
Upvotes: 2