Read carefully the dlopen(3) man page.
If filename contains a slash ("/"), then it is interpreted as a
(relative or absolute) pathname. Otherwise, the dynamic linker
searches for the library as follows:
So you should run
./dyna ./libfoo.so
or
./dyna $PWD/libfoo.so
or you could set LD_LIBRARY_PATH
to contain .
(which is not recommended)
P.S. don't forget to use dlerror()
for error reporting, and remember that dlopen
is probably not re-entrant (so if you have a multi-threaded application, serialize the calls to dlopen
and dlsym
with some mutex).