mkshrobj

Option Type Default Value #pragma options  C C++
-qoption - - x

Syntax

    -qmkshrobj[=priority]

Purpose
Creates a shared object from generated object files. This option, together with the related options described below, should be used instead of the makeC++SharedLib command.  The advantage to using this option is that the compiler will automatically include and compile the template instantiations in the tempinc directory.

The compiler will automatically export all global symbols from the shared object unless one explicitly specifies which symbols to export with the -bE:, -bexport: or -bexpall options.

The priority suboption has no effect if the you use the xlc command to link with or the shared object has no static initialization.

Suboption
 

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.  If no priority is specified the default priority of 0 is used.  The priority is not used when linking shared objects (using the xlc command) written in C.

Related Options
The following xlC options are optionally used with -qmkshrobj
 

-oshared_file.o Is the name of the file that will hold the shared file information. The default is shr.o.
-qexpfile=filename Saves all exported symbols in filename.  This option is ignored unless xlC automatically creates the export list.
-e name Sets the entry name for the shared executable to name.  The default is -bnoentry.

Notes
If you use -qmkshrobj to create a shared library, the compiler will:

  1. If the user doesn't specify -bE:, -bexport:, -bexpall or -bnoexpall, create an export list containing all global symbols using the CreateExportList script. You can specify another script with the -tE/-B or -qpath=E: options.
  2. If CreateExportList was used to create the export list and -qexpfile was specified, the export list is saved.
  3. Calls the linker with the appropriate options and object files to build a shared object.

Example

The following example shows how to construct a shared library containing two shared objects using the the -qmkshrobj option, and the AIX ar command.  The shared library is then linked with a file that contains the main function.  Different priorities are used to ensure objects are initialized in the specified order.

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

animals.o
(priority 40)
house.C
#pragma priority(20) 
     ...             
   class dog D       
     ...             
#pragma priority(100)
     ...             
   class cat C       
farm.C
     ...             
  class horse H      
     ...             
#pragma priority(500)
     ...             
   class cow W       
zoo.C
     ...           
  class lion L     
     ...           
#pragma priority(50) 
     ...           
  class zebra Z    
     ...             
fish.o
(priority -100)
fresh.C
#pragma priority(-80) 
     ...             
   class trout A     
     ...             
#pragma priority(500)
     ...             
   class bass B      
salt.C
     ...            
#pragma priority(-200)
     ...              
   class shark S      
     ...              
#pragma priority(10)  
     ...              
   class tuna T       
myprogram.C
(priority 0)
      ...
    main () {     
       ... 
  class Cage CAGE 
      ...

      ...

The first part of this example shows how to use the -qpriority=N option and the #pragma priority(N) directive to specify the initialization order for objects within the object files.

The example shows how to make two shared objects: 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 -qmkshrobj=P option is used to specify the priority of the initialization of the shared objects.

The priority values for the shared objects are chosen 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.

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 xlC -qmkshrobj to construct a library animals.o with a priority of 40:
      xlC -qmkshrobj=40 -o animals.o 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 xlC -qmkshrobj to construct a library fish.o with a priority of -100.
      xlC -qmkshrobj=-100 -o fish.o 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 objects animals.o and fish.o, you use the ar command. To produce an archive file, libzoo.a, enter the command:
    ar rv libzoo.a animals.o fish.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.
    libzoo.a  Is the name you specified for the archive file that will contain the shared object files and their priority levels.
    animals.o
    fish.o
    Are the two shared files you created with xlC -qmkshrobj.
  5. To produce an executable file, animal_time, so that the objects are initialized in the order you have specified, enter:
    xlC -oanimal_time myprogram.o -L. -lzoo
  6. The order of initialization of the objects is shown in the following table.
    Order of Initialization of Objects in libzoo.a 
    File Class Object Priority
    Value
    Comment
    "fish.o"   -100 All objects in "fish.o" are initialized first because they are in a library prepared with -qmkshrobj=-100 (lowest priority number, -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 -qmkshrobj=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) 

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



Constructing a Library


Initialize Shared Library


CreateExportList Command
makeC++SharedLib Command
List of Batch Compiler Options and Their Defaults
Options that Define the Compiler Object Code Produced
Equivalent Batch Compile-Link and Incremental Build Options