Reputation: 133
If I load a dll or so library at runtime using dlopen() on unix or LoadLibrary() on windows, do i need to ensure that the symbols in the library do not have the same names as symbols in my program? Also is it possible to load multiple shared libraries that define the same functions?
Edit: I am specifically asking about runtime dynamic linking.
Upvotes: 5
Views: 789
Reputation: 224082
Objects in a shared library loaded by dlopen
do not appear in the namespace of the main program. You need to call dlsym
with the name of the object as a string to get a pointer to it.
Using your example of having two libraries dynamically loaded having a global with the same name:
void *lib1 = dlopen("lib1.so", RTLD_LAZY);
int *lib1_global1 = dlsym(lib1, "global1");
void *lib2 = dlopen("lib2.so", RTLD_LAZY);
int *lib2_global1 = dlsym(lib2, "global1");
Here, both lib1.so and lib2.so contain a global variable of type int
named global1
. Because the dlsym
function returns a pointer to the variable/function in question, you can handle this case with no conflict.
Upvotes: 2