When compiling a C program in Unix, why do I get "ld: Unresolved:"?
When compiling a C program in Unix, an "ld: Unresolved:" error indicates that your program uses a symbol from a library which is not being linked. To solve this problem, add the following to any flags you already use when compiling your program:
-lname
Replace name with name of the appropriate library, for
example:
Above, test1.c is the name of the C program, and the
library referenced is /usr/lib/libm.a, which is an
archive (static) library. The -l flag is passed to the
linker so it can find the needed code.
Notes:
- Only the part of the library filename after
libbut before the period (.) is used with the-lflag. Thus, the file/usr/lib/libm.ais called by the flag-lm.
- Also, in most C compilers the
-loption must follow the name of your source file in the C compiler command line.
The -l option performs a different function than the
#include directive. When you include the header file
associated with a library by placing an #include
directive in the program itself, this only instructs the compiler to
include the statements from that file. It does not automatically
cause the linker to search that library when linking your program.
If you are not sure what library contains the unresolved symbols,
you can use the Unix ar command to list the contents of a
static library. Alternatively, you can use the nm command
to obtain a name list of either a shared object or archive library.
To search for specific routines, combine these commands with the
grep command. For example, to search the list of the
symbols in the math library for those containing cos, you
could enter any of the following:
If the library you need is not in one of the directories normally
searched by the linker, you must use the -L flag before
the -l in the compile line (linkers are position
dependent), for example:
One of the defaults of the cc command gives the location
of the standard include directory -I/usr/include. To see
all the defaults, use the -v verbose flag, for example:
For more information, see the man pages on cc
(or gcc) and ld.
At Indiana University, to get support for personal or departmental Linux or Unix systems, see At IU, how do I get support for Linux or Unix?
Also see:
- At IU, what C++ compilers are available on the UITS central systems?
- At IU, what C compilers are available on the UITS central systems?
- On Libra, how can I link a C job to the IMSL C Library and execute it?
- What is a linker, and what are dynamic and static linking?
Last modified on August 22, 2008.






