To compile a static (unshared) library:
For example:
xlc -c bar.c example.c ar -rv libfoo.a bar.o example.o
To create a shared library that uses static linking:
xlc -c foo.c -o foo.o
xlc -qmkshrobj -qexpfile=exportlist foo.o
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.
ar -rv libfoo.a shr.o anotherlibrary.so
To create a shared library that uses run-time linking:
xlc -G -o libfoo.so foo1.o foo2.o -bE:exportlist
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.
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:
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.
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/ ,如需转载,请注明出处,否则将追究法律责任。