目录
前言
我的系统信息如下:
CentOS Linux release 7.6.1810 (AltArch) [running via Linux Deploy]
gcc版本最新是4.8.5
1 2 3 4 5 6 7 8 |
[root@localhost benz]# gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/armv7hl-redhat-linux-gnueabi/4.8.5/lto-wrapper Target: armv7hl-redhat-linux-gnueabi Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-armv7hl-redhat-linux-gnueabi/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-armv7hl-redhat-linux-gnueabi/cloog-install --enable-gnu-indirect-function --disable-sjlj-exceptions --with-cpu=cortex-a8 --with-tune=cortex-a8 --with-arch=armv7-a --with-float=hard --with-fpu=vfpv3-d16 --with-abi=aapcs-linux --build=armv7hl-redhat-linux-gnueabi Thread model: posix gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) |
由于安装nodejs-12.13.1的Linux 二进制文件 (ARM)报错:
/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.20′ not found
在确保gcc已经是最新版本了,那么再确认下目前/usr/lib目录下的libstdc++.so.6中包含动态库版本有哪些:strings /usr/lib/libstdc++.so.6 | grep GLIBC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@localhost bin]# strings /usr/lib/libstdc++.so.6 | grep GLIBC GLIBCXX_3.4 GLIBCXX_3.4.1 GLIBCXX_3.4.2 GLIBCXX_3.4.3 GLIBCXX_3.4.4 GLIBCXX_3.4.5 GLIBCXX_3.4.6 GLIBCXX_3.4.7 GLIBCXX_3.4.8 GLIBCXX_3.4.9 GLIBCXX_3.4.10 GLIBCXX_3.4.11 GLIBCXX_3.4.12 GLIBCXX_3.4.13 GLIBCXX_3.4.14 GLIBCXX_3.4.15 GLIBCXX_3.4.16 GLIBCXX_3.4.17 GLIBCXX_3.4.18 GLIBCXX_3.4.19 GLIBC_2.4 GLIBCXX_DEBUG_MESSAGE_LENGTH |
发现确实没有3.4.20版本,3.4.20版本需要升级gcc的版本。
升级
1 2 3 4 5 |
[root@vmqs88zm ~]# yum install centos-release-scl [root@vmqs88zm ~]# yum install devtoolset-7-gcc* [root@vmqs88zm ~]# scl enable devtoolset-7 bash [root@vmqs88zm ~]# which gcc [root@vmqs88zm ~]# gcc --version |
这种方法同样适用于安装gcc6,只需要将上面命令中7改成6即可。详情请参考: https://www.softwarecollections.org/en/scls/rhscl/devtoolset-7/
预先安装
1 2 |
yum -y install bzip2 yum -y install flex |
1、下载源码包
到http://ftp.gnu.org/gnu/gcc/ 选择需要编译的gcc版本,这里我选择编译gcc的5.4.0版本:
下载源码包
1 2 |
wget http://ftp.gnu.org/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.gz #下载源码包 tar -zxvf gcc-5.4.0.tar.gz #解压 |
2、进入解压后的目录,安装gcc依赖库
1 2 |
[root@localhost benz]# cd gcc-5.4.0 [root@localhost gcc-5.4.0]# ./contrib/download_prerequisites |
执行命令后它会自动下载mpfr、gmp、mpc、 isl这4个库。
如果执行报错:tar (child): lbzip2: no exec: no file or directory,确保安装了bzip2
3、在新目录中配置、编译、安装
1 2 3 4 5 |
[root@localhost gcc-5.4.0]# mkdir build [root@localhost gcc-5.4.0]# cd build/ [root@localhost build]# ../configure CFLAGS='-mcpu=cortex-a8 -mtune=cortex-a8 -mfloat-abi=hard -mfpu=vfpv3-d16 -march=armv7-a' --prefix=/usr/local/gcc --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,lto --enable-plugin --enable-initfini-array --disable-libgcj --enable-gnu-indirect-function --disable-sjlj-exceptions --with-arch=armv7-a --with-float=hard --with-fpu=vfpv3-d16 --with-abi=aapcs-linux [root@localhost build]# make [root@localhost build]# make install |
编译很漫长。。。大约需要2个多小时。
最主要的是配置参数,可以参考gcc -v
的配置参数,除了--prefix
以外,其它指定目录的参数,没有什么要求,都不指定,使用默认的;
还有一个参数就是
CFLAGS
,像这样的以--with-
的参数:--with-arch=armv7-a --with-float=hard --with-fpu=vfpv3-d16
都转换为-m
开头,并写在CFLAGS
参数里
卸载旧的gcc
(1)查看安装的gcc版本
1 2 |
[root@localhost benz]# rpm -q gcc gcc-4.8.5-39.armv7hl |
(2)执行卸载命令 rpm -e
1 2 3 |
[root@localhost benz]# rpm -e gcc-4.8.5-39.armv7hl error: Failed dependencies: gcc = gcc-4.8.5-39.armv7hl is needed by (installed) gcc-c++-4.8.5-39.armv7hl |
提示gcc-4.8.5-39.armv7hl被1个软件依赖,卸载失败,则先卸载被依赖的软件包.
1 |
[root@localhost benz]# rpm -e gcc-c++-4.8.5-39.armv7hl |
(4)再执行rpm -e gcc-4.8.5-39.armv7hl
进行卸载。
卸载成功
(5) 验证,输入gcc -v
1 2 |
[root@localhost benz]# gcc -v bash: gcc: command not found |
提示command not found,说明的确卸载成功了。
链接新的gcc和g++并更新更新libstdc++
删除旧的gcc和g++
1 2 |
rm -f /usr/bin/gcc rm -f /usr/bin/g++ |
链接新的gcc和g++
1 2 |
ln -s /usr/local/gcc/bin/gcc /usr/bin/gcc ln -s /usr/local/gcc/bin/g++ /usr/bin/g++ |
更新libstdc++
1 2 3 4 5 6 |
#通过 ls -lrt /usr/lib64/libstdc++.so.6 可以看到不是链接我们最新的libstdc++.so.6.0.26 #删除 rm -f /usr/lib64/libstdc++.so.6 #更新 ln -s /usr/local/lib64/libstdc++.so.6.0.26 /usr/lib64/libstdc++.so.6 |
可以使用gcc -v
输出新版本gcc信息,教程到此结束。
一些遇到的问题
默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件,通常通过源码包进行安装时,如果不指定–prefix,会将库安装在/usr/local/lib目录下;当运行程序需要链接动态库时,提示找不到相关的.so库,会报错。也就是说,/usr/local/lib目录不在系统默认的库搜索目录中,需要将目录加进去。
1、首先打开/etc/ld.so.conf文件
2、加入动态库文件所在的目录:执行vi /etc/ld.so.conf,在”include ld.so.conf.d/*.conf”下方增加”/usr/local/lib”。
3、保存后,在命令行终端执行:/sbin/ldconfig;其作用是将文件/etc/ld.so.conf列出的路径下的库文件缓存到/etc/ld.so.cache以供使用,因此当安装完一些库文件,或者修改/etc/ld.so.conf增加了库的新搜索路径,需要运行一下ldconfig,使所有的库文件都被缓存到文件/etc/ld.so.cache中,如果没做,可能会找不到刚安装的库。/sbin/ldconfig -v 查看已经缓存的库。
经过以上三个步骤,”error while loading shared libraries”的问题通常情况下就可以解决了。
Linux中error while loading shared libraries错误解决办法
为什么使用 CFLAGS
1 2 3 4 5 6 7 |
/usr/bin/ld: error: perf_test uses VFP register arguments, perf_test.o does not /usr/bin/ld: failed to merge target specific data of file perf_test.o /usr/bin/ld: error: perf_test uses VFP register arguments, util.o does not /usr/bin/ld: failed to merge target specific data of file util.o /usr/bin/ld: error: perf_test uses VFP register arguments, ../baseline/lib.a(a.o) does not collect2: ld returned 1 exit status make: *** [perf_test] Error 1 |
当编译过程出现类似以上的错误,是因为目标三元组指示编译器已为硬浮动ABI配置。这意味着libgcc库也将是hardfp。错误消息表明,至少部分系统正在使用软浮动ABI。硬浮点运算与软浮点运算是不兼容的,所以会报以上的错。解决办法就是在编译参数时配置整个系统为硬浮点:-mfloat-abi=hard
,而-mfloat-abi=hard
是需要写到CFLAGS
里的。
ARM compilation error, VFP registered used by executable, not object file
如何使用-mfloat-abi=hard
How do I use the –with-cpu and –with-arch when cross compiling stuff like gcc binutils and glibc
其中的一个答案
I instead used the
./configure CFLAGS='-mcpu=cortex-a7 -mfpu=neon-vfpv4'
and the binaries are no longer compiled for armv5te. Thanks Everyone