How to build and install gcc with built-in rpath?

I am trying to create and install my own gcc 4.7.2 in / usr / local to use instead of gcc 4.4.6 in / usr. (This applies to CentOS 6.3.)

gcc creates executables and dynamic libraries that dynamically link their own dynamic libraries, for example. libstdc ++. So. How to create and install gcc so that the generated binaries automatically receive the -rpath link (-rpath / usr / local / lib64), which links the dynamic libraries in / usr / local / lib 64, and not in / usr / lib 64 or / lib64?

If it works correctly, after creating the executable with gcc without specifying "-Wl, -rpath = / usr / local / lib64", when I have the ldd executable, it should show / usr / local / lib64 / libstdC ++. So. 6 instead of / usr / lib 64 / libstdc ++. So.6. Similarly for libgcc_s.so.1.

I tried various approaches, including specifying LDFLAGS_FOR_TARGET = -Wl, -rpath = / usr / local / lib64, -rpath = / usr / local / lib on the 'configure' command line, but nothing worked.

+9
source share
2 answers

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: # if more downloads are added just remove {dl,src}/*-my-stamp not the .bak # the .bak should avoid to rebuild targets because of timestamp touch_stamp = if [ -f $@.bak ]; then \ touch -r $@.bak $@ ; \ else \ touch $@ $@.bak ; \ fi dl/dl-my-stamp: $(FETCH) https://ftp.gnu.org/gnu/gcc/gcc-4.7.2/gcc-4.7.2.tar.bz2 $(FETCH) http://ftp.gnu.org/gnu/gmp/gmp-4.3.2.tar.bz2 $(FETCH) ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz $(FETCH) https://ftp.gnu.org/gnu/mpfr/mpfr-2.4.2.tar.bz2 $(FETCH) --check-certificate=false http://www.mirrorservice.org/sites/sourceware.org/pub/binutils/snapshots/binutils-2.24.51.tar.bz2 \ ftp://sourceware.org/pub/binutils/snapshots/binutils-2.24.51.tar.bz2 $(touch_stamp) untar_dep = src/untar-my-stamp src/untar-my-stamp: dl/dl-my-stamp mkdir -p src tar -C src -xjf dl/gcc-4.7.2.tar.bz2 tar -C src -xjf dl/gmp-4.3.2.tar.bz2 tar -C src -xzf dl/mpc-0.8.1.tar.gz tar -C src -xjf dl/mpfr-2.4.2.tar.bz2 tar -C src -xjf dl/binutils-2.24.51.tar.bz2 $(touch_stamp) define configure-rule $(1)_install = $(DESTDIR)/$(1)-install-my-stamp $(1)_builddir = $$($(1)_dir)/build $(DESTDIR)/$(1)-install-my-stamp: $$($(1)_deps) mkdir -p $$($(1)_builddir) cd $$($(1)_builddir) && \ $$($(1)_env) ../configure --cache-file=$(SRCDIR)/$(1)-config.cache \ $$($(1)_configure) $(NICE) make -C $$($(1)_builddir) $$($(1)_make_target) $(PARALLEL) ifneq ($$($(1)_post_make),) $$($(1)_post_make) endif touch $$@ .PHONY: build-$(1) clean-$(1) build-$(1): $$($(1)_install) clean-$(1): -rm -fr $$($(1)_builddir) endef gmp_dir = src/gmp-4.3.2 gmp_env = CONFIG_SITE=$(SRCDIR)/config.site gmp_configure = --prefix=$(DESTDIR) \ --disable-shared --enable-static --enable-cxx gmp_deps = $(untar_dep) gmp_make_target = install $(eval $(call configure-rule,gmp)) mpfr_dir = src/mpfr-2.4.2 mpfr_env = CONFIG_SITE=$(SRCDIR)/config.site mpfr_configure = --prefix=$(DESTDIR) \ --disable-shared --enable-static \ --with-gmp=$(DESTDIR) mpfr_deps = $(untar_dep) $(gmp_install) mpfr_make_target = install $(eval $(call configure-rule,mpfr)) mpc_dir = src/mpc-0.8.1 mpc_env = CONFIG_SITE=$(SRCDIR)/config.site mpc_configure = --prefix=$(DESTDIR) \ --disable-shared --enable-static \ --with-gmp=$(DESTDIR) --with-mpfr=$(DESTDIR) mpc_deps = $(untar_dep) $(gmp_install) $(mpfr_install) mpc_make_target = install $(eval $(call configure-rule,mpc)) gcc_dir = src/gcc-4.7.2 gcc_env = CONFIG_SITE=$(SRCDIR)/config.site \ CFLAGS="-I/usr/include/i386-linux-gnu" \ CXXFLAGS="-I/usr/include/i386-linux-gnu" gcc_configure = --prefix=$(DESTDIR) \ --libdir=$(DESTDIR)/lib \ --with-local-prefix=$(DESTDIR) \ --with-gmp=$(DESTDIR) --with-mpfr=$(DESTDIR) \ --with-mpc=$(DESTDIR) \ --disable-bootstrap \ --enable-languages=c,c++ \ --disable-libgomp --disable-multilib \ --disable-libmudflap --disable-libssp \ --disable-libquadmath \ --enable-rpath \ MAKEINFO=missing gcc_deps = $(untar_dep) $(gmp_install) $(mpfr_install) $(mpc_install) gcc_make_target = gcc_post_make = make -C $(gcc_builddir) install $(eval $(call configure-rule,gcc)) binutils_dir = src/binutils-2.24.51 #binutils_env = LDFLAGS=-Wl,-rpath\ $(DESTDIR)/lib binutils_env = CONFIG_SITE=$(SRCDIR)/config.site \ CFLAGS="-I/usr/include/i386-linux-gnu" \ BOOT_LDFLAGS="-rpath-link=$(DESTDIR)/lib -rpath=$(DESTDIR)/lib" binutils_configure = --prefix=$(DESTDIR) \ --libdir=$(DESTDIR)/lib \ --with-gmp=$(DESTDIR) \ --enable-rpath binutils_deps = $(untar_dep) $(gmp_install) #binutils_make_target = install binutils_post_make = make -C $(binutils_builddir) install $(eval $(call configure-rule,binutils)) .PHONY: env env: @echo export PATH=$(DESTDIR)/bin:\$$PATH @echo export LIBRARY_PATH=/usr/lib/i386-linux-gnu 
+6
source

Using --with-boot-ldflags = "-Wl, - rpath, / some / path" at the setup stage seems to work for me on gcc 4.8.3. The docs discuss this a bit https://gcc.gnu.org/install/configure.html, but the idea is that by default gcc creates a multi-stage build (rebuilds itself) and after the first step uses a different set of ldflags from what I can tell , the first stage obeys LDFLAGS and LDFLAGS_FOR_TARGET, but later stages do not)

0
source

Source: https://habr.com/ru/post/1436225/


All Articles