By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

dlopen - calling functions

P: n/a
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
ma**@userve.net
http://www.userve.net/
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Matt Churchyard wrote:
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.

--
Bertrand Mollinier Toublet
"Reality exists" - Richard Heathfield, 1 July 2003

Nov 13 '05 #2

P: n/a
Matt Churchyard wrote:
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.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #3

P: n/a
>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).
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);
}
----------------------------------------------------------------------------


--
- Jan Engelhardt
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.