LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 08-25-2009, 03:45 AM   #1
er_khatke
LQ Newbie
 
Registered: Aug 2009
Posts: 2
Thanked: 0
Question reverse dependency problem while dlopen shared lib


[Log in to get rid of this advertisement]
Hi everyone ,
I am using loading a shared library dynamically in my code using dlopen() & then calling a function of it which in turn calling the function of my own program .But this is giving "undefined reference" problem .Please see the following snap of the code :

Code for main Program :
==================================
/*sample.c*/
#include <dlfcn.h>
#include "sample_header.h"
void main_fun(int k)
{
printf("\n ***** In Main Fun : %d",k);
}
void main()
{
char *error;
void (*sample_fptr)(int);
void *handle = dlopen ("./sample_shared.so", RTLD_LAZY);
if (!handle)
{
printf ("\n%s\n", dlerror());
return;
}

dlerror();
sample_fptr = dlsym(handle, "shared_fun");
if ((error = dlerror()) != 0)
{
printf ("\n%s\n", dlerror());
return;
}

(*sample_fptr)(7);
printf("Done ........\n");

}
compilation command used : gcc -ldl sample.c
==============================================================
------------------------
//sample_header declaration is :

/*sample_header.h*/
#include <stdio.h>
void main_fun(int k);

--------------------------


Code for sample_shared.c :
++++++++++++++++++++++++++++++
#include "sample_header.h"
void shared_fun(int k);
void shared_fun(int k)
{
main_fun(k);
}

compilation command : gcc -shared -Wall sample_shared.c -o sample_shared.so
++++++++++++++++++++++++++++

Result @ runtime : ./sample_shared.so: undefined symbol: main_fun

Any suggestion to resolve this issue is appreciated.

Thanx in Advance
Kapil
linuxfedora er_khatke is offline  
Tag This Post , , ,
Reply With Quote
Old 08-25-2009, 09:48 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: openSUSE 11.1; Pebble; PNE-LE; LFS (no book)
Posts: 382
Thanked: 12
You need to use -rdynamic and -fPIC.

Here are the compilation commands to use:

gcc -o sample -ldl -fPIC -rdynamic sample.c

gcc -shared -rdynamic -fPIC -Wall -o sample_shared.so sample_shared.c
orgcandman is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
undefined refrence while reverse dependency in shared library.. er_khatke Linux - Newbie 1 08-25-2009 05:24 AM
xine-lib dependency problem slackhack Fedora 2 04-23-2007 11:41 AM
problem with opening shared library using dlopen() in Red Hat Linux phani@tcs Linux - General 1 01-10-2006 04:49 PM
PAM unable to open dlopen(/lib/security/pam_securetty.so) NavForum Debian 0 07-30-2005 05:10 PM


All times are GMT -5. The time now is 09:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration