Search code examples
c++linuxqtdynamiccompiler-flags

Qt Creator/Linux: setting compiler/linker option -ldl for dynamic libraries


I'm all googled out! Appreciation to anyone who can help me ...

I have built test.so and wish to call it dynamically using dlopen(), dlsym(), etc defined in dlfcn.h

I can compile my calling program (main.cpp) on the command line without error

g++ main.cpp -ldl -o myprog

Without the -ldl switch, I get the error: undefined reference dlopen(). Also, if the -ldl switch isn't placed after the source file in the g++ argument list, then I get the same error.

My question is this: how can I compile my main.cpp in Qt Creator? Qt Creator reports the same error: undefined reference dlopen()

I've tried directly adding -ldl to the flags in the Makefile (CFLAGS, CXXFLAGS, LFLAGS) but to no avail. (Perhaps the flags are inserted before the source files where -ldl doesn't work?)

Here's my main.cpp :

#include <dlfcn.h>
int main()
{

   void* handle = dlopen("./test.so", RTLD_LAZY);

   return 0;
}

I'm using Qt Creator 2.4.1 on linux

Many thanks :)


Solution

  • In your project_name.pro file, you should add to the LIBS variable like so:

    LIBS += -ldl