GCC | Ubuntu下编译、安装gcc

目录

1.系统环境

2.下载、编译、安装

2-1.安装GCC

2-2.编译、安装GMP

2-3.安装、编译mpfr

2-3.安装、编译MPC

2-4.重新安装、编译GCC

参考、拓展:


1.系统环境

[fly@fly-vm gcc]$ uname -a
Linux fly-vm 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

2.下载、编译、安装

2-1.安装GCC

[fly@fly-vm gcc]$ wget ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/releases/gcc-11.1.0/gcc-11.1.0.tar.gz
[fly@fly-vm gcc]$ ls
gcc-11.1.0.tar.gz
[fly@fly-vm gcc]$ tar -zxvf gcc-11.1.0.tar.gz
[fly@fly-vm gcc]$ du -sh gcc-11.1.0.tar.gz
133M    gcc-11.1.0.tar.gz
[fly@fly-vm gcc]$ du -sh gcc-11.1.0
932M    gcc-11.1.0
[fly@fly-vm gcc]$ cd gcc-11.1.0/
[fly@fly-vm gcc-11.1.0]$ ls
ABOUT-NLS           COPYING3.LIB     libbacktrace   libquadmath         Makefile.def
ar-lib              COPYING.LIB      libcc1         libsanitizer        Makefile.in
ChangeLog           COPYING.RUNTIME  libcody        libssp              Makefile.tpl
ChangeLog.jit       c++tools         libcpp         libstdc++-v3        MD5SUMS
ChangeLog.tree-ssa  depcomp          libdecnumber   libtool-ldflags     missing
compile             fixincludes      libffi         libtool.m4          mkdep
config              gcc              libgcc         libvtv              mkinstalldirs
config.guess        gnattools        libgfortran    ltgcc.m4            move-if-change
config-ml.in        gotools          libgo          ltmain.sh           multilib.am
config.rpath        include          libgomp        lt~obsolete.m4      NEWS
config.sub          INSTALL          libhsail-rt    lto-plugin          README
configure           install-sh       libiberty      ltoptions.m4        symlink-tree
configure.ac        intl             libitm         ltsugar.m4          test-driver
contrib             LAST_UPDATED     libobjc        ltversion.m4        ylwrap
COPYING             libada           liboffloadmic  MAINTAINERS         zlib
COPYING3            libatomic        libphobos      maintainer-scripts
[fly@fly-vm gcc-11.1.0]$ ./configure --prefix=$PWD/_install

报错:

configure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations.  Source code for these libraries can be found at
their respective hosting sites as well as at
https://gcc.gnu.org/pub/gcc/infrastructure/.  See also
http://gcc.gnu.org/install/prerequisites.html for additional info.  If
you obtained GMP, MPFR and/or MPC 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.

2-2.编译、安装GMP

[fly@fly-vm gcc]$ wget http://ftp.gnu.org/gnu/gmp/gmp-6.2.1.tar.bz2
[fly@fly-vm gcc]$ tar -xjf gmp-6.2.1.tar.bz2
[fly@fly-vm gcc]$ cd gmp-6.2.1/
[fly@fly-vm gmp-6.2.1]$ ls
acinclude.m4  configfsf.guess   COPYINGv2      gen-fib.c          INSTALL           mini-gmp       mpq           scanf
aclocal.m4    configfsf.sub     COPYINGv3      gen-jacobitab.c    INSTALL.autoconf  missing        mp_set_fns.c  tal-debug.c
asl.h         config.guess      cxx            gen-psqr.c         install-sh        mp_bpl.c       mpz           tal-notreent.c
assert.c      config.in         demos          gen-trialdivtab.c  invalid.c         mp_clz_tab.c   NEWS          tal-reent.c
AUTHORS       config.sub        doc            gmp-h.in           longlong.h        mp_dv_tab.c    nextprime.c   test-driver
bootstrap.c   configure         errno.c        gmp-impl.h         ltmain.sh         mpf            primesieve.c  tests
ChangeLog     configure.ac      extract-dbl.c  gmp.pc.in          Makefile.am       mp_get_fns.c   printf        tune
compat.c      COPYING           gen-bases.c    gmpxx.h            Makefile.in       mp_minv_tab.c  rand          version.c
compile       COPYING.LESSERv3  gen-fac.c      gmpxx.pc.in        memory.c          mpn            README        ylwrap
[fly@fly-vm gmp-6.2.1]$ ./configure --prefix=$PWD/_install --enable-cxx --enable-mpbsd
configure: WARNING: unrecognized options: --enable-mpbsd
configure: summary of build options:

  Version:           GNU MP 6.2.1
  Host type:         sandybridge-pc-linux-gnu
  ABI:               64
  Install prefix:    /home/fly/workSpace/gcc/gmp-6.2.1/_install
  Compiler:          gcc
  Static libraries:  yes
  Shared libraries:  yes
