I came here because i'm in trouble with something strange.
I have a code that load a dll and retrieve a function from this dll.
I work on both windows and linux.
Everything work like a charm in linux, but when windows come, some problems follow!
I use Clion as IDE, when i compile my code and launch it from Clion, everything work, my dll is loaded, the function was fetched, all are ok.
But when i launch my application from it's directory, my app can't find the dll.
This is my current code for the loader (in fact this is normally something simple) :
void dllLoader(const char *libPath){
void* handle = dlopen(libPath, RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
std::cout << "Cannot open library: " << dlerror() << std::endl;
}
else{
std::cout << "library loaded" << std::endl;
dlclose(handle);
}
}
int main(int argc, char **argv) {
dllLoader("RenderModule.dll");
return 0;
}
And as i say, running this from my IDE was good (i get "library loaded" output) but when i run in directory and manually run the application,
i get the "cannot open library : RenderModule.dll No such file or directory" output.
The target dll and the binary are stored in the same directory.
I use CMake 3.3 and Mingw to compile this code.
The stranger thing was when i check the running path of my application and i try to read the dll ,using std::ifstream and i can read the dll correctly! there probably something i don't get...
I don't know what to do to fix this problem, someone has an idea ?
If you need the dll code, i'll edit this post, but i don't think it was usefull because if this dll was loaded in linux/windows(under ide) i think the problem doesn't come from the dll, but i can be wrong.
Thank you in advance and, sorry for my english :/
Have a nice day !
I changed my dllLoader
function to use the Win32 API:
void dllLoader(const char *libPath){
HMODULE handle = LoadLibrary(libPath);
if (!handle) {
std::cout << "Cannot open library: " << GetLastError() << std::endl;
}
else{
std::cout << "library loaded" << std::endl;
FreeLibrary(handle);
}
}
As I expected it falls in the first if branch and GetLastError returns error code 127. After some searching, this error code was retrieved is, in some cases, because the library dependencies can't be found.
In the past, when I tried to execute my first application compiled with CLion outside the IDE (manually, through Windows Explorer, it was a simple "hello world", so no reason to fail)
I got some error like "...unresolved symbols..", the compiler don't link all needed library at linking time, so your binary
always depends on external libs.
I need to put
set(GCC_CXX_FLAGS ${GCC_CXX_FLAGS} "-static-libgcc -static-libstdc++ -static")
Flag into my CMake config file to build my binary with all dependencies imported in it.
After some testing, the problem was the same here, my DLL wasn't built with all dependencies and it depends on some other dll to run correctly. The update of GCC_CXX_FLAGS resolved the problem for the application that loads the dll but not for the dll itself. I don't know if there exists a similar flag to compile a library using cmake that let linker import all dependencies in my dll, but I know what I need to search.
@Alexey Omelchenko, you point something interesting in your comment, I have run an app inside and outside my IDE that just shows the PATH env variable. The only difference was inside the IDE the PATH as C:\MingW\bin folder, so it confirms what I was thinking, this is a dependencies problem.