HP-UX Linker and Libraries User's Guide
}
C Source for libtwo.c
#include <stdio.h>
void load() /* called after libtwo.so loaded */
{
printf("libtwo.so loaded\n");
}
void unload() /* called after libtwo.so unloaded */
{
printf("libtwo.so unloaded\n");
}
extern void _INITIALIZER();
void (*init_ptr)() = _INITIALIZER;
void foo()
{
printf("foo called\n");
}
void bar()
{
printf("bar called\n");
}
Following are the commands used to build these libraries:
$ cc -Aa -c libunits.c
$ ld -b -o libunits.so +I _INITIALIZER libunits.o
$ cc -Aa -c libtwo.c
$ ld -b -o libtwo.so +I _INITIALIZER libtwo.o
The following is an example program that loads these two libraries:
C Source for testlib2.c
#include <stdio.h>
#include <dl.h>
main()
{
float (*in_to_cm)(float), (*gal_to_l)(float), (*oz_to_g)(float);
void (*foo)(), (*bar)();
shl_t hndl_units, hndl_two;
/*
* Load libunits.so and find the required symbols:
*/
if ((hndl_units = shl_load("libunits.so", BIND_IMMEDIATE, 0)) == NULL)
perror("shl_load: error loading libunits.so"), exit(1);
if (shl_findsym(hndl_units, "in_to_cm",
TYPE_PROCEDURE, (void *) in_to_cm))
perror("shl_findsym: error finding in_to_cm"), exit(1);
if (shl_findsym(hndl_units, "gal_to_l",
TYPE_PROCEDURE, (void *) gal_to_l))
perror("shl_findsym: error finding gal_to_l"), exit(1);
if (shl_findsym(hndl_units, "oz_to_g",
TYPE_PROCEDURE, (void *) oz_to_g))
perror("shl_findsym: error finding oz_to_g"), exit(1);
/*
* Load libtwo.so and find the required symbols:
*/
if ((hndl_two = shl_load("libtwo.so", BIND_IMMEDIATE, 0)) == NULL)
perror("shl_load: error loading libtwo.so"), exit(1);
if (shl_findsym(hndl_two, "foo", TYPE_PROCEDURE, (void *) foo))
perror("shl_findsym: error finding foo"), exit(1);
if (shl_findsym(hndl_two, "bar", TYPE_PROCEDURE, (void *) bar))
perror("shl_findsym: error finding bar"), exit(1);
/*
* Call routines from libunits.so:
Initializers for Shared Libraries 147