[fly@fly-vm gmp-6.2.1]$ make -j4
[fly@fly-vm gmp-6.2.1]$ sudo make install
[fly@fly-vm gmp-6.2.1]$ cd _install/
[fly@fly-vm _install]$ ls
include  lib  share
[fly@fly-vm _install]$ tree
.
├── include
│   ├── gmp.h
│   └── gmpxx.h
├── lib
│   ├── libgmp.a
│   ├── libgmp.la
│   ├── libgmp.so -> libgmp.so.10.4.1
│   ├── libgmp.so.10 -> libgmp.so.10.4.1
│   ├── libgmp.so.10.4.1
│   ├── libgmpxx.a
│   ├── libgmpxx.la
│   ├── libgmpxx.so -> libgmpxx.so.4.6.1
│   ├── libgmpxx.so.4 -> libgmpxx.so.4.6.1
│   ├── libgmpxx.so.4.6.1
│   └── pkgconfig
│       ├── gmp.pc
│       └── gmpxx.pc
└── share
    └── info
        ├── dir
        ├── gmp.info
        ├── gmp.info-1
        └── gmp.info-2

5 directories, 18 files

为GMP库文件、头文件创建软链接:

.PHONY: all links unlinks

LK                              = sudo ln -s
RM                              = sudo rm -rf

SRC_DIR                 = $(shell pwd)
DST_DIR                 = /usr
INC_FILE0               = $(SRC_DIR)/include/gmp.h
INC_FILE1               = $(SRC_DIR)/include/gmpxx.h
LIB_FILE0               = $(SRC_DIR)/lib/libgmp.a
LIB_FILE1               = $(SRC_DIR)/lib/libgmp.la
LIB_FILE2               = $(SRC_DIR)/lib/libgmp.so.10.4.1
LIB_FILE3               = $(SRC_DIR)/lib/libgmp.so.10.4.1

INC_FILE                = $(shell ls include)
LIB_FILE                = $(shell ls lib | grep lib*)
PKG_FILE                = $(shell ls lib/pkgconfig)
SHARE_F                 = $(shell ls share/info)

INC_FILES               = $(addprefix $(SRC_DIR)/include/, $(INC_FILE))
LIB_FILES               = $(addprefix $(SRC_DIR)/lib/, $(LIB_FILE))
PKG_FILES               = $(addprefix $(SRC_DIR)/lib/pkgconfig/, $(PKG_FILE))
SHARE_FS                = $(addprefix $(SRC_DIR)/share/info/, $(SHARE_F))

DST_INC_FS              = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(INC_FILES))
DST_LIB_FS              = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(LIB_FILES))
DST_PKG_FS              = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(PKG_FILES))
DST_SHARE_FS    = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(SHARE_FS))

