By Plugin.
We mean a library that is loaded vi dlopen()
and its symbols resolved via dlsym()
(not a standard shard library that is dynamically loaded by the runtime system).
Referring to http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/. The document was last updated in 2006. It recommends the use of extern "C"
to prevent mangling of function names, so that dlsym
can find its functions with relative ease.
Is this still relevant for dynamic libraries? In my particular case, I am trying to create a dynamic library on OSX using libtool. Perhaps using __attribute__ ((constructor))
is more hip and modern, I have had little success discovering the recommended practice.
I'm pretty sure extern "C"
is still the best way to export unmangled functions from C++ code. Using it across several platforms without issues.
If you plan to load a library manually dlopen()
and use dlsym()
to retrieve and resolve functions/objects then using a extern "C" name becomes much easier. It is a pain for mangled names.
Thus if you want easier portability/usabilty then use an C (extern "C") interface.
But you should consider the cons to exposing a C (extern "C") interface.
This means you can not expose any of your C++ objects directly via the interface. Thus you are loosing a lot of functionality like RAII initially. You should compensate for this by writing extra wrapper calls to wrap the calls to your C interface
The original question was about shared libraries (only via comments did we find it was about plugin shared libraries).
So the names are unmanageable.
This is not a problem if the compiler/runtime are resolving the symbols.
Do you plan on using multiple compilers? as this is the only reason I can see for exporting a C interface (as different C++ compiler often use different ABI).
If you are using the same compiler then why not use the C++ interface.
Personally on any given platform I only use one compiler and I only use the shared objects on that platform. If the compiler is upgraded something bigger has happened I am re-build all my libraries anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With