www.delorie.com/djgpp/faq/dev-probs/missing-libs.html   search  
When I compile my program, the linker complains about mathematical functions, although I did #include <math.h>.

The linker complains it cannot find cprintf function.

Why do I get so many unresolved symbols when linking C++ programs?

By default, gcc instructs the linker to only look in two libraries: libgcc.a and libc.a. Some functions aren't included there, so the linker can't find them. For math functions, like sin() and exp(), append -lm to the gcc command line; for pc-specific functions, like cputs() and cprintf() append -lpc; to use C++ classes append -lgpp. GPL library routines, like obstack and regex packages are in libgpl.a library; append -lgpl to use them.

You can use the nm program to check what functions are included in a library. Run it with -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 to 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
  U _iob
  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(), _flsbuf() and _iob() 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.

Note that some C++ classes use math routines, so the -lm should be given after the -lgpp.


  webmaster     delorie software   privacy  
  Copyright © 1995     Updated Feb 1995