Compiling a shared library

To compile a shared library that uses static linking:

  1. Compile each source file into an object file, with no linking. For example:
    xlc -c foo.c -o foo.o
  2. Optionally, 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, to create the basis for the export file used in the real link step. For example:
      xlc -qmkshrobj -qexpfile=exportlist foo.o
    • Manually create the export file. If necessary, in a text editor, edit the export file to control which symbols will be exported when you create the shared library.
  3. Create the shared library from the desired object files, using the -qmkshrobj compiler option and the -bE linker option if you created an export file in step 2. If you do not specify a -bE option, all symbols will be exported. (If you are creating a shared library from C++ object files, you can also assign an initialization priority to the shared library, as described in Assigning priorities to objects.) For example:
    xlc -qmkshrobj foo.o -o mySharedObject -bE:exportlist
    (The default name of the shared object is shr.o, unless you use the -o option to specify another name.)

    Alternatively, if you are creating a shared library from C++ object files you can use the makeC++SharedLib utility, described in Creating a shared library with the makeC++SharedLib utility; however, the -qmkshrobj method is preferred as it has several advantages, including the ability to automatically handle C++ template instantiation, and compatibility with the -O5 optimization option.

  4. Optionally, use the AIX® ar command to produce an archive library file from multiple shared or static objects. For example:
    ar -rv libfoo.a shr.o anotherlibrary.so
  5. Link the shared library to the main application, as described in Linking a library to an application.

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, to be linked at load-time, and the -bE linker option to specify the name of the export list file. (You can also use the -qmkshrobj option if you want to specify a priority for a C++ shared object; see Initializing static objects in libraries (C++).) For example:
    xlc -G -o libfoo.so foo1.o foo2.o -bE:exportlist  
  3. Link the shared library to the main application, as described in Linking a library to an application.

C++ If you want the system to perform static initialization when dynamically loading a shared library, use the load and unload functions described in Dynamically loading a shared library.

Exporting symbols with the CreateExportList utility

CreateExportList is a shell script that creates a file containing a list of all the global symbols found in a given set of object files. Note that this command is run automatically when you use the -qmkshrobj option, unless you specify an alternative export file with the -qexpfile command.

The syntax of the CreateExportList command is as follows:

Read syntax diagramSkip visual syntax diagram
>>-CreateExportList--+-----+--exp_list--+- -f--file_list-+--+-----+--+-------------+-><
                     '- -r-'            '-obj_files------'  '- -w-'  |      .-32-. |   
                                                                     '- -X--+-64-+-'   

You can specify one or more of the following options:

-r
If specified, template prefixes are pruned. The resource file symbol (__rsrc) is not added to the resource list.
exp_list
The name of a file that will contain a list of global symbols found in the object files. This file is overwritten each time the CreateExportList command is run.
-ffile_list
The name of a file that contains a list of object file names.
obj_files
One or more names of object files.
-w
Excludes weak symbols from the export list.
-X32
Generates names from 32-bit object files in the input list specified by -f file_list or obj_files. This is the default.
-X64
Generates names from 64-bit object files in the input list specified by -f file_list or obj_files.
The CreateExportList command creates an empty list if any of the following are true:
  • No object files are specified by either -f file_list or obj_files.
  • The file specified by the -f file_list parameter is empty.
Related external information