##├── include
##│   ├── gmp.h
##│   └── gmpxx.h
##├── lib
##│   ├── libgmp.a
##│   ├── libgmp.la
##│   ├── libgmp.so -> libgmp.so.10.4.1
##│   ├── libgmp.so.10 -> libgmp.so.10.4.1
##│   ├── libgmp.so.10.4.1
##│   ├── libgmpxx.a
##│   ├── libgmpxx.la
##│   ├── libgmpxx.so -> libgmpxx.so.4.6.1
##│   ├── libgmpxx.so.4 -> libgmpxx.so.4.6.1
##│   ├── libgmpxx.so.4.6.1
##│   └── pkgconfig
##│       ├── gmp.pc
##│       └── gmpxx.pc
##└── share
##    └── info
##        ├── dir
##        ├── gmp.info
##        ├── gmp.info-1
##        └── gmp.info-2
##

all:links

links:$(DST_INC_FS) $(DST_LIB_FS) $(DST_PKG_FS) $(DST_SHARE_FS)

$(DST_INC_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

$(DST_LIB_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

$(DST_PKG_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

$(DST_SHARE_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

unlinks:
        @echo "make unlinks for include && lib files"
        $(RM) $(DST_INC_FS)
        $(RM) $(DST_LIB_FS)
        $(RM) $(DST_PKG_FS)
        $(RM) $(DST_SHARE_FS)

2-3.安装、编译mpfr

[fly@fly-vm gcc]$ wget https://www.mpfr.org/mpfr-current/mpfr-4.1.0.tar.bz2
[fly@fly-vm gcc]$ tar -jxvf mpfr-4.1.0.tar.bz2
[fly@fly-vm gcc]$ cd mpfr-4.1.0/
[fly@fly-vm mpfr-4.1.0]$ ./configure --prefix=$PWD/_install
[fly@fly-vm mpfr-4.1.0]$ make -j12
[fly@fly-vm mpfr-4.1.0]$ make check
[fly@fly-vm mpfr-4.1.0]$ make install
[fly@fly-vm mpfr-4.1.0]$ cd _install/
[fly@fly-vm _install]$ ls
include  lib  share
[fly@fly-vm _install]$ tree
.
├── include
│   ├── mpf2mpfr.h
│   └── mpfr.h
├── lib
│   ├── libmpfr.a
│   ├── libmpfr.la
│   ├── libmpfr.so -> libmpfr.so.6.1.0
│   ├── libmpfr.so.6 -> libmpfr.so.6.1.0
│   ├── libmpfr.so.6.1.0
│   └── pkgconfig
│       └── mpfr.pc
└── share
    ├── doc
    │   └── mpfr
    │       ├── AUTHORS
    │       ├── BUGS
    │       ├── COPYING
    │       ├── COPYING.LESSER
    │       ├── examples
    │       │   ├── can_round.c
    │       │   ├── divworst.c
    │       │   ├── ReadMe
    │       │   ├── rndo-add.c
    │       │   ├── sample.c
    │       │   ├── threads.c
    │       │   └── version.c
    │       ├── FAQ.html
    │       ├── NEWS
    │       └── TODO
    └── info
        ├── dir
        └── mpfr.info

8 directories, 24 files

为MPFR的库文件、头文件创建链接

.PHONY: all links unlinks

LK                  = sudo ln -s
RM                  = sudo rm -rf

SRC_DIR             = $(shell pwd)
DST_DIR             = /usr

INC_FILE            = $(shell ls include)
LIB_FILE            = $(shell ls lib | grep lib*)
PKG_FILE            = $(shell ls lib/pkgconfig)
SHARE_F             = $(shell ls share/info)

INC_FILES           = $(addprefix $(SRC_DIR)/include/, $(INC_FILE))
LIB_FILES           = $(addprefix $(SRC_DIR)/lib/, $(LIB_FILE))
PKG_FILES           = $(addprefix $(SRC_DIR)/lib/pkgconfig/, $(PKG_FILE))
SHARE_FS            = $(addprefix $(SRC_DIR)/share/info/, $(SHARE_F))

DST_INC_FS          = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(INC_FILES))
DST_LIB_FS          = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(LIB_FILES))
DST_PKG_FS          = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(PKG_FILES))
DST_SHARE_FS            = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(SHARE_FS))


all:links

links:$(DST_INC_FS) $(DST_LIB_FS) $(DST_PKG_FS) $(DST_SHARE_FS)

