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


Question

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);
}
1
128
4/7/2015 5:33:32 PM

Accepted Answer

You have to link against libdl, add

-ldl

to your linker options

230
6/5/2009 4:00:43 PM

@Masci is correct, but in case you're using C (and the gcc compiler) take in account that this doesn't work:

gcc -ldl dlopentest.c

But this does:

gcc dlopentest.c -ldl

Took me a bit to figure out...


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon