One of the more confusing things when compiling code is telling the compiler where libraries are so it can link the code and create an executable binary.
-loption tells the compiler to link in the library that follows it. If some call needs extra libraries it is usually mentioned in the man page for it. For example, when programming sockets
-lnslwill need to be added to the cc/gcc link line. Another common library is the math library, which requires an
-Loption takes a directory to look in for libraries that are not in /usr/lib. For example when compiling X11 code either
-L/usr/local/X/libwill need to be added to the final cc/gcc link line.
-Roption is similar to the
-Loption and should always be used when the
-Loption is used. This places the runtime location of the library into the binary. Usually the
-Roptions will take the same directories, but that is not a requirement. This option should always be used when -L is used so
LD_LIBRARY_PATHdoes not need to be set. Many Makefiles leave off the
-Roption, but have a variable where it could be added easily. Wherever you see a
-Lwhatever, just add a
-Rwhateverto that variable. LD_RUN_PATH can also be used at compile time instead of -R to do the same thing.
LD_LIBRARY_PATHThis environment variable consists of a colon separated list of directories to look in for libraries. It is best to not have it set. Problems with this variable are:
xfigin SunOS not to work, among other programs. Also can cause very serious problems in Solaris if a
/usr/ucbliblibrary is found, for example, compiling a curses program is impossible if
/usr/ucblibis in it.
LD_LIBRARY_PATHis the slower things will be.
The only case that this should be used is with a program available in binary form only, in which case a wrapper script, be sure to make it executable, should be written around it like this:
#!/bin/sh LD_LIBRARY_PATH=/path/to/lib export LD_LIBRARY_PATH exec Binary_prog_real $*
or one can also use (with tcsh or csh):
env LD_LIBRARY_PATH=/path/to/lib Binary_prog
Replace Binary_prog with the name of the program.