Expert C Programming

(Jeff_L) #1
the routines that a library contains. More about this in the next heuristic!

Handy Heuristic


How to Match a Symbol with its Library

If you're trying to link a program and get this kind of error:

ld: Undefined symbol


_xdr_reference


*** Error code 2


make: Fatal error: Command failed for target 'prog'


Here's how you can locate the libraries with which you need to link. The basic plan
is to use nm to look through the symbols in every library in /usr/lib, grepping for the
symbols you're missing. The linker looks in /usr/ccs/lib and /usr/lib by default, and
so should you. If this doesn't get results, extend your search to all other library
directories (such as /usr/openwin/lib), too.

% cd /usr/lib


% foreach i (lib?*)


? echo $i


? nm $i | grep xdr_reference | grep -v UNDEF


? end


libc.so


libnsl.so


[2491] | 217028| 196|FUNC |GLOB |0 |8


|xdr_reference


libposix4.so


...


This runs "nm" on each library in the directory, to list the symbols known in the
library. Pipe it through grep to limit it to the symbol you are searching for, and filter

out symbols marked as "UNDEF" (referenced, but not defined in this library). The


result shows you that xdr_reference is in libnsl. You need to add -lnsl on


the end of the compiler command line.


  1. Symbols from static libraries are extracted in a more restricted way than symbols
    from dynamic libraries

Free download pdf