记录在Ubuntu18.04编译gcc-4.4.7与glibc-2.20遇到的坑

记录在Ubuntu18.04编译gcc-4.4.7与glibc-2.20遇到的坑

因为工作缘故,代码需要在本地写,但是由于服务器环境与本地环境差异比较大,在本地编译的二进制文件无法直接在服务器运行。看了许多解决方案,选择了其中一种比较可行的,具体请看这里[1]

为了实验一下,得在本地搭建服务器上的老环境nmd,为什么,本地环境为Ubuntu18.04lts,服务器为Centos,具体版本不清楚,只得知gcc版本为4.4.7,glibc版本为2.20。apt源gcc最老的版本为4.8,因此打算一并编译4.4.7的gcc,废话不多说,开干。

1、gcc安装

1、源码获取

从gnu的中国镜像中选择了中科大源。https://mirrors.ustc.edu.cn/gnu/ ,包括gcc所有gnu项目的源码都可以在这里找到,所以后面glibc的源码也是在这里下载的。

2、安装指导

从源码中的INSTALL文件夹中可以找到一些安装所需信息,有时间的同学可以阅读一下。

3、编译前

在源码目录下新建目录build作为编译目录,进入目录运行配置指令,其中--prefix选项指定编译完成后的安装目录,注意尽量不要选择系统默认的gcc所在目录进行安装,以免出现不可预料的后果。

cd gcc-4.4.7
mkdir build
cd build
../configure --prefix=/usr/local/bin/gcc-4.4.7

运行结果如下

checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln works... yes
checking whether ln -s works... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for gnatbind... no
checking for gnatmake... no
checking whether compiler driver understands Ada... no
checking how to compare bootstrapped objects... cmp --ignore-initial=16 $$f1 $$f2
checking for correct version of gmp.h... no
configure: error: Building GCC requires GMP 4.1+ and MPFR 2.3.2+.
Try the --with-gmp and/or --with-mpfr options to specify their locations.
Copies of these libraries' source code can be found at their respective
hosting sites as well as at ftp://gcc.gnu.org/pub/gcc/infrastructure/.
See also http://gcc.gnu.org/install/prerequisites.html for additional info.
If you obtained GMP and/or MPFR from a vendor distribution package, make
sure that you have installed both the libraries and the header files.
They may be located in separate packages.

可以看到报错是由于环境缺少了GMP 4.1+MPFR 2.3.2+,在ubuntu18.04中可以通过apt安装libgmp-devlibmpfr-dev解决报错。

4、正式编译

  • texinfo错误
make[1]: Entering directory '/home/fo/gcc-4.4.7/objdir/gcc'
if [ xinfo = xinfo ]; then \
        makeinfo --split-size=5000000 --no-split -I . -I ../../gcc/doc \
                -I ../../gcc/doc/include -o doc/cpp.info ../../gcc/doc/cpp.texi; \
fi
../../gcc/doc/cppopts.texi:772: @itemx must follow @item
Makefile:4270: recipe for target 'doc/cpp.info' failed
make[1]: *** [doc/cpp.info] Error 1
make[1]: Leaving directory '/home/ADMIN/cort ex_m4/gcc-4.4.7/objdir/gcc'
Makefile:5256: recipe for target 'all-gcc' failed
make: *** [all-gcc] Error 2

原因:环境中texinfo版本过高,源码中的.texi代码在高版本的texinfo语法为错误,无法通过编译。

解决办法:编译过程中暂时卸载texinfo,编译后再重新安装[2]

sudo apt remove texinfo
  • struct siginfo类型错误

待施工

原因:从某个版本信号量类型从struct siginfo变更为siginfo_t

解决办法:将源码中的struct siginfo替换为siginfo_t

  • struct ucontext类型错误
/dev/shm/GCCcore/6.3.0/dummy-/gcc-6.3.0/stage1_obj/./gcc/xgcc -B/dev/shm/GCCcore/6.3.0/dummy-/gcc-6.3.0/stage1_obj/./gcc/ -B/dev/shm/GCCcore/6.3.0/dummy-/GCC_stage1_eb/x86_64-pc-linux-gnu/bin/ -B/dev/shm/GCCcore/6.3.0/dummy-/GCC_stage1_eb/x86_64-pc-linux-gnu/lib/ -isystem /dev/shm/GCCcore/6.3.0/dummy-/GCC_stage1_eb/x86_64-pc-linux-gnu/include -isystem /dev/shm/GCCcore/6.3.0/dummy-/GCC_stage1_eb/x86_64-pc-linux-gnu/sys-include    -g -O2 -O2  -g -O2 -DIN_GCC    -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition  -isystem ./include   -fpic -mlong-double-80 -DUSE_ELF_SYMVER -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector   -fpic -mlong-double-80 -DUSE_ELF_SYMVER -I. -I. -I../.././gcc -I../../../libgcc -I../../../libgcc/. -I../../../libgcc/../gcc -I../../../libgcc/../include -I../../../libgcc/config/libbid -DENABLE_DECIMAL_BID_FORMAT -DHAVE_CC_TLS  -DUSE_TLS -o unwind-sjlj.o -MT unwind-sjlj.o -MD -MP -MF unwind-sjlj.dep -fexceptions -c ../../../libgcc/unwind-sjlj.c -fvisibility=hidden -DHIDE_EXPORTS
In file included from ../../../libgcc/unwind-dw2.c:401:0:
./md-unwind-support.h: In function ‘x86_64_fallback_frame_state’:
./md-unwind-support.h:65:47: error: dereferencing pointer to incomplete type ‘struct ucontext’
       sc = (struct sigcontext *) (void *) &uc_->uc_mcontext;
                                               ^~
make[2]: *** [../../../libgcc/shared-object.mk:14: unwind-dw2.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: Leaving directory '/dev/shm/GCCcore/6.3.0/dummy-/gcc-6.3.0/stage1_obj/x86_64-pc-linux-gnu/libgcc'
make[1]: *** [Makefile:14075: all-target-libgcc] Error 2
make[1]: Leaving directory '/dev/shm/GCCcore/6.3.0/dummy-/gcc-6.3.0/stage1_obj'
make: *** [Makefile:888: all] Error 2

原因:本地glibc版本过高,struct ucontext从glibc2.26起就不再存在在库中。

解决办法:使用高版本gcc,建议在6.5版本之后[3]

  • 待施工

2、glibc安装

待施工

猜你喜欢

转载自www.cnblogs.com/xsuns/p/13382643.html