delorie.com is funded by banner ads.
  www.delorie.com/djgpp/v2faq/faq059.html   search  

| Previous | Next | Up | Top |

8.9 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:
      -lgpp -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 -lgpp -lstdcxx

or, if you compile and link in one command:
      gcc -o main.exe main.cc sub.cc -lgpp -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 -lgpp -lstdcxx -lm

When you use the gxx, the gpp or g++ compilation 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. So if you must call ld directly, use the -L option to tell it where to look for the libraries.


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright ⌐ 1998   by Eli Zaretskii     Updated Sep 1998  

Powered by Apache!

You can help support this site by visiting the advertisers that sponsor it! (only once each, though)