|
|
The following are general link editing conventions that you must be familiar with:
$ cc file1.c file2.c file3.c
To link your program statically with libc.a, turn off the dynamic linking default with the -dn option:
$ cc -dn file1.c file2.c file3.cSpecify the -l option explicitly to link your program with any other library. If the library is in the standard place, the command
$ cc file1.c file2.c file3.c -lxwill direct the link editor to search for libx.so, then libx.a in the standard place. Note that, as a rule, it's best to place -l at the end of the command line.
If the library is not in the standard place, specify the path of the directory in which it is stored with the -L option
$ cc -Ldir file1.c file2.c file3.c -lxor the environment variable LD_LIBRARY_PATH
$ LD_LIBRARY_PATH=dir export LD_LIBRARY_PATH $ cc file1.c file2.c file3.c -lxIf the library is a shared object and is not in the standard place, you must also specify the path of the directory in which it is stored with either the environment variable LD_RUN_PATH at link time, or the environment variable LD_LIBRARY_PATH at run time:
$ LD_RUN_PATH=dir export LD_RUN_PATH $ LD_LIBRARY_PATH=dir export LD_LIBRARY_PATHIt's best to use an absolute path when you set these environment variables. Note that LD_LIBRARY_PATH is read both at link time and at run time.
To direct the link editor to search libx.a where libx.so exists in the same directory, turn off the dynamic linking default with the -dn option:
$ cc -dn -Ldir file1.c file2.c file3.c -lxThat command will direct the link editor to search libc.a well as libx.a. To link your program statically with libx.a and dynamically with libc.so, use the -Bstatic and -Bdynamic options to turn dynamic linking off and on:
$ cc -Ldir file1.c file2.c file3.c -Bstatic -lx -BdynamicFiles, including libraries, are searched for definitions in the order they are listed on the cc command line. The standard C library is always searched last.