Ask a Question related to UNIX Programming, Design and Development.
-
Matt Churchyard #1
dlopen - calling functions other way round
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.
Passing a pointer for the getnumber() function is out of the question as the
real application
contains 30+ api functions which may all need to be accessible.
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
[email]matt@userve.net[/email]
[url]http://www.userve.net/[/url]
Matt Churchyard Guest
-
Calling other functions
All, Using .NET 1.1, I have built a custom control, however I am having trouble compiling it. For the on click event of the button, I want it to... -
calling functions with include
suppose i have... <?php include("common_functions.php"); fxn1(); fxn2(); ?> i want fxn1 and fxn2 to be on the common_functions.php page,... -
Calling functions between frames...
JavaScript can not run on server side. You can include it in the output of your PHP script. "Albert Finchly" <Albert_Finchly@Yahoo.co.uk>... -
Calling functions?
Hello, I wrote a jscript function within the HTML window of my webpage and would like to call it from inside the ASPX code-behind page. Any... -
calling functions
how do i call a vbscript function (which is between <%%>) from a vbscript script function (which is between script tags-<script... -
Jens.Toerring@physik.fu-berlin.de #2
Re: dlopen - calling functions other way round
Matt Churchyard <matt@userve.net> wrote:
You must tell the linker that your main program exports symbols. If you're> 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.
e.g. the gcc and ld combination you need to do
gcc -Wl,-E -o load load.c -ldl
where the -Wl,-E tells gcc to invoke the linker with the -E option,
which in turn will make ld add all symbols to the dynamic symbol
table, so that they can be found by the dlopen'ed library. Of
course, you may need some other flags when you're using a different
compiler and linker.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| [email]Jens.Toerring@physik.fu-berlin.de[/email]
_ | | | | | |
| |_| | | | | | [url]http://www.physik.fu-berlin.de/~toerring[/url]
\___/ens|_|homs|_|oerring
Jens.Toerring@physik.fu-berlin.de Guest
-
Måns Rullgård #3
Re: dlopen - calling functions other way round
[email]Jens.Toerring@physik.fu-berlin.de[/email] writes:
gcc -rdynamic does the same thing.>>> 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.
> You must tell the linker that your main program exports symbols. If you're
> e.g. the gcc and ld combination you need to do
>
> gcc -Wl,-E -o load load.c -ldl
>
> where the -Wl,-E tells gcc to invoke the linker with the -E option,
> which in turn will make ld add all symbols to the dynamic symbol
> table, so that they can be found by the dlopen'ed library. Of
> course, you may need some other flags when you're using a different
> compiler and linker.
--
Måns Rullgård
[email]mru@users.sf.net[/email]
Måns Rullgård Guest
-
Matt Churchyard #4
Re: dlopen - calling functions other way round
thanks,
its all works perfectly with the -rdynamic switch
"Måns Rullgård" <mru@users.sourceforge.net> wrote in message
news:yw1xhe5kl1it.fsf@users.sourceforge.net...my> [email]Jens.Toerring@physik.fu-berlin.de[/email] writes:
>> >> I am using the dlopen,dlsym functions to load in an external module inand> >> program which works fine. I can call functions inside the loaded modulefrom> >> all is well.
> >> The problem is, when i try and call a function inside the main programof> >> the module,
> >> it won't load giving 'Undefined symbol "getnumber"' errors. I can thinkyou're> >> >> no way round this.
> > You must tell the linker that your main program exports symbols. If>> > e.g. the gcc and ld combination you need to do
> >
> > gcc -Wl,-E -o load load.c -ldl
> >
> > where the -Wl,-E tells gcc to invoke the linker with the -E option,
> > which in turn will make ld add all symbols to the dynamic symbol
> > table, so that they can be found by the dlopen'ed library. Of
> > course, you may need some other flags when you're using a different
> > compiler and linker.
> gcc -rdynamic does the same thing.
>
> --
> Måns Rullgård
> [email]mru@users.sf.net[/email]
Matt Churchyard Guest



Reply With Quote

