ITPub博客

首页 > 应用开发 > IT综合 > XL C/C++ V7.0 (for AIX) Compiling and linking a library

XL C/C++ V7.0 (for AIX) Compiling and linking a library

原创 IT综合 作者:mingheinfo 时间:2011-01-08 05:53:00 0 删除 编辑

Compiling a static library

To compile a static (unshared) library:

  1. Compile each source file into an object file, with no linking.
  2. Use the AIX ar command to add the generated object files to an archive library file.

For example:

xlc -c bar.c example.c
ar -rv libfoo.a bar.o example.o 

Compiling a shared library

To create 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 run-time 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:

>>-CreateExportList--+-----+--exp_list--+- -f--file_list-+--+-------------+-><
                     '- -r-'            '-obj_files------'  |      .-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.
-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.

Linking a library to an application

You can use the same command string to link a static or shared library to your main program. For example:

xlc -o myprogram main.c -Ldirectory -lfoo

where directory is the path to the directory containing the library.

If your library uses run-time linking, add the -brtl option to the command:

xlc -brtl -o myprogram main.c -Ldirectory -lfoo

By using the -l option, you instruct the linker to search in the directory specified via the -L option for libfoo.so; if it is not found, the linker searches for libfoo.a. For additional linkage options, including options that modify the default behavior, see the AIX ld documentation.

Linking a shared library to another shared library

Just as you link modules into an application, you can create dependencies between shared libraries by linking them together. For example:

xlc -qmkshrobj [-G] -o mylib.so myfile.o -Ldirectory -lfoo
[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25191105/viewspace-1044275/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论
  • 博文量
    4
  • 访问量
    13805