I have a shared library written in C++. It exports a visible interface made of extern "C" functions which create, destroy and manipulate opaque types.
Now, I’d like to have a pure C program which uses this library.
Can I do this (platform independently) ? When will the C++ runtime and the C++ static objects get initialized if main is not written in C++ ?
The initialization phase is platform dependent.
In the case of Linux, dynamically loaded libraries can have specially declared symbols that are automatically called by
dlopen()when the library is loaded.See the manpage for
dlopen(3), section The obsolete symbols init() and fini() for more info.Static initializers are implicitly marked as
__attribute__((constructor)), so in general you don’t have to do anything special to have them called when the shared library is loaded. I suspect this is the same or similar on other platforms.