makeC++SharedLib Command

This section describes the makeC++SharedLib command and provides an example of how to use it to make two shared libraries. It also describes how to combine these two files in a library using the ar command so that object initialization takes place in the specified order.

Options

-X32 Work with object files compiled for 32-bit usage. If the OBJECT_MODE environment variable is not set otherwise, this is the default mode.
-X64 Work with object files compiled for 64-bit usage.
-X32_64 Work with object files compiled for either 64- or 32-bit usage.
-oshared_file.o Name of the file that will hold the shared file information. The default is shr.o.
-bOptions Uses the -b binder options of the ld command.
-Llib_dir Uses the -L option of the ld command to add the directory lib_dir to the list of directories to be searched for unresolved symbols. The ld command is described in the AIX Version 4 Commands Reference.
-llib_name Adds lib_name to the list of libraries to be searched for unresolved symbols.
-p priority Specifies the priority level for the file. priority may be any number from -214782623 (highest priority-initialized first) to 214783647 (lowest priority-initialized last). Numbers from -214783648 to -214782624 are reserved for system use.
-I import_list Uses the -bI option of the ld command to resolve the list of symbols in the file import_list that can be resolved by the binder.
-E export_list Uses the -bE option of the ld command to export the external symbols in the export_list file. If you do not specify -E export_list, a list of all global symbols is generated.
-e file Saves in file the list computed by -E export_list.
-n name Sets the entry name for the shared executable to name. This is equivalent to using the command ld -e name
file.o Name of an object file to put into the shared library.
file.a Name of an archive file to put into the shared library.

 

Example

The following example shows how to construct two shared libraries using the makeC++SharedLib command, and then use the AIX ar command to combine these libraries along with a file that contains the main function so that objects are initialized in the specified order.

The drawing below shows how the objects in this example are arranged in various files.

The first part of this example shows how to use makeC++SharedLib along with the -qpriority=N option and the #pragma priority(N) directive to specify the initialization order for objects in these files.

The example shows how to make two shared libraries: animals.o containing object files compiled from house.C, farm.C, and zoo.C, and fish.o containing object files compiled from fresh.C and salt.C.

The example shows how to specify priorities and use the ar command so that all the objects in fish.o are initialized before the objects in myprogram.o, and all the objects in animals.o are initialized after the objects in myprogram.o. Within animals.o, the objects in zoo.C are initialized before the objects in house.C and farm.C.

To specify this initialization order, follow these steps:

  1. Develop an initialization order for the objects in house.C, farm.C, and zoo.C:

    1. To ensure that the object lion L in zoo.C is initialized before any other objects in either of the other two files, compile zoo.C using a -qpriority=N option with N less than zero so both objects have a priority number less than any other objects in farm.C and house.C:

      xlC zoo.C -c -qpriority=-50
    2. Compile the house.C and farm.C files without specifying the -qpriority=N option (so N=0) so objects within the files retain the priority numbers specified by their #pragma priority(N) directives:

      xlC house.C farm.C -c
    3. Combine these three files in a shared library. Use makeC++SharedLib to construct a library animals.o with a priority of 40:

      makeC++SharedLib -o animals.o -p 40 house.o farm.o zoo.o
  2. Develop an initialization order for the objects in fresh.C, and salt.C:

    1. Compile the fresh.C and salt.C files:

      xlC fresh.C salt.C -c
    2. To assure that all objects in fresh.C and salt.C are initialized before any other objects, use makeC++SharedLib to construct a library fish.o with a priority of -100.

      makeC++SharedLib -o fish.o -p -100 fresh.o salt.o

      Because the shared library fish.o has a lower priority number (-100) than animals.o (40), when the files are placed in an archive file with the ar command, their objects are initialized first.

  3. Compile myprogram.C that contains the function main to produce an object file myprogram.o. By not specifying a priority, this file is compiled with a default priority of zero, and the objects in main have a priority of zero.

    xlC myprogram.C -c
  4. To create a library that contains the two shared libraries, and the program myprogram.o that contains the function main, so that the objects are initialized in the order you have specified, you use the ar command. To produce an archive file, prio_lib.a, enter the command:

    ar rv prio_lib.a animals.o fish.o myprogram.o

    where:

    rv Are two ar options. r replaces a named file if it already appears in the library, and v writes to standard output a file-by-file description of the making of the new library.
    prio_lib.a Is the name you specified for the archive file that will contain the shared library files and their priority levels.
    animals.o
    fish.o
    Are the two shared files you created with makeC++SharedLib.
    myprogram.o Is the name of the file that contains the function main.

    The order of initialization of the objects is shown in the following table.

    Order of Initialization of Objects in priolib.a
    Object Priority
    Value
    Comment
    "fish.o" -100 All objects in "fish.o" are initialized first because they are in a library prepared with makeC++SharedLib -p -100 (lowest priority number, -p -100, specified for any files in this compilation)
    "shark S " -100(-200) Initialized first in "fish.o" because within file, #pragma priority(-200)
    "trout A" -100(-80) #pragma priority(-80)
    "tuna T" -100(10) #pragma priority(10)
    "bass B" -100(500) #pragma priority(500)
    "myprog.o" 0 File generated with no priority specifications; default is 0
    "CAGE" 0(0) Object generated in main with no priority specifications; default is 0
    "animals.o" 40 File generated with makeC++SharedLib with -p 40
    "lion L" 40(-50) Initialized first in file "animals.o" compiled with -qpriority=-50
    "horse H" 40(0) Follows with priority of 0 (since -qpriority=N not specified at compilation and no #pragma priority(N) directive)
    "dog D" 40(20) Next priority number (specified by #pragma priority(20))
    "zebra N" 40(50) Next priority number from #pragma priority(50)
    "cat C" 40(100) Next priority number from #pragma priority(100)
    "cow W" 40(500) Next priority number from #pragma priority(500) (Initialized last)

     

  5. To produce an executable file, animal_time, so that the objects are initialized in the order you have specified, enter:

    xlC prio_lib.a -oanimal_time

    You can place both nonshared and shared files with different priority levels in the same archive library using the AIX ar command.



Overview of Shared Library Initialization
Constructing a Library


Setting Up the Compilation Environment


ar Command