212 Chapter 6
Shared Library Management Routines
Initializers for Shared Libraries
C source for lib3.sl (file lib3.c):
lib3()
{
printf(“lib3\n”);
}
#pragma init “lib3_init”
void
lib3_init()
{
printf(“lib3_init\n”);
}
#pragma fini “lib3_fini”
void
lib3_fini()
{
printf(“lib3_fini\n”);
}
Commands used to build these libraries:
$ cc +DD64 lib1.c lib2.c lib3.c main.c -c;
$ ld -b lib3.o -o lib3.sl;
$ ld -b +init lib2_init +fini lib2_fini lib2.o -o lib2.sl;
$ ld -b +init lib1_init +fini lib1_fini lib1.o ./lib3.sl -o \
lib1.sl;
$ cc -L. +DD64 main.o -l1 -l2 -lc;
Output from running a.out:
lib2_init
lib3_init
lib1_init
lib1
lib2
lib3
lib1_fini
lib3_fini
lib2_fini
Ordering Within an Executable or Shared Library
Multiple initializers/terminators within the same load module (an
executable or shared library) are called in an order following these rules:
• Inits in .o (object) files or .a (archive) files are called in the reverse
order of the link line.
• Finis in .o or .a files are called in forward order of the link line.