ARCHIVED: 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:
name with name of the appropriate library, for
cc test1.c -lm
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.
- 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
- Also, in most C compilers the
-loption must follow the name of your source file in the C compiler command line.
-l option performs a different function than the
#include directive. When you include the header file
associated with a library by placing an
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
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
could enter any of the following:
nm -g /usr/lib/libm.so | grep cos nm -g /usr/lib/libm.a | grep cos ar -t /usr/lib/libm.a | grep cos
If the library you need is not in one of the directories normally
searched by the linker, you must use the
-L flag before
-l in the compile line (linkers are position
dependent), for example:
cc test1.c -lm -L/libapps/lapack -llapack
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:
cc test1.c -lm -v
For more information, see the man pages on
At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?
This is document aevh in the Knowledge Base.
Last modified on 2016-12-06 11:10:40.
- Fill out this form to submit your issue to the UITS Support Center.
- Please note that you must be affiliated with Indiana University to receive support.
- All fields are required.