If you do not want to export paths, there is an alternative solution:
with your toolchain in PATH :
gcc -dumpspecs > specsfile
edit the specsfile and in the link section add your -rpath :
*link: %{!static:--eh-frame-hdr} -m %(link_emulation) %{shared:-shared} %{!shared: %{!static: %{rdynamic:-export-dynamic} -dynamic-linker %(dynamic_linker)} %{static:-static}} -rpath /usr/local/lib64
at this point you can check if it works with:
g++ -specs=specsfile test.cpp readelf -d a.out |grep RPATH
if it works, you can make it permanent (no need to go through -specs every time)
strace -fF -o /tmp/g++.log g++ test.cpp grep specs /tmp/g++.log
grep should show the paths by which gcc looks for the default specs file.
The specs files are flexible enough to allow conditional binding depending on variables, for example:
{!static: %{mabi=n32:-rpath-link %R/lib32:%R/usr/lib32} %{mabi=64:-rpath-link %R/lib64:%R/usr/lib64} %{mabi=32:-rpath-link %R/lib:%R/usr/lib}}
different and multiple paths should be used depending on mabi (not yet verified), %R must be a sysroot , can be changed with the necessary full path.
There is also --with-specs= gcc configure option , which will eventually be used during build , itβs not yet clear to me how to use the link section (work on it) .
--with-specs="%{shared:-Wl,-rpath -Wl,$(DESTDIR)/lib}%{!shared:-Wl,-rpath -Wl,$(DESTDIR)/lib}"
This works, I used both shared and not !shared just for the test, maybe a more reasonable condition should be used, note that this is not reported with -dumpspecs .
While reading some branch of the gcc mailing list, I got the impression that not everyone likes specs (but, if I'm not mistaken, 4.9 add another parameter --with-extra-specs ), instead, the preferred way to make such settings is to configure.host , but I did and donβt look at it, have fun! :-)
See also: gcc faq rpath
update above
I donβt know if you can install the predefined rpath , maybe you could be in the ld binutils linker not in gcc/g++ , but why would you do that?
Just export LD_LIBRARY_PATH at run time and LD_RUN_PATH at build time
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH ldd a.out
ldd should show the paths you exported.
To quote the message given when creating the shared library using libtool:
If you ever want to communicate with the installed libraries in this directory, LIBDIR, you must either use libtool and specify the full path to the library, or use the [-LLIBDIR] flag during linking and perform at least one of the following:
- add LIBDIR to the environment variable LD_LIBRARY_PATH at run time
- add LIBDIR to the environment variable 'LD_RUN_PATH' at build time
- use the linker flag '-Wl, - -rpath -Wl, LIBDIR'
- ask your system administrator to add LIBDIR to /etc/ld.so.conf
For more information, see Any documentation on the operating system about shared libraries, for example the man pages ld (1) and ld.so (8).
for completeness, I used the Makefile for testing, all configuration parameters, environment variables (see boot ldflags) that I tried did not work, --enable-rpath .
use with mkdir ~/gcc copy the below Makefile to ~/gcc then cd ~/gcc && make build-gcc
Please note that the parameters are used only for this test, do not use as a reference.
FETCH = aria2c --file-allocation=none -c -d dl NICE = nice -n 19 PARALLEL = -j4 DESTDIR = $(HOME)/gcc/install SRCDIR = $(HOME)/gcc/src all: