Parent document is top of "comp.unix.aix Frequently Asked Questions (Part 4 of 5)"
Previous document is "Introduction"
Next document is "2.06: Linking my program fails with strange errors. Why?"
2.05: How do I make my own shared library?
To make your own shared object or library of shared objects, you should
know that a shared object cannot have undefined symbols. Thus, if your
code uses any externals from /lib/libc.a, the latter MUST be linked with
your code to make a shared object. Mike Heath (mike@pencom.com) said it
is possible to split code into more than one shared object when externals
in one object refer to another one. You must be very good at
import/export files. Perhaps he or someone can provide an example.
Assume you have one file, sub1.c, containing a routine with no external
references, and another one, sub2.c, calling stuff in /lib/libc.a. You
will also need two export files, sub1.exp, sub2.exp. Read the example
below together with the examples on the ld man page.
---- sub1.c ----
int addint(int a, int b)
{
return a + b;
}
---- sub2.c ----
#include <stdio.h>
void printint(int a)
{
printf("The integer is: %d\n", a);
}
---- sub1.exp ----
#!
addint
---- sub2.exp ----
#!
printint
---- usesub.c ----
main()
{
printint( addint(5,8) );
}
The following commands will build your libshr.a, and compile/link the
program usesub to use it.
$ cc -c sub1.c
$ cc -bM:SRE -bnoentry -bE:sub1.exp -o sub1shr.o sub1.o
$ cc -c sub2.c
$ cc -bM:SRE -bnoentry -bE:sub2.exp -o sub2shr.o sub2.o
$ ar r libshr.a sub1shr.o sub2shr.o
$ cc -o usesub usesub.c -L: libshr.a
$ usesub
The integer is: 13
$
Parent document is top of "comp.unix.aix Frequently Asked Questions (Part 4 of 5)"
Previous document is "Introduction"
Next document is "2.06: Linking my program fails with strange errors. Why?"