- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Dynamic loading of Shared Library problem
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 01:11 AM
12-27-2004 01:11 AM
Dynamic loading of Shared Library problem
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
- Tags:
- dlsym
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2004 01:38 AM
12-28-2004 01:38 AM
Re: Dynamic loading of Shared Library problem
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
- Tags:
- mangled
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2004 01:53 AM
12-28-2004 01:53 AM
Re: Dynamic loading of Shared Library problem
Try in compilation of this module to use
"-fpic" instead of "+z".
This work with gcc.
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2005 09:25 PM
01-03-2005 09:25 PM
Re: Dynamic loading of Shared Library problem
Your code works without modification or an extra header file when compiled using gcc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2005 11:03 PM
01-03-2005 11:03 PM
Re: Dynamic loading of Shared Library problem
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2005 11:10 PM
01-03-2005 11:10 PM
Re: Dynamic loading of Shared Library problem
Your earlier solution of extern "C"{} worked with aCC and served my purpose.
Thanks a lot.
--Sujoy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2005 11:13 PM
01-03-2005 11:13 PM
Re: Dynamic loading of Shared Library problem
---Thanks everybody.