Download Dependencies
tar zxvf gcc-releases-gcc-14.2.0.tar.gz
cd gcc-releases-gcc-14.2.0
contrib/download_prerequisites
Older versions of GCC require manual installation of dependencies, while the new version does not; a script automates the process. Note: <span>contrib/download_prerequisites</span> must be executed in the root directory of the installation (<span>gcc-releases-gcc-14.2.0/</span>), and there will be no output on the screen when this command is executed. If the download is too slow, you can download the following files from the internet and move them to <span>gcc-releases-gcc-14.2.0/</span>, then execute <span>contrib/download_prerequisites</span> again, which will skip the download and proceed to extract and create symbolic links:
gettext-0.22.tar.gz
gmp-6.2.1.tar.bz2
isl-0.24.tar.bz2
mpc-1.2.1.tar.gz
mpfr-4.1.0.tar.bz2
After execution, the directory will contain the following 10 files:
drwxr-xr-x 9 xxx xxx 4096 Jul 10 2020 mpfr-4.1.0
drwxr-xr-x 8 xxx xxx 319 Oct 21 2020 mpc-1.2.1
drwxrwxr-x 15 xxx xxx 4096 Nov 15 2020 gmp-6.2.1
drwxrwxr-x 11 test test 12288 May 2 2021 isl-0.24
drwxrwxr-x 10 root root 4096 Jun 17 2023 gettext-0.22
lrwxrwxrwx 1 root root 15 Oct 14 20:08 gettext -> ./gettext-0.22/
lrwxrwxrwx 1 root root 12 Oct 14 20:08 gmp -> ./gmp-6.2.1/
lrwxrwxrwx 1 root root 13 Oct 14 20:08 mpfr -> ./mpfr-4.1.0/
lrwxrwxrwx 1 root root 12 Oct 14 20:08 mpc -> ./mpc-1.2.1/
lrwxrwxrwx 1 root root 11 Oct 14 20:08 isl -> ./isl-0.24/
Compilation
Then start the compilation:
mkdir build && cd build
../configure --prefix=/opt/gcc-14.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++,fortran
make -j
Compilation error:
In file included from ../../../gettext/gettext-runtime/gnulib-lib/scratch_buffer.h:115:0,
from ../../../gettext/gettext-runtime/gnulib-lib/malloc/scratch_buffer_grow.c:23:
./malloc/scratch_buffer.gl.h:70:9: error: unknown type name ‘max_align_t’
union { max_align_t __align; char __c[1024]; } __space;
^
checking whether gcc -std=gnu99 supports -Wstrict-prototypes... make[7]: *** [Makefile:2156: malloc/libgrt_a-scratch_buffer_grow.o] Error 1
make[7]: *** Waiting for unfinished jobs....
In file included from ../../../gettext/gettext-runtime/gnulib-lib/scratch_buffer.h:115:0,
from ../../../gettext/gettext-runtime/gnulib-lib/malloc/scratch_buffer_set_array_size.c:23:
./malloc/scratch_buffer.gl.h:70:9: error: unknown type name ‘max_align_t’
union { max_align_t __align; char __c[1024]; } __space;
^
In file included from ../../../gettext/gettext-runtime/gnulib-lib/scratch_buffer.h:115:0,
from ../../../gettext/gettext-runtime/gnulib-lib/malloc/scratch_buffer_grow_preserve.c:23:
./malloc/scratch_buffer.gl.h:70:9: error: unknown type name ‘max_align_t’
union { max_align_t __align; char __c[1024]; } __space;
According to AI suggestions, <span>max_align_t</span> is a type defined in the C11 standard, while my system is CentOS 7, with the default GCC version being 4.8.5, which does not support the C11 standard. In short, the upgrade step is too large; it is necessary to pre-install an intermediate version of GCC and then use that intermediate version to compile GCC 14.
Fortunately, I had previously installed GCC 10, located at <span>/usr/local/gcc10</span>. For other systems that do not have an intermediate version installed, you will need to install one yourself.
Now rebuild:
# Completely clean the build environment
make distclean
rm -rf *
# Simplify environment variables (to avoid pollution)
unset C_INCLUDE_PATH
unset CPLUS_INCLUDE_PATH
unset LIBRARY_PATH
unset CPATH
# Keep only necessary variables
export PATH=/usr/local/gcc10/bin:$PATH
export CC=/usr/local/gcc10/gcc
export CXX=/usr/local/gcc10/g++
export CFLAGS="-O2"
export CXXFLAGS="-O2"
# Build and compile
../configure --prefix=/opt/gcc-14.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++,fortran
make -j
make install
# Check if the installation was successful
[root@mu02 build]# /opt/gcc-14.2.0/bin/gcc -v
Using built-in specs.
COLLECT_GCC=/opt/gcc-14.2.0/bin/gcc
COLLECT_LTO_WRAPPER=/opt/gcc-14.2.0/libexec/gcc/x86_64-pc-linux-gnu/14.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/opt/gcc-14.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++,fortran
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.0 (GCC)
This time it was finally successful.
Running
Try compiling a C++ program with GCC 14 (just write one yourself):
/opt/gcc-14.2.0/bin/g++ -o test test.cpp
./test
Error:
./test: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.32' not found (required by ./test)
It can be seen that there is a problem with <span>/lib64/libstdc++.so.6</span>. Let’s check <span>/lib64/libstdc++.so.6</span>:
[root@mu02 gcc-releases-gcc-14.2.0]# ll /usr/lib64/libstdc++.so.6
lrwxrwxrwx 1 root root 30 Oct 14 23:40 /usr/lib64/libstdc++.so.6 -> /usr/lib64/libstdc++.so.6.0.26
[root@mu02 gcc-releases-gcc-14.2.0]# strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
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
GLIBCXX_3.4.20
GLIBCXX_3.4.21
GLIBCXX_3.4.22
GLIBCXX_3.4.23
GLIBCXX_3.4.24
GLIBCXX_3.4.25
GLIBCXX_3.4.26
It can be seen that <span>/usr/lib64/libstdc++.so.6</span> links to <span>/usr/lib64/libstdc++.so.6.0.26</span>, which contains the highest GLIBCXX version of GLIBCXX_3.4.26. However, GCC 14 requires version 3.4.32. We need to upgrade <span>libstdc++.so.6</span> to <span>/lib64/libstdc++.so.6.0.32</span> or a higher version. Users can download it from the internet.
mv libstdc++.so.6.0.32 /usr/lib64/
rm /usr/lib64/libstdc++.so.6
ln -s /usr/lib64/libstdc++.so.6.0.33 /usr/lib64/libstdc++.so.6
Note: Deleting <span>/usr/lib64/libstdc++.so.6</span> is safe for the system. System commands depend on the C library, not the C++ library. Do not delete <span>/usr/lib64/libc.so.6</span>, as it will crash the system; I just tried it a few days ago and had to enter rescue mode to fix it.
Recompile <span>test.cpp</span> and run it; this time there were no errors.
Adding Environment Variables
Now add GCC 14 to the environment variables. Create <span>/opt/gcc-14.2.0/gcc-14.2.0.env.sh</span> and write the following content:
# /opt/gcc-14.2.0/gcc-14.2.0.env.sh
GCCHOME=/opt/gcc-14.2.0
PATH=$GCCHOME/bin:$PATH
LD_LIBRARY_PATH=$GCCHOME/lib:$GCCHOME/lib64:$LD_LIBRARY_PATH
LIBRARY_PATH=$GCCHOME/lib
C_INCLUDE_PATH=$GCCHOME/include/
CPLUS_INCLUDE_PATH=$GCCHOME/include/
export GCCHOME PATH LD_LIBRARY_PATH LIBRARY_PATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH
source /opt/gcc-14.2.0/gcc-14.2.0.env.sh
gcc -v
g++ -v