如何使用内置 rpath 构建和安装 gcc? [英] How to build and install gcc with built-in rpath?
问题描述
我正在尝试在/usr/local 中构建和安装我自己的 gcc 4.7.2 以代替/usr 中的 gcc 4.4.6.(这是在 CentOS 6.3 上.)
I'm trying to build and install my own gcc 4.7.2 in /usr/local to use in place of the gcc 4.4.6 in /usr. (This is on CentOS 6.3.)
gcc 使可执行文件和动态库动态链接到它自己的动态库,例如libstdc++.so.如何构建和安装 gcc,以便生成的二进制文件自动获取链接器 -rpath 选项(-rpath/usr/local/lib64),该选项导致链接/usr/local/lib64 中的动态库而不是/usr/中的动态库lib64 还是/lib64?
gcc makes executables and dynamic libraries that dynamically link to its own dynamic libraries, e.g. libstdc++.so. How do I build and install gcc so that the generated binaries automatically get a linker -rpath option (-rpath /usr/local/lib64) that causes dynamic libraries in /usr/local/lib64 to be linked instead of those in /usr/lib64 or /lib64?
如果它工作正常,在我使用 gcc 构建可执行文件而不指定-Wl,-rpath=/usr/local/lib64"后,当我 ldd 可执行文件时,它应该显示/usr/local/lib64/libstdc++.so.6 而不是/usr/lib64/libstdc++.so.6.对于 libgcc_s.so.1 也是如此.
If it works properly, after I build an executable with the gcc without specifying "-Wl,-rpath=/usr/local/lib64", when I ldd the executable, it should show /usr/local/lib64/libstdc++.so.6 instead of /usr/lib64/libstdc++.so.6. Similarly for the libgcc_s.so.1.
我尝试了不同的方法,包括在配置"命令行上指定 LDFLAGS_FOR_TARGET=-Wl,-rpath=/usr/local/lib64,-rpath=/usr/local/lib,但没有任何效果.
I have tried different approaches, including specifying LDFLAGS_FOR_TARGET=-Wl,-rpath=/usr/local/lib64,-rpath=/usr/local/lib on the 'configure' command-line, but nothing worked.
推荐答案
如果您不想导出路径,还有另一种解决方案:
If you don't want to export paths there's an alternative solution:
在 PATH
中使用您的工具链:
with your toolchain in the PATH
:
gcc -dumpspecs > specsfile
编辑 specsfile
并在 link
部分添加您的 -rpath
示例:
edit specsfile
and in the section link
add your -rpath
example:
*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 test if it work with:
g++ -specs=specsfile test.cpp
readelf -d a.out |grep RPATH
如果它有效,您可以将其永久化(无需每次都通过 -specs
)
if it work you can make it permanent (no need to pass -specs
everytime)
strace -fF -o /tmp/g++.log g++ test.cpp
grep specs /tmp/g++.log
grep 应该显示 gcc
查找默认 specs
文件的路径.
the grep should show the paths where gcc
look for the default specs
file.
specs 文件足够灵活,可以根据具体情况进行条件链接变量,例如:
The specs files are flexible enough to allow conditional linking depending on variables, 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}}
应该根据mabi使用不同的和多个路径(尚未测试),%R
应该是sysroot
路径,可以更改为所需的完整路径.
should use different and multiple paths depending on mabi (untested, yet), %R
should be the sysroot
path, can be changed with needed full path.
还有一个 --with-specs=
选项 gcc configure 最终将在构建时使用,我还不清楚如何与 .link
部分一起使用(正在处理它)
There's also a --with-specs=
option gcc configure eventually to be used at build time, not clear to me yet how to use with the .link
section (working on it)
--with-specs="%{shared:-Wl,-rpath -Wl,$(DESTDIR)/lib}%{!shared:-Wl,-rpath -Wl,$(DESTDIR)/lib}"
它工作,我使用 shared
而不是 !shared
只是为了测试,可能应该使用一些更智能的条件,注意它没有与 -dumpspecs
.
It work, I used both shared
and not !shared
just for test, probably some smarter condition should be used, note that it isn't reported with -dumpspecs
.
阅读 gcc 邮件列表的一些线程,我的印象是 specs
并不是每个人都喜欢(但如果我没记错的话 4.9 添加另一个选项 --with-extra-specs
) 而不是进行此类自定义的首选方法似乎是 configure.host
,但我已经完成并且没有研究它,玩得开心!:-)
Reading through some thread of the gcc mailing list I had the impression specs
aren't liked by everyone (but if I'm not wrong 4.9 add another option --with-extra-specs
) instead preferred way to do such customizations appears to be configure.host
, but I'm done and not looking into it, have fun! :-)
以上更新
不知道能不能设置一个预定义的rpath
,可能会在binutils
ld中> 不在 gcc/g++
中,但你为什么要这样做?
I don't know if you can set a pre-defined rpath
, probably if you can would be in the linker ld
of binutils
not in gcc/g++
, but why would you do that?
只需在运行时导出 LD_LIBRARY_PATH
并在构建时导出 LD_RUN_PATH
Just export LD_LIBRARY_PATH
at runtime and LD_RUN_PATH
at build time
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH
ldd a.out
ldd
应该会显示您导出的路径.
ldd
should show the paths you exported.
引用使用 libtool 构建共享库时给出的消息:
To quote a message given when a shared library is built with libtool:
如果您碰巧想要链接给定目录 LIBDIR 中已安装的库,则必须使用 libtool 并指定库的完整路径名,或者在链接期间使用 `-LLIBDIR' 标志并至少执行以下之一:
If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following:
- 在执行期间将 LIBDIR 添加到 `LD_LIBRARY_PATH' 环境变量中
- 在链接过程中将 LIBDIR 添加到 `LD_RUN_PATH' 环境变量中
- 使用 `-Wl,--rpath -Wl,LIBDIR' 链接器标志
- 让您的系统管理员将 LIBDIR 添加到 `/etc/ld.so.conf'
查看有关共享库的任何操作系统文档更多信息,例如 ld(1) 和 ld.so(8) 手册页.
See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages.
为了完整性Makefile
我用来测试的东西,所有的配置选项,环境变量(见引导 ldflags)我试过没用,包括 --enable-rpath
.
for completeness
the Makefile
I used for testing the thing, all the configure options, environment variables (see boot ldflags) I tried didn't work, --enable-rpath
included.
使用 mkdir ~/gcc
将下面的 Makefile
复制到 ~/gcc
然后 cd ~/gcc &&make build-gcc
use with mkdir ~/gcc
copy the Makefile
below into ~/gcc
then cd ~/gcc && make build-gcc
注意所用选项仅用于本测试用例,请勿作为参考.
notice the options used are only for this test case, don't use as 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
这篇关于如何使用内置 rpath 构建和安装 gcc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!