Node:Which library, Next:, Previous:Unresolved externals, Up:Compiling

8.9 How not to lose your head with all these libraries

Q: I'm lost with all those different libraries. How in the world can I find out which functions are included in which library?

A: You can use the nm program to check what functions are included in a library. Run it with the -C option and with the library as its argument and look in the output for the name of your function (the -C, or --demangle option makes the function names look closer to what they are called in the source file). Functions which have their code included in the library have a capital T before their name. For example, the following is a fragment from the listing produced by nm:

    c:\djgpp\lib> nm --demangle libc.a
    .
    .
    .
    stdio.o:
    000000e4 b .bss
    000000e4 d .data
    00000000 t .text
    00000098 t L12
    0000001e t L3
    00000042 t L6
    0000004d t L7
    0000006a t L9
    00000000 t __gnu_compiled_c
	     U _filbuf
	     U _flsbuf
    00000000 T clearerr
    000000ac T feof
    000000c2 T ferror
    000000d8 T fileno
    0000000c T getc
    00000052 T getchar
    0000002a T putc
    0000007c T putchar
    00000000 t gcc2_compiled.
    .
    .
    .

Here we see that the module stdio.o defines the functions clearerr, feof, ferror, fileno, getc, getchar, putc and putchar, and calls functions _filbuf and _flsbuf which aren't defined on this module.

Alternatively, you can call nm with the -s or --print-armap, which will print an index of what symbols are included in what modules. For instance, for libc.a, we will see:

    c:\djgpp\lib> nm --print-armap libc.a
    .
    .
    .
    _feof in stdio.o
    _ferror in stdio.o
    _fileno in stdio.o
    .
    .
    .

which tells us that the functions feof, ferror and fileno are defined in the module stdio.o.

nm is fully described in the GNU docs. See GNU Binutils Manual.