centos 如何构建和安装带有内置rpath的gcc?

pkmbmrz7  于 2022-11-07  发布在  其他
关注(0)|答案(2)|浏览(216)

我尝试在/usr/local中构建和安装我自己的gcc 4.7.2,以代替/usr中的gcc 4.4.6。(这是在CentOS 6.3上。)
gcc创建了可执行文件和动态库,它们动态链接到自己的动态库,例如libstdc++. so。如何构建和安装gcc,以便生成的二进制文件自动获得链接器-rpath选项(-rpath /usr/local/lib 64),该选项会导致链接/usr/local/lib 64中的动态库,而不是链接/usr/lib 64或/lib 64中的动态库?
如果它工作正常,在我使用gcc构建一个可执行文件而不指定“-Wl,-rpath=/usr/local/lib 64”之后,当我ldd该可执行文件时,它应该显示/usr/local/lib 64/libstdc++.so.6而不是/usr/lib 64/libstdc++.so.6。libgcc_s.so.1也是如此。
我尝试了不同的方法,包括在'configure'命令行上指定LDFLAGS_FOR_TARGET=-Wl、-rpath=/usr/local/lib 64、-rpath=/usr/local/lib,但没有任何效果。

o0lyfsai

o0lyfsai1#

如果您不想导出路径,还有一个替代解决方案:
PATH中的工具链:

gcc -dumpspecs > specsfile

编辑specsfile,并在link部分中添加-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

此时,您可以测试它是否与以下各项一起工作:

g++ -specs=specsfile test.cpp
readelf -d a.out |grep RPATH

如果它工作,您可以使它成为永久(不需要每次都传递-specs

strace -fF -o /tmp/g++.log g++ test.cpp
grep specs /tmp/g++.log

grep应该显示gcc查找默认specs文件的路径。
specs文件非常灵活,允许根据变量进行条件链接,例如:

{!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}}

应根据mabi使用不同的多个路径(尚未测试),%R应为sysroot路径,可根据需要更改为完整路径。
还有一个--with-specs=选项gcc configure最终将在构建时使用,我还不清楚如何与link部分一起使用(正在研究)。

--with-specs="%{shared:-Wl,-rpath -Wl,$(DESTDIR)/lib}%{!shared:-Wl,-rpath -Wl,$(DESTDIR)/lib}"

它起作用了,我使用了shared而不是!shared只是为了测试,可能应该使用一些更聪明的条件,请注意,它没有与-dumpspecs一起报告。
通过阅读一些线程的gcc邮件列表,我有印象specs不是每个人都喜欢(但如果我没有错4.9添加另一个选项--with-extra-specs),而不是首选的方式来做这样的自定义似乎是configure.host,但我做了,不看它,玩得开心!:-)
另请参阅:gcc常见问题解答rpath

更新以上内容

我不知道你是否可以设置一个预定义的rpath,如果你可以的话,可能会在binutils的链接器ld中,而不是在gcc/g++中,但是你为什么要这样做呢?
只需在运行时导出LD_LIBRARY_PATH,在构建时导出LD_RUN_PATH

export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH
ldd a.out

ldd应该会显示您汇出的路径。
引用使用libtool构建共享库时给出的一条消息:
如果你想链接到一个指定目录LIBDIR中的已安装库,你必须使用libtool,并指定库的完整路径名,或者在链接过程中使用'-LLIBDIR'标志,并至少执行以下操作之一:

  • 在执行期间将LIBDIR添加到'LD_LIBRARY_PATH'环境变量中
  • 在链接期间将LIBDIR添加到'LD_RUN_PATH'环境变量中
  • 使用'-Wl,--rpath -Wl,LIBDIR'连接器标志
  • 请系统管理员将LIBDIR添加到′/etc/ld.so.conf ′中

有关更多信息,请参见有关共享库的任何操作系统文档,如ld(1)和ld.so(8)手册页。

为了完整性我用于测试的Makefile、所有配置选项、环境变量(请参见 Boot ldflags)都不起作用,包括--enable-rpath

mkdir ~/gcc一起使用将下面的Makefile复制到~/gcc,然后复制到cd ~/gcc && make build-gcc

注意所使用的选项仅针对本测试用例,不作为参考使用。

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
dhxwm5r4

dhxwm5r42#

我正在安装httpd-2.4.51,需要指定-rpath来编译程序。我使用链接器标志-Wl,-rpath -Wl,LIBDIR
使用命令:

sudo mv httpd.c /usr/lib #create a .c file and move it to the default directory for the library file
sudo gcc -o httpd httpd.c -L$--prefix=/usr/lib -lhttpd -Wl,-rpath=$--prefix=/usr/lib

您可以检查gcc的安装位置:

whereis gcc

gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz

相关问题