473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

dlopen - calling functions

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
3 5294
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
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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christopher Armstrong | last post by:
With the following situation, Numarray can't find Python's symbols. problematic-Python:: Main dlopens Two Two dynlinks Python Python dlopens Numarray Numarray dynlinks Python I have another...
1
by: Attila.Rajmund.Nohl | last post by:
Hello! I'm using KAI C++ Compiler (KCC) Version 4.0d on Sparc Solaris 8 with Sun WorkShop 6 update 2 backend (KCC compiles C++ code to C than uses the Sun compiler to produce machine...
2
by: Manuel Maria Diaz Gomez | last post by:
Hi Everybody, maybe you can give me a hint about this. I implemented a simple singleton Factory that will (among other things) keep a registry of objects. Objects register themself in the...
2
by: Dave | last post by:
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;
5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
12
by: edware | last post by:
I've seen the dlopen() function in code I've read, is that a C or C++ function? Is it a standard function? In case it is a topical question, what is the advantage of using it instead of let the...
10
by: Unknown Soldier | last post by:
Hello, I have a program that makes use of external plugins, implemented as shared libraries loaded at runtime through a call to dlopen(). The libraries export symbols which the main program...
12
by: tvnaidu | last post by:
I am getting this link error, I am using mongoose web server with Linux, any idea?: mongoose.c:3114: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from...
6
by: tvnaidu | last post by:
opening shared lib with dlopen, gets success, but dlsym returns an error saying undefined symbol, also passing "-rdynamic" during linking this shared lib, any idea?. dlopen success, dlsym gets error,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.