How to specify the path of libraries?

Andi Hellmund mail@andihellmund.com
Sun Jun 28 10:02:00 GMT 2009


Hey,
> Hello,
>
> I'm trying to build my own GNU/Linux system.
>
> Until now I just did the following:
> - created a directory called "tools"
> - manually compiled glibc, binutils and gcc and installed them into
> the "tools" directory (the only options passed to the various
> configure scripts were --prefix, --build, --host and in some cases
> --target)
>
> The problem is that all the binaries/libraries installed in "tools"
> are linked against the libraries in the host system. All I want to do
> now is to break this link and make the system contained in "tools"
> self-sufficient. In other words, I'd like to recompile binutils and
> gcc so that they link to the libraries installed in "tools" itself.
>
To accomplish your task you should use the -L  and -Wl,-rpath  option to
rebuild binutils and gcc.  The -L option specifies  which library paths
should be used by gcc during linking - this would then select your
libraries in the "tools" directory when gcc/collect2/ld searches for
unresolved symbol references. As a note: this is only used during
compilation/linking. If you now run the ldd command on the executable,
you'll see that the default (host) system libraries will still be used
at runtime. To get around this, -Wl,-rpath specifies which paths should
be searched by the  runtime linker.

The default case:

andi@roma:~/Programming/c$ gcc prog.c -o prog
andi@roma:~/Programming/c$ ldd prog
    linux-gate.so.1 =>  (0xb7fa1000)
    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7e12000)      <--
standard system directory
    /lib/ld-linux.so.2 (0xb7f87000)


The extended case:

andi@roma:~/Programming/c$ gcc
-Wl,-rpath,/home/andi/Development/glibc/build2.9 prog.c -o prog
andi@roma:~/Programming/c$ ldd prog
    linux-gate.so.1 =>  (0xb80a9000)
    libc.so.6 => /home/andi/Development/glibc/build2.9/libc.so.6
(0xb7f44000)
    /lib/ld-linux.so.2 (0xb808f000)
andi@roma:~/Programming/c$ readelf -d prog

Dynamic section at offset 0xf18 contains 22 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 0x0000000f (RPATH)                      Library rpath:
[/home/andi/Development/glibc/build2.9]
 [ output omitted ... ]

In the default case, the RPATH value is not set within the ELF executable!


There is also a way to solve the runtime linking search path problem
without recompiling your executables. This might be inappropriate when
you try to build an own linux system / distribution, but you could set
the LD_LIBRARY_PATH environment variable to your "tools" directory which
then force the dynamic linker to search the given paths LD_LIBRARY_PATH
before the standard system paths.

By using a "tools" directory for your own GNU/Linux system, you deviate
from the default locations in Linux systems (/usr/bin, /usr/local/bin,
...). In this case, you could/should think of probably rebuilding the
dynamic linker (ld-linux.so which is part of the glibc project) with
modified standard system search paths. But  unfortunately, I don't know
how to change this. I think you need to modify the configure.in /
configure.ac file and rerun the automake tools. But you could ask the
glibc mailing list for answers: libc-help@sourceware.org

Hope that helps,
Andi



More information about the Gcc-help mailing list