Use the -G option (-dy is optional as it is implied by default).
Input relocatable objects should be built from position-independent code. Use the -z text option to enforce this requirement (see "Position-Independent Code").
Establish the shared objects public interface by defining the global symbols that should be visible from this shared object, and reducing any other global symbols to local scope. This definition is provided by the -M option together with an associated mapfile, and is covered in more detail inAppendix B, Versioning Quick Reference.
Use a versioned name for the shared object to allow for future upgrades (see "Coordination of Versioned Filenames").
If the shared object being generated has dependencies on any other shared objects, and these dependencies do not reside in /usr/lib, record their pathname in the output file using the -R option (see "Shared Objects with Dependencies").
Self contained shared objects offer maximum flexibility, and are produced when the object expresses all dependency needs. Use the -z defs to enforce this self containment (see "Generating a Shared Object")
The following example combines the above points:
$ cc -c -o foo.o -Kpic foo.c $ cc -M mapfile -G -o libfoo.so.1 -z text -z defs \ -R /home/lib foo.o -L. -lbar |
If the shared object being generated will be used as input to another link-edit, record within it the shared object's runtime name using the -h option (see "Recording a Shared Object Name").
Make the shared object available to the compilation environment by creating a file system link to a non-versioned shared object name (see "Coordination of Versioned Filenames").
The following example combines the above points:
$ cc -M mapfile -G -o libfoo.so.1 -z text -z defs \ -R /home/lib-h libfoo.so.1 foo.o $ ln -s libfoo.so.1 libfoo.so |
Consider the performance implications of the shared object; maximize shareability (see "Maximizing Shareability") and minimize paging activity (see "Minimizing Paging Activity"), reduce relocation overhead, especially by minimizing symbolic relocations (see "Profiling Shared Objects"), and allow access to data via functional interfaces (see "Copy Relocations").