Operating System - HP-UX
1832218 Members
1807 Online
110041 Solutions
New Discussion

Dynamic loading of Shared Library problem

 
Sujoy
Occasional Advisor

Dynamic loading of Shared Library problem

Hi,
I am trying to create a shared libray in HP-UX which i intend to load dynamically using dlopen()/dlsym() calls.

The library code is as follows:
===============================
bar.c
---------
#include
#include

void gprint ()
{
fprintf ( stderr, "Hello, Shared World!\n" );
}

And the corresponding main is given as below:-
=============================================
foo.c
------
#include
#include
#include

int main()
{
void * handle = NULL;
void(*bar)(); // pointer to the gprint() //function in the shared lib
double (*cosine)(double);
char *error;

handle = dlopen ( "./bar.so", RTLD_LAZY ); // open the shared lib

// if the open failed, NULL was returned. Print the error code
if ( handle == NULL ) {
fprintf ( stderr, "fail 1: %s\n", dlerror() );
}
// the open succeeded, try and get the print() function from the shared lib
else {
fprintf ( stderr, "%x\n", handle );
bar = (void(*)())(dlsym ( handle, "gprint" ));

// if bar is NULL, print() wasn't found in the lib, print error message
if ( bar == NULL ) {
fprintf ( stderr, "fail 2: %s\n", dlerror() );
} else {

// print the address of the print() function, then call it
fprintf ( stderr, "%d\n", bar );
bar ( );
}

}
handle = dlopen ("/usr/lib/libm.sl", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}

cosine =(double (*)(double)) dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}

printf ("%f\n", (*cosine)(90.0));
dlclose(handle);

return 0;
}

Here, i compiled bar.c as follows:
aCC +z -b bar.c -o bar.so
and foo.c as: -
aCC +z foo.c -o foo

But when i try to run this as ./foo i see that
dlopen("./bar.so"), succeeds w/o error but when dlsym(handle,"gprint") called it returns NULL, and i see dlerror o/p as "Unresolved Symbol: gprint"
Ironically, the dlsym() call to cos() in /usr/lib/libm.sl succeeds w/o error.

I am using 64 bit HP-UX 11i and my SHLIB_PATH includes the directory where bar.so is present.

The "nm bar.so" o/p is given below:-
Symbols from bar.so:

Name Value Scope Type Subspace

$PIC$0 | 4808|static|code |$CODE$
C$3 | 4872|static|data |$LIT$
__StaticCtorTable_End|1073746024|static|data |$DATA$CDTORCEND$
__StaticCtorTable_Start|1073746008|static|data |$DATA$CDTORC1$
__iob | |undef |data |
__shlInit | |undef |code |
__shlInit | 4728|uext |stub |
__shlinit | -4|uext |stub |
__shlinit | |undef |code |
_shlInit | 4712|extern|entry |
_shlInit | 4744|extern|code |$CODE$
fprintf | |undef |code |
fprintf | 4792|uext |stub |
gprint__Fv | 4808|extern|entry |
gprint__Fv | 4808|extern|code |$CODE$
static___soa_bar_c_ |1073746008|static|data |$DATA$CDTORC1$
static___soa_shlInit_C_|1073746016|static|data |$DATA$CDTORC1$

I am new to this dynamic loading, so any help will be very much appreciated.

--Sujoy
6 REPLIES 6
MAUCCI_2
Frequent Advisor

Re: Dynamic loading of Shared Library problem

Hi,

your problem is a pure C++ name mangling pb.
Your usage of dl stuff seems good.

Create a bar.h file like this
bar.h
----
extern "C" {
void gprint ();
}
----

include it in your bar.c and that's it.

++Cyrille
Stanimir
Trusted Contributor

Re: Dynamic loading of Shared Library problem

Hi!

Try in compilation of this module to use
"-fpic" instead of "+z".
This work with gcc.

Regards.


Stephen Keane
Honored Contributor

Re: Dynamic loading of Shared Library problem

Have you tried compiling using +Z (i.e. uppercase Z, not lowercase z) ?

Your code works without modification or an extra header file when compiled using gcc.
MAUCCI_2
Frequent Advisor

Re: Dynamic loading of Shared Library problem

It is normal for this code to work with gcc, since gcc is a C compiler.
It also works as is with HPUX ANSIC cc compiler since it is also a C compiler.

It doesn't work as is (and needs the modification I referred to in my initial post) because aCC is a C++ compiler and will mangle names.

Try with g++ and it won't work until you modify the code as I said.


BR
Cyrille
Sujoy
Occasional Advisor

Re: Dynamic loading of Shared Library problem

Hi Cyrille,
Your earlier solution of extern "C"{} worked with aCC and served my purpose.
Thanks a lot.
--Sujoy
Sujoy
Occasional Advisor

Re: Dynamic loading of Shared Library problem

The solution given by Cyrille to use extern "C"{} to avoid name mangling when compiled with aCC has worked for me.
---Thanks everybody.