PPP 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.
-l
option 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 -lsocket
and -lnsl
will
need to be added to the cc/gcc link line. Another common library is the
math library, which requires an -lm
.-L
option takes a directory to look in for libraries that are not in
/usr/lib. For example when compiling X11 code either -L/usr/openwin/lib
or -L/usr/local/X/lib
will need to be added to the final cc/gcc link
line.-R
option is similar to the -L
option and should always be used
when the -L
option is used. This places the runtime location of the
library into the binary. Usually the -L
and -R
options will take the
same directories, but that is not a requirement. This option should always
be used when -L is used so LD_LIBRARY_PATH
does not need to be set. Many
Makefiles leave off the -R
option, but have a variable where it could be
added easily. Wherever you see a -Lwhatever
, just add a -Rwhatever
to
that variable. LD_RUN_PATH can also be used at compile time instead of -R
to do the same thing.LD_LIBRARY_PATH
This 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:
xfig
in SunOS not to work, among other programs. Also can cause very
serious problems in Solaris if a /usr/ucblib
library is found, for
example, compiling a curses program is impossible if /usr/ucblib
is
in it.LD_LIBRARY_PATH
is the slower
things will be.-R
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.
ECE/CIS • University of Delaware — All Rights Reserved • Newark, DE 19716 • USA • 2015 • Website by AndrĂ© Rauh • Maintained by Labstaff
Comments • Contact Us • Accessibility Notice • Legal Notices