$(DST_INC_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

$(DST_LIB_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

$(DST_PKG_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

$(DST_SHARE_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

unlinks:
        @echo "make unlinks for include && lib files"
        $(RM) $(DST_INC_FS)
        $(RM) $(DST_LIB_FS)
        $(RM) $(DST_PKG_FS)
        $(RM) $(DST_SHARE_FS)

2-3.安装、编译MPC

[fly@fly-vm gcc]$ wget https://ftp.gnu.org/gnu/mpc/mpc-1.2.1.tar.gz
[fly@fly-vm gcc]$ tar -zxvf mpc-1.2.1.tar.gz
[fly@fly-vm gcc]$ cd mpc-1.2.1/
[fly@fly-vm mpc-1.2.1]$ ./configure --prefix=$PWD/_install
[fly@fly-vm mpc-1.2.1]$ make -j12
[fly@fly-vm mpc-1.2.1]$ make install
[fly@fly-vm mpc-1.2.1]$ cd _install/
[fly@fly-vm _install]$ tree
.
├── include
│   └── mpc.h
├── lib
│   ├── libmpc.a
│   ├── libmpc.la
│   ├── libmpc.so -> libmpc.so.3.2.1
│   ├── libmpc.so.3 -> libmpc.so.3.2.1
│   └── libmpc.so.3.2.1
└── share
    └── info
        ├── dir
        └── mpc.info

4 directories, 8 files

为MPC库文件、头文件创建软链接:

.PHONY: all links unlinks

LK                  = sudo ln -s
RM                  = sudo rm -rf

SRC_DIR             = $(shell pwd)
DST_DIR             = /usr

INC_FILE            = $(shell ls include)
LIB_FILE            = $(shell ls lib | grep libmpc*)
#PKG_FILE           = $(shell ls lib/pkgconfig)
SHARE_F             = $(shell ls share/info/ | grep mpc.*)

INC_FILES           = $(addprefix $(SRC_DIR)/include/, $(INC_FILE))
LIB_FILES           = $(addprefix $(SRC_DIR)/lib/, $(LIB_FILE))
#PKG_FILES          = $(addprefix $(SRC_DIR)/lib/pkgconfig/, $(PKG_FILE))
SHARE_FS            = $(addprefix $(SRC_DIR)/share/info/, $(SHARE_F))

DST_INC_FS          = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(INC_FILES))
DST_LIB_FS          = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(LIB_FILES))
#DST_PKG_FS         = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(PKG_FILES))
DST_SHARE_FS        = $(patsubst $(SRC_DIR)/%, $(DST_DIR)/%, $(SHARE_FS))


all:links

links:$(DST_INC_FS) $(DST_LIB_FS)  $(DST_SHARE_FS)

$(DST_INC_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

$(DST_LIB_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

$(DST_SHARE_FS):$(DST_DIR)/%:$(SRC_DIR)/%
        $(LK) $^ $@

unlinks:
        @echo "make unlinks for include && lib files"
        $(RM) $(DST_INC_FS)
        $(RM) $(DST_LIB_FS)
        $(RM) $(DST_SHARE_FS)

2-4.重新安装、编译GCC

[fly@fly-vm gcc-11.1.0]$ ./configure --prefix=$PWD/_install
[fly@fly-vm gcc-11.1.0]$ make -j12

报错:

Makefile:2461: recipe for target 's-automata' failed
make[3]: *** [s-automata] Error 137
make[3]: *** Waiting for unfinished jobs....

重新编译报错:

[fly@fly-vm gcc-11.1.0]$ ./configure --prefix=$PWD/_install --enable-languages=c,c++
[fly@fly-vm gcc-11.1.0]$ make -j8
g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See  for instructions.
Makefile:1142: recipe for target 'generic-match.o' failed
make[3]: *** [generic-match.o] Error 4
make[3]: *** Waiting for unfinished jobs....
/bin/sh ../.././gcc/../move-if-change tmp-attrtab.c    insn-attrtab.c
/bin/sh ../.././gcc/../move-if-change tmp-dfatab.c     insn-dfatab.c
/bin/sh ../.././gcc/../move-if-change tmp-latencytab.c insn-latencytab.c
echo timestamp > s-attrtab
/bin/sh ../.././gcc/../move-if-change tmp-automata.c insn-automata.c
echo timestamp > s-automata
make[3]: Leaving directory '/home/fly/workSpace/gcc/gcc-11.1.0/host-x86_64-pc-linux-gnu/gcc'
Makefile:4779: recipe for target 'all-stage1-gcc' failed
make[2]: *** [all-stage1-gcc] Error 2
make[2]: Leaving directory '/home/fly/workSpace/gcc/gcc-11.1.0'
Makefile:24816: recipe for target 'stage1-bubble' failed
make[1]: *** [stage1-bubble] Error 2
make[1]: Leaving directory '/home/fly/workSpace/gcc/gcc-11.1.0'
Makefile:1000: recipe for target 'all' failed
make: *** [all] Error 2

https://blog.csdn.net/weixin_41803041/article/details/79496016

collect2: fatal error: ld terminated with signal 9 [Killed]
compilation terminated.
../.././gcc/cp/Make-lang.in:135: recipe for target 'cc1plus' failed
make[3]: *** [cc1plus] Error 1
make[3]: *** Waiting for unfinished jobs....
^Cmake[3]: *** Deleting file 'cc1'
make[3]: *** Deleting file 'lto-dump'
make[3]: *** Deleting file 'lto1'
../.././gcc/lto/Make-lang.in:94: recipe for target 'lto1' failed
make[3]: *** [lto1] Interrupt
../.././gcc/lto/Make-lang.in:100: recipe for target 'lto-dump' failed
make[3]: *** [lto-dump] Interrupt
../.././gcc/c/Make-lang.in:86: recipe for target 'cc1' failed
make[3]: *** [cc1] Interrupt
Makefile:4779: recipe for target 'all-stage1-gcc' failed
make[2]: *** [all-stage1-gcc] Interrupt
Makefile:24816: recipe for target 'stage1-bubble' failed
make[1]: *** [stage1-bubble] Interrupt
^CMakefile:1000: recipe for target 'all' failed
make: *** [all] Interrupt

解决办法,重新设置虚拟机内存大小,重启后重新编译后OK。

GCC | Ubuntu下编译、安装gcc_第1张图片 图1.重新设置虚拟机内存大小

编译命令执行如下:

 2003  cd gcc-11.1.0/
 2005  make clean
 2006  ./configure --prefix=$PWD/_install --enable-languages=c,c++
 2007  make -j8
 2010  make install
 2011  cd _install/
 2013  cd bin/
 2015  ./gcc --version

可以查看gcc新版本,编译OK。

[fly@fly-vm gcc-11.1.0]$ cd _install/
[fly@fly-vm _install]$ ls
bin  include  lib  lib32  lib64  libexec  share
[fly@fly-vm _install]$ cd bin/
[fly@fly-vm bin]$ ls
c++  gcc     gcc-ranlib  gcov-tool                x86_64-pc-linux-gnu-g++         x86_64-pc-linux-gnu-gcc-ar
cpp  gcc-ar  gcov        lto-dump                 x86_64-pc-linux-gnu-gcc         x86_64-pc-linux-gnu-gcc-nm
g++  gcc-nm  gcov-dump   x86_64-pc-linux-gnu-c++  x86_64-pc-linux-gnu-gcc-11.1.0  x86_64-pc-linux-gnu-gcc-ranlib
[fly@fly-vm bin]$ ./gcc --version
gcc (GCC) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 

参考、拓展:

1.GCC,GNU 编译器集合

2.GMP下载

3.GMP编译、安装

4.MPFR官网

5.GNU MPFR 4.1.0文档

6.MPC官网

你可能感兴趣的:(3.Linux系统使用,1.C/C++编程,12.开发笔记,gcc编译,安装)