Compiling a shared library

For use with dynamic linking

To compile a shared library that uses dynamic linking:

  1. Compile each source file into an object file, with no linking. For example:
    xlc -c test1.c -o test1.o
  2. Optional: Create an export file listing the global symbols to be exported, by doing one of the following:
    • Use the CreateExportList utility, described in Exporting symbols with the CreateExportList utility.
    • Use the -qexpfile compiler option with the -qmkshrobj option. The -qexpfile option saves all exported symbols from a list of given object files in a designated file. For example:
      xlc -qmkshrobj -qexpfile=exportlist test1.o test2.o
    • Manually create an export file using a text editor. You can edit an export file to include or exclude global symbols from the target shared library.
  3. Use the -qmkshrobj option to create a shared library from the generated object files.
    • If you created an export file in step 2, use the -bE linker option to use your global symbol export list. If you do not specify a -bE option, all the global symbols are exported IBM extensionexcept for those symbols that have the hidden or internal visibility attribute.IBM extension
    For example:
    xlc -qmkshrobj -o mySharedObject.o test1.o test2.o -bE:exportlist
    Notes:
    • The default name of the shared object is shr.o, unless you use the -o option to specify another name.
    • Exporting some functions (such as restf# where # is a number) might cause incorrect execution. If any files in the shared library use floating point and are compiled with the -qtwolink option, do not export the restf# or equivalent floating-point utility functions.
  4. Optional: Use the AIX® ar command to produce an archive library file from multiple shared or static objects. For example:
    ar -rv libtest.a mySharedObject.o myStaticObject.o
  5. Link the shared library to the main application, as described in Linking a library to an application.

For use with runtime linking

To create a shared library that uses runtime linking:

  1. Follow steps 1 and 2 in the procedure described above.
  2. Use the -G option to create a shared library from the generated object files, and to enable runtime linking with applications that support it.
    • If you created an export file, use the -bE linker option to use your global symbol export list. If you do not specify a -bE option, all the global symbols are exported IBM extensionexcept for those symbols that have the hidden or internal visibility attribute.IBM extension
    For example:
    xlc -G -o libtest.so test1.o test2.o -bE:exportlist  
  3. Link the shared library to the main application using the -brtl option, as described in Linking a library to an application.

Dynamic loading of a shared library

Shared libraries built for either dynamic or runtime linking can be dynamically loaded. See the AIX documentation for more information about using the dynamic loading routines:
Related external information