ARCHIVED: When compiling a C program in Unix, why do I get "ld: Unresolved:"?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

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:

  cc test1.c -lm

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 lib but before the period (.) is used with the -l flag. Thus, the file /usr/lib/libm.a is called by the flag -lm.
  • Also, in most C compilers the -l option 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:

  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 the -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 cc (or gcc) and ld.

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document aevh in the Knowledge Base.
Last modified on 2018-01-18 12:13:18.