dlopen - calling functions

M

Matt Churchyard

Now I realize this is off topic, and will probably quickly receive a list
of replys from people who are quicker to criticize posters than give them
help but if you know of a more suitable newsgroup i will be happy to go
there instead.

I am using the dlopen,dlsym functions to load in an external module in my
program
which works fine. I can call functions inside the loaded module and all is
well.
The problem is, when i try and call a function inside the main program from
the module,
it won't load giving 'Undefined symbol "getnumber"' errors. I can think of
no way round this.
Below is the test program I am using.

load.c
----------------------------------------------------------------------------
#include <stdio.h>
#include <dlfcn.h>
#include "load.h"

typedef int (*pointer)();

int main(void)
{
void *modhandle;
pointer symhandle;

modhandle = dlopen("./testmod.so", RTLD_LAZY);

if( modhandle == NULL )
{
printf("Load failed \"%s\"\n", dlerror());
exit(1);
}

symhandle = (pointer)dlsym(modhandle, "testfunc");

if( symhandle == NULL )
{
printf("Sym failed \"%s\"\n", dlerror());
exit(1);
}

(*symhandle)();

dlclose(modhandle);
}

int getnumber()
{
return 10;
}
----------------------------------------------------------------------------

load.h
----------------------------------------------------------------------------
int getnumber();
----------------------------------------------------------------------------

testmod.c
----------------------------------------------------------------------------
#include <stdio.h>
#include "load.h"

int testfunc()
{
int i;

i = getnumber();

printf("Hello %d\n", i);
}
----------------------------------------------------------------------------

--

Regards,
Matt Churchyard
_______________________________
Project Development Manager
Userve Internet
(e-mail address removed)
http://www.userve.net/
 
B

Bertrand Mollinier Toublet

Matt said:
Now I realize this is off topic, and will probably quickly receive a list
of replys from people who are quicker to criticize posters than give them
help but if you know of a more suitable newsgroup i will be happy to go
there instead.

I am using the dlopen,dlsym functions to load in an external module in my
program which works fine. I can call functions inside the loaded module and all is
well. The problem is, when i try and call a function inside the main program from
the module, it won't load giving 'Undefined symbol "getnumber"' errors. I can think of
no way round this. Below is the test program I am using.

This is somewhat off-topic indeed in that there must be some linker
(eventually linker) specific way of saying that that function you call
is not going to be bundled with the library.

Now, from a design point of view, it doesn't make sense to let your
library call a function from your main program. The idea should be that
your library offers an interface that your program is able to use as it
wishes. On the other hand there is no dependency of your library on your
main program.
 
C

Chris Dollin

Matt said:
Now I realize this is off topic, and will probably quickly receive a list
of replys from people who are quicker to criticize posters than give them
help but if you know of a more suitable newsgroup i will be happy to go
there instead.

I am using the dlopen,dlsym functions to load in an external module in my

comp.unix.programmer.
 
J

Jan Engelhardt

Now I realize this is off topic, and will probably quickly receive a list
of replys from people who are quicker to criticize posters than give them
help but if you know of a more suitable newsgroup i will be happy to go
there instead.

Compile the main program (or both main+module) with the switch -rdynamic (when
using GCC).
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top