Unable to dynamically access a function using dlopen

Unable to dynamically access a function using dlopen

Post by mshet » Tue, 21 May 2002 22:43:24



Hi,

     I am trying to execute the following program with calls to dlopen,
     dlsym and dlclose on Solaris 2.8 with CC 5.0.
     #include <dlfcn.h>
     #include <stdio.h>
     int main()
     {
         void *handle;
         char nameDll[255];
         sprintf(nameDll, "/usr1/try/libprog.so");
         handle = dlopen(nameDll, RTLD_NOW);

        printf("handle=%d\n", handle);

         void (*func)();

        func  = (void (*)(void))dlsym(handle, "myfunc");

         // Invoke method
          (*func)();

         dlclose(handle);
         return 0;
     }

     On execution the program throws a Segmentation Fault. The value of
     func is 0. The library prog libprog.cpp contains

     #include <iostream.h>
     void myfunc()
     {
         cout << "Function: myfunc" << endl;
         cout << "From libprog.cpp" << endl;
     }

     I am compiling libprog.cpp in the following manner:
     CC -KPIC libprog.cpp -G -o libprog.so

     Kindly let me know if I have missed out on something.

     Thanks and Regards,
     M Shetty

     PS : The program works if I try to locate a function from libm.so like
     sqrt.

 
 
 

Unable to dynamically access a function using dlopen

Post by Drazen Kac » Tue, 21 May 2002 22:47:54



>       #include <iostream.h>
>       void myfunc()
>       {
>           cout << "Function: myfunc" << endl;
>           cout << "From libprog.cpp" << endl;
>       }

>       I am compiling libprog.cpp in the following manner:
>       CC -KPIC libprog.cpp -G -o libprog.so

>       Kindly let me know if I have missed out on something.

Your function needs to have C linkage.

--
 .-.   .-.    I don't work here. I'm a consultant.
(_  \ /  _)

     |