Is there a way to expose a symbol only to dlsym
and hide it during static and dynamic linking?
What I'd like to do is allow for object files that can be used either as shared libraries or plugins without modification. If the object files are going to be usable as libraries, symbol collisions have to be avoided. Unfortunately, this means that when used as plugins, the name of the function that dlsym
will use to initialize the plugin has to be different for each plugin. CPython solves this by relating the symbol name dlsym
will look for to the filename of the shared object. This is an ok solution but it breaks if the shared objects are renamed.
Instead, I'd prefer for plugins to expose a common initialization function name to dlsym
but for this name to be ignored if the shared object is being dynamically linked. (In that case, initialization would be done some other way.)
Edit: I was mistaken about the premise of the question. With dynamic libs A and B that export identical symbols, if a program links to both of them it doesn't cause a linker error.