[comp.unix.aix] How do you make a shared library on RISC System/6000

bengsig@oracle.nl (Bjorn Engsig) (07/26/90)

As known, the libraries /lib/libc.a etc. are shared.  How do you create one
yourself?  If I e.g. have two sources a1.c and a2.c that I want to compile
and put into libshr.a, so that they will be shared between more programs
linked with libshr.a.

I've tried various methods like the ones shown in the ld manual page (in info),
but I keep getting errors when trying to execute the program:

Could not load program <program-name>
Could not load library libshr.a[a1.o]
Error was: Exec format error

I need to know:

 - which ld options (and export files) are needed to make shared object?

 - are there any special ar options needed to put these into an archive?

 - which ld (cc) options are needed to link my program with this shared archive?

 - Do I need to set the LIBPATH environment?

Thanks for any help - in particular a working example.
-- 
Bjorn Engsig,	Domain:		bengsig@oracle.nl, bengsig@oracle.com
		Path:		uunet!mcsun!orcenl!bengsig

marc@stingray.austin.ibm.com (Marc J. Stephenson/140000;1C-22) (07/27/90)

In article <921@nlsun1.oracle.nl> bengsig@oracle.nl (Bjorn Engsig) writes:
>[ experiences with trying to build shared libraries omitted ]
>I need to know:
> - which ld options (and export files) are needed to make shared object?
  -bM:Sre is the important one.  Your export list should consist on whatever
  symbols are directly referenced from your non-shared portion.  One easy
  way to determine that is to try to link without the library, then 
  pick up the unresolved symbols as your export list.  I'm afraid of wording
  this one badly, but another requirement is that the shared library must
  be fully linked.  That is, it can have no unresolved references which
  are not resolved by imports from other libraries.  See the example the
  bottom 
> - are there any special ar options needed to put these into an archive?
  No.
> - which ld (cc) options are needed to link my program with this shared archive?
Nothing different than with normal archives.
> - Do I need to set the LIBPATH environment?
Only if your archive is going to reside in a non-standard place, that is, other
than /lib or /usr/lib.
Otherwise, use LIBPATH=/lib:/usr/lib:$WHEREVER_YOUR_ARCHIVES_ARE, and export
LIBPATH.  If you foul up setting your LIBPATH, you will start getting "Killed"
on normal commands.  You would need to reset your LIBPATH at that time.

>
>Thanks for any help - in particular a working example.
>-- 
>Bjorn Engsig,	Domain:		bengsig@oracle.nl, bengsig@oracle.com
>		Path:		uunet!mcsun!orcenl!bengsig

Follows is a working example (please don't quibble with ar options & rot).
For my application, I have a shared portion (shr.o) which I keep in
library (libshr.a).  My makefile looks like this: (names changed to protect me)

shr.o: ${SHARED_OBJECTS}
	ld -D0 -H512 -T512 -bM:Sre -o shr.o ${SHARED_OBJECTS} /lib/libc.a -bexport:mylib.exp

mylib.a: shr.o
	ar qv mylib.a shr.o

myprog: ${NON_SHARED_OBJECTS} mylib.a
	cc ${CFLAGS} ${NON_SHARED_OBJECTS} mylib.a -o myprog

It also has @echo "		eat death scum", but that's probably
unnecessary. :-)

--
Marc Stephenson (marc@stingray.austin.ibm.com)
DISCLAIMER: The content of this posting is independent of official IBM position.
External: uunet!cs.utexas.edu!ibmaus!auschs!stingray.austin.ibm.com!marc
Internal: marc@stingray.austin.ibm.com 	VNET: MARC at AUSVMQ  T/L: 793-3796