Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

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:

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 At IU, how do I get support for Linux or Unix?

This is document aevh in domain all.
Last modified on August 22, 2008.

I need help with a computing problem

  • 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.



Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

I have a comment for the Knowledge Base

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.