LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   reverse dependency problem while dlopen shared lib (https://www.linuxquestions.org/questions/programming-9/reverse-dependency-problem-while-dlopen-shared-lib-750046/)

er_khatke 08-25-2009 02:45 AM

reverse dependency problem while dlopen shared lib
 
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

orgcandman 08-25-2009 08:48 AM

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


All times are GMT -5. The time now is 11:21 PM.