Create shared library

How to create shared libraries using EGCS1.0 version gcc/g++,
which are installed under ccjlc1:/jlc/local/egcs/bin
( gcc version egcs-2.90.29 980515, egcs-1.0.3 release).
In the example below, shared library, libshared.sl, is created
from a source, source.C.  

Setting of flaggs:
CXXFLAGS =  -shared -fPIC -fPIC -fno-rtti -fno-exceptions 
SOFLAGS  =  -nostdlib -nostartfiles -Wl,-b,-E
LDFLAGS  =  -fPIC -Wl,+s

Compile:
g++ $(CXXFLAGS) source.C -o source.o

Make sharedlib:
g++ source.o $(SOFLAGS) -o libshared.sl
(note)
g++/gcc installed in ccjlc uses Hitachi's ld command.  
Options following -Wl are for it.  -b is to create shared library
and -E is to make symbol exported.   -nostdlib and -nostartfiles
are not to include g++ standard libraries.  Without them, 
shared library can not be created.   If Hitach ld is called 
through gcc/g++, special startup codes are implemented in the
library which is required by ROOT system.

Link Program:
g++ main.o $(LDFLAGS) -L./ -lshared -o main
(note)
+s LD option enables the searches of shared libraries from the
path specified by the environment variable, SHLIB_PATH.
Shared ilb path is specified ( -lshared ) ath the link time,
the program main will search libraries from them.

Usefull commands:
chatr 
      lists included shared libraries and the status of shared lib PATH.
nm 
      lists symbols in the object.  Meaning of the mark is as follows.
      T  symbol is defined in the object.
      U  undefined symbol.

Example of dynamic loading

If shared libaries are included in the library at link time, 
they are loaded automatically when they are needed at run time.

If not, user can load shared libary by himself and executed
by using the syetem libraries, shl_load, shl_findsym, etc.
Following example shows how to load, find symbol and get information
of shared libraries.

#include 
#include 

int call1();
int call1()
{
  int ir;
  shl_t handle ;
  int isym;
  struct shl_descriptor *desc; 
  void *func = (void*)NULL;
  void *null = (void*)NULL;
  struct shl_symbol *symbol;

  int ind;
  int i,j,k;

  handle=shl_load("libshlib3.sl",BIND_DEFERRED,0L);
  printf(" handle is %x\n",handle);
  isym=shl_findsym(&handle, "call3",TYPE_PROCEDURE ,(void*)&func);
  printf(" isym is %d \n",isym);  /* isym=-1 when not found */


  func=NULL;
  handle=shl_load("libEWParameters.sl",(DYNAMIC_PATH | BIND_REFERENCE ,0L);
  printf(" handle is %x\n",handle);
  isym=shl_findsym(&handle, "Initialize__12EWParameters",TYPE_UNDEFINED,(void*)&func); 
  printf(" isym is %d \n",isym);
  printf(" func is %x \n",func);


/*  List loaded shared library */
  desc=(struct shl_descriptor *)malloc(sizeof(struct shl_descriptor ));
  ind=-4;
  while(shl_get(ind,&desc) == 0 ) {
   printf("shl_get.. ind=%d",ind); 
   printf(" file name is %s",desc->filename); 
   printf(" initializer..%x",(int)desc->initializer);
   printf("\n");
   ind++;
  }


/* Get shared lib symbols */

  k= EXPORT_SYMBOLS ;
  i=shl_getsymbols(handle, TYPE_UNDEFINED, k,
    (void *)(*malloc),&symbol);

  printf("Get symbol .. i=%d \n",i);  
  printf("Meaning of Type .. TYPE_PROCEDURE=%d ",TYPE_PROCEDURE);
  printf(" TYPE_DATA=%d TYPE_STORAGE=%d\n",TYPE_DATA,TYPE_STORAGE);
  for( j=0;j



Akiya Miyamoto: Last update 1998-Jul-23