In my directory, I have two files. One is foo.cpp
, and the other is bar.so
. In foo.cpp
, I am attempting to load the library bar.so
:
#include <dlfcn.h>
#include <iostream>
int main()
{
void* handle = dlopen("bar.so", RTLD_NOW | RTLD_GLOBAL);
std::cout << handle << std::endl;
return 0;
}
In this same directory, I then compile the code from the command line with:
g++ foo.cpp -ldl -o test
However, when executing test
, this prints out 0
, and according to the documentation for dlopen
:
If dlopen() fails for any reason, it returns NULL
So why is this returning NULL, when the library file is in the same directory as the CPP file?
Update:
I have now added dlopen()
to my CPP file, and this outputs:
bar.so: cannot open shared object file: No such file or directory
But I don't understand... bar.so
and foo.cpp
are in the same directory, the executable is built in this same directory, and I am in this same directory when I run the executable.
So then, I tried using an absolute path for bar.so
, but I then receive a new error:
invalid ELF header
After a quick google, I think this may be due to my Ubuntu installation. I am actually using a MacBook, and have installed a native copy of Ubuntu (not a virtual machine). It seems that this is causing the problem, but I don't know how to fix it. Perhaps this library file will just not work on MacBook Ubuntu.