Renaud Bataille Renaud Bataille - 2 days ago 5
C++ Question

Can't load dll when running app manually but work when running app through ide



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 !

Answer


In first, thank you for your response ! and sorry for the time i take (i don't have too much time to spent on this project) I came here cause i have found the solution for my problem, and i think it may be helpful for other people stuck in "dll's hell" :p

I change 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 fall in the first if branch and GetLastError return error code 127. After some search, this error code was retrieved is some case, because the library dependencies can't be found.

In the past when, i try 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 test, the problems was the same here, my DLL wasn't built with all dependencies and it depend and some other dll to run correctly. the update of GCC_CXX_FLAGS resolve the problem for the application that load the dll but not for the dll itself. I don't know if it exist a similar flag to compile 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 oustide my ide that just show the PATH env variable. The only difference was inside the IDE the PATH as C:\MingW\bin folder, so it confirm what i was thinking, this is a dependencies problem.

Again sorry for my English and for my response time and thanks for your help :)
And have a nice day!

Comments