一枚老菜鸟 吐槽基地:http://xuxiake1981.blog.hexun.com/
分类: C/C++
2012-12-16 13:59:31
1、程序连接指定连接库时的顺序要求:A库若调用了B库的程序,则A库应先被连接
The linker searches libraries in the order in which they are specified on the command line - the link order. Link order is important in that a library containing an external reference to another library must precede the library containing the definition. This is why libc is typically the last library specified on the linker command line: because the other libraries preceding it in the link order often contain references to libc routines and so must precede it.
2、使用LPATH设置连接库的默认路径:Overriding the Default Linker Search Path with LPATH
The LPATH environment variable allows you to specify which directories ld should search. If LPATH is not set, ld searches the default directory /usr/lib. If LPATH is set, ld searches only the directories specified in LPATH; the default directories are not searched unless they are specified in LPATH.
If set, LPATH must contain a list of colon-separated directory path names that ld must search. For example, to include /usr/local/lib in the search path after the default directories, set LPATH as follows:
$ LPATH=/usr/lib:/usr/local/lib //Korn and Bourne shell syntax.
$ export LPATH
3、使用 - L 选项添加连接库的路径
The -L option to ld also allows you to add additional directories to the search path. If -L libpath is specified, ld searches the libpath directory before the default places.
For example, suppose you have a locally developed version of libc, which resides in the directory /usr/local/lib. To make ld find this version of libc before the default libc, use the -L option as follows:
$ ld prog.o -L /usr/local/lib -lc
Multiple -L options can be
specified. For example, to search /usr/contrib/lib and /usr/local/lib before
the default places:
$ ld prog.o -L /usr/contrib/lib -L /usr/local/lib –lc
4、使用(-l)指定要连接的库
When writing programs that call routines not found in the default libraries linked at compile time, you must specify the libraries on the compiler command line with the -lx option. For example, if you write a C program that calls POSIX math functions, you must link with libm.
The x argument corresponds to the identifying portion of the library path name - the part following lib and preceding the suffix .a or .sl. For example, for the libm.sl or libm.a library, x is the letter m:
$ cc -Aa mathprog.c -lm
$ ld /opt/langtools/lib/hpux32/crt0.o -u main prog.o -L/usr/mylibs \ -l:libfoo.a -l:libc.a -l:libm.a
另外,-l:与-l基本相同,但前者能够忽略-a 对于archive库还是shared库的指定。
5、压缩连接阶段,只生成中间文件.o:Suppressing the Link-Edit Phase with -c
The -c compiler option suppresses the link-edit phase. That is, the compiler generates only the .o files and not the a.out file. This is useful when compiling source files that contain only subprograms and data. These may be linked later with other object files, or placed in an archive or shared library. The resulting object files can then be specified on the compiler command line, just like source files. For example:
$ f90 -c func.f //Produce .o for func.f.
6、用 –o 选项重命名输出文件
The -o name option causes ld to name the output file name instead of a.out. For example, to compile a C program prog.c and name the resulting file sum_num:
$ cc -Aa -o sum_num prog.c //Compile using -o option.
$ sum_num //Run the program.
Enter a number to sum: 5
The sum of 1 to 5: 15
7、用 -a 指定连接archive lib(.a)还是shared lib(动态库.so,.sl) The following command links with the archive versions of libcurses, libm, and libc:
$ ld /opt/langtools/lib/hpux32/crt0.o prog.o -a archive -lcurses -lm -lc
参考: