How to use dlopen()???

D

Dave

Hi all...
I wrote a simple program to test the dlopen()...\

#include <dlfcn.h>

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

handle = dlopen ("plugins/test", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}

dlerror(); /* Clear any existing error */
....
I also made a libtest.so in the plugins/test dir.
But the compiler give me the message as follow.

inst.h:21: warning: useless keyword or type name in empty declaration
/tmp/cc2c6Rb1.o(.text+0x27): In function `main':
: undefined reference to `dlopen'
/tmp/cc2c6Rb1.o(.text+0x3e): In function `main':
: undefined reference to `dlerror'
/tmp/cc2c6Rb1.o(.text+0x64): In function `main':
: undefined reference to `dlerror'
/tmp/cc2c6Rb1.o(.text+0x6f): In function `main':
: undefined reference to `dlclose'
collect2: ld returned 1 exit status

what's happened????
 
D

David Resnick

Dave said:
Hi all...
I wrote a simple program to test the dlopen()...\

#include <dlfcn.h>

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

handle = dlopen ("plugins/test", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}

dlerror(); /* Clear any existing error */
...
I also made a libtest.so in the plugins/test dir.
But the compiler give me the message as follow.

inst.h:21: warning: useless keyword or type name in empty declaration
/tmp/cc2c6Rb1.o(.text+0x27): In function `main':
: undefined reference to `dlopen'
/tmp/cc2c6Rb1.o(.text+0x3e): In function `main':
: undefined reference to `dlerror'
/tmp/cc2c6Rb1.o(.text+0x64): In function `main':
: undefined reference to `dlerror'
/tmp/cc2c6Rb1.o(.text+0x6f): In function `main':
: undefined reference to `dlclose'
collect2: ld returned 1 exit status

what's happened????

comp.programmer.unix or gnu.gcc.help are better places for questions
about dynamic libraries if I read the tea leaves above right.
They are off topic here.

But for the generic question of why symbols can't be found, a generic
answer
of "you need to link against the library that exports them" fits OK
here I guess.

-David
 
J

Jonathan Bartlett

inst.h:21: warning: useless keyword or type name in empty declaration
/tmp/cc2c6Rb1.o(.text+0x27): In function `main':
: undefined reference to `dlopen'
/tmp/cc2c6Rb1.o(.text+0x3e): In function `main':
: undefined reference to `dlerror'
/tmp/cc2c6Rb1.o(.text+0x64): In function `main':
: undefined reference to `dlerror'
/tmp/cc2c6Rb1.o(.text+0x6f): In function `main':
: undefined reference to `dlclose'
collect2: ld returned 1 exit status

Offtopic, but I'd add a -ldl to your gcc command line.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top