Linux c++ error: undefined reference to 'dlopen'

user101375 picture user101375 · Jun 5, 2009 · Viewed 176.5k times · Source

I work in Linux with C++ (Eclipse), and want to use a library. Eclipse shows me an error:

undefined reference to 'dlopen' 

Do you know a solution?

Here is my code:

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
    void *handle;
    double (*desk)(char*);
    char *error;

    handle = dlopen ("/lib/CEDD_LIB.so.6", RTLD_LAZY);
    if (!handle) {
        fputs (dlerror(), stderr);
        exit(1);
    }

    desk= dlsym(handle, "Apply");

    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }

    dlclose(handle);
}

Answer

Masci picture Masci · Jun 5, 2009

You have to link against libdl, add

-ldl

to your linker options