[an error occurred while processing this directive] Node:Libraries order, Next:, Previous:Which library, Up:Compiling

8.10 DJGPP uses a one-pass linker

Q: I give all the libraries to gcc, but I still get unresolved externals when I link. What gives?

A: Ld is a one-pass linker: it only scans each library once looking for unresolved externals it saw until that point. This means the relative position of object files and libraries' names on the command line is significant. You should put all the libraries after all the object files, and in this order:

 -lstdcxx -lm

E.g., to link files main.o and sub.o into a C++ library, use the following command line:

 gcc -o main.exe main.o sub.o -lstdcxx -lm

or, if you compile and link in one command:

 gcc -o main.exe main.cc sub.cc -lstdcxx -lm

If you have any libraries of your own, put them before the above system libraries, like this:

 gcc -o main.exe main.cc sub.cc -lmylib -lstdcxx -lm

When you use the gpp or the gxx drivers to compile a C++ program, it automatically names the C++ libraries in the correct order. (gpp and gxx are the alternative names for g++ on DOS, which doesn't allow the + character in file names.)

You can also force the linker to repeatedly scan a group of libraries until all externals are resolved. To this end, put the names of these libraries between the -( and the -) options (if you invoke GCC to link, use the -Wl or -Xlinker options to pass switches to the linker). Check out the linker docs for more info about -( ... -) groups.

If your installation tree is different from the default, i.e., if you keep the libraries not in the default lib/ subdirectory, then you should add that directory to the line in the [gcc] section of your DJGPP.ENV file which starts with LIBRARY_PATH, or put into your environment a variable called LIBRARY_PATH and point it to the directory where you keep the libraries. Note that if you invoke the linker by itself (not through the gcc driver), then LIBRARY_PATH will have no effect, because this variable is only known to the gcc driver. Invoking ld directly is not recommended, but if you must do it, use the -L option to tell it where to look for the libraries.


[an error occurred while processing this directive]