4

According to g++ -print-search-dirs my C++ compiler is searching for libraries in many directories, including ...

  • /lib/../lib/:
  • /usr/lib/../lib/:
  • /lib/:
  • /usr/lib/

Naively, /lib/../lib/ would appear to be the same directory as /lib/ — lib's parent will have a child named lib, "that man's father's son is my father's son's son" and all that. The same holds for /usr/lib/../lib/ and /usr/lib/

  1. Is there some reason, perhaps having to do with symbolic links, that g++ ought to be configured to search both /lib/../lib/ and /lib/?

  2. If this is unnecessary redundancy, how would one go about fixing it?

If it matters, this was observed on an unmodified install of Ubuntu 9.04.

Edit: More information.

The results are from executing g++ -print-search-dirs with no other switches, from a bash shell.

Neither LIBRARY_PATH nor LPATH are output from printenv, and both echo $LPATH and echo LIBRARY_PATH return blank lines.

Thomas L Holaday
  • 13,614
  • 6
  • 40
  • 51
  • A good question - you should see what my MinGW library path looks like, and I have no idea why! –  Jun 13 '09 at 09:28

2 Answers2

4

An attempt at an answer (which I gathered from a few minutes of looking at the gcc.c driver source and the Makefile environment).

These paths are constructed in runtime from:

  1. GCC exec prefix (see GCC documentation on GCC_EXEC_PREFIX)
  2. The $LIBRARY_PATH environment variable
  3. The $LPATH environment variable (which is treated like $LIBRARY_PATH)
  4. Any values passed to -B command-line switch
  5. Standard executable prefixes (as specified during compilation time)
  6. Tooldir prefix

The last one (tooldir prefix) is usually defined to be a relative path: From gcc's Makefile.in

# Directory in which the compiler finds libraries etc.
libsubdir = $(libdir)/gcc/$(target_noncanonical)/$(version)
# Directory in which the compiler finds executables
libexecsubdir = $(libexecdir)/gcc/$(target_noncanonical)/$(version)
# Used to produce a relative $(gcc_tooldir) in gcc.o
unlibsubdir = ../../..
....
# These go as compilation flags, so they define the tooldir base prefix
# as ../../../../, and the one of the library search prefixes as ../../../
# These get PREFIX appended, and then machine for which gcc is built
# i.e i484-linux-gnu, to get something like: 
# /usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../i486-linux-gnu/lib/../lib/
DRIVER_DEFINES = \
-DSTANDARD_STARTFILE_PREFIX=\"$(unlibsubdir)/\" \
-DTOOLDIR_BASE_PREFIX=\"$(unlibsubdir)/../\" \

However, these are for compiler-version specific paths. Your examples are likely affected by the environment variables that I've listed above (LIBRARY_PATH, LPATH)

ASk
  • 4,157
  • 1
  • 18
  • 15
  • @ASk, I checked for LIBRARY_PATH and LPATH in the environment (bash), and neither is defined. Tracing the g++ source should be guaranteed to terminate with the resolution of this mystery; thanks for the clue. – Thomas L Holaday Jun 13 '09 at 13:20
1

Well, theoretically, if /lib was a symlink to /drive2/foo, then /lib/../lib would point to /drive2/lib if I'm not mistaken. Theoretically...

Edit: I just tested and it's not the case - it comes back to /lib. Hrm :(

Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115