Node:Unresolved externals, Next:, Previous:DJGPP-specific, Up:Compiling

8.8 Undefined references when linking programs

Q: Why do I get so many undefined references when linking my programs?

Q: Why do I get "Undefined reference to yywrap" when linking programs produced by Flex?

Q: GCC complains that it cannot find -liostream. Where can I find this library?

A: 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. If you need to link against some optional library, say libxy.a, put the library into the DJGPP lib/ subdirectory and append a -lxy to the link command line. The Standard C++ Template classes are in libstdcxx.a (it's called libstdc++.a on Unix); append -lstdcxx. To use the additional GNU C++ classes in the libgpp.a library (it's called libg++.a on Unix systems), append -lgpp. Flex-generated lexical analyzers call functions in the libfl.a library; you need to append -lfl when linking them. Append -lgrx if you are using GRX library, and -lalleg for linking with Allegro.

When linking C++ programs, you should use either the gpp or gxx commands instead of gcc; they will then instruct the linker to also scan the C++ libraries automatically, so you don't have to remember doing that yourself.

Another reason for undefined references when linking C++ programs is that you mix GCC and libstdcxx.a from different releases: they are usually incompatible. In particular, sometimes people install an additional compiler based on (old releases of) GCC, such as GNU Pascal or GNAT, the GNU Ada development environment, and these additional compilers overwrite some of the libraries, like libgcc.a, with older and incompatible versions. You should always make sure you don't mix different releases of the compiler and libraries; if you must install different releases, install them in separate directories and prepare some batch files or shortcuts to set up the environments for each of the compilers by pointing the DJGPP variable to different directories and changing the order of directories in PATH.

If your program uses a lot of floating-point math, or needs math functions beyond those specified in the ANSI/ISO standard, consider appending -lm to your link command line. The basic math functions required by ANSI/ISO standard are included in the libc.a library, but libm.a includes different versions of these functions which sometimes are more accurate or more compatible with widely-accepted standards for numeric computations; libm.a also includes some functions not included in the default library, like Gamma function and Bessel functions, support for different standards of behavior in case of errors, a matherr facility, etc.

Old C++ programs used to be built with the GNU iostream library, libiostream.a. The iostream classes are now part of the standard C++ library, libstdcxx.a, so if you come across a Makefile that passes the -liostream option to the compiler, change that to -lstdcxx instead.

Further problems which cause the linker to fail with C++ programs are discussed in listing libraries in the correct order, and in exceptions and inline functions.