Build GCC 11 from source on Ubuntu
Here, I want to build and install GCC 11.1 on my Ubuntu 21.04. This procedure is probably valid for future versions of GCC too.
Prerequisites
You need a C and C++ compiler found in $PATH to compile the new version. Try gcc and g++ in a terminal, if not there, install the system default ones
sudo apt install gcc sudo apt install g++
Method
Go to GCC releases on GitHub, download the latest version in the format of tar.gz . Here, I install GCC 11.1. I downloaded it in the home folder.
A folder with the same name as the tar.gz file is created.
Look out for any missing dependency, I needed to install bzip2 and flex on a fresh Ubuntu:
sudo apt install bzip2 sudo apt install flex
Create a build directory, just outside this directory:
Configure GCC for compilation:
../gcc-releases-gcc-11.1.0/configure -v --buildYou can change
- –prefix (installation path), to an address in your home folder if you are not an administrator.
Moreover, it is good to set
- –program-suffix (suffix to executables), as a version number so we can identify different GCC versions.
If everything goes well with configuration, you get a Makefile in ~/build/ directory, otherwise, read the errors and fix them and configure again.
make -j My laptop has 16 processing threads (8 logical cores), because of that, I put 16. For me, it took about 10 min to finish.
During the make process, you might get errors, read, google and fix them. Then run the make command again.
You finally see the below message:
Libraries have been installed in: /usr/local/gcc-11.1.0/lib/../lib64
Usage
There are several options to use the new GCC, the simplest is to add the lines below to ~/.bashrc :
Nowadays, most Fortran/C/C++ projects are build by CMake. To guide CMake to use the new compiler, I defined CC , CXX and FC environment variables.
Afterwards, open a new terminal and run
You should see the one that is newly installed.
Instead of editing ~/.bashrc , another option is to create a file like ~/load_gcc11.1.sh and paste the above export lines in it. Then whenever you need to load this GCC in a terminal, you run
In this way, you can have multiple versions, and load the one that suits your project.
Compilers
You can check all available GCC commands with:
I have below ones and some more:
c++-11.1 cpp-11.1 g++-11.1 gcc-11.1 gfortran-11.1
Delete garbage
If all is good, we don’t need the source and build folders anymore, delete them
rm ~/build -rf rm ~/gcc-releases-gcc-11.1.0 -rf
More details
The default installation of GCC on Ubuntu is accessible with commands without the version extension:
These commands are symlinks. To find out where they point to
For me it gives /usr/bin/gcc . We check its link
It gives /usr/bin/gcc -> gcc-10 . We can find the address of it:
It gives /usr/bin/gcc-10 for me. You can change the symlinks, for example, gcc , to point to gcc-11.1 , but personally I do not change the default settings of Ubuntu.
More
If you like to compile latest Clang from source, have a look at this post.
Moreover, I have a series of posts on how to straightforwardly program, build, and config a project with modern CMake.
Latest Posts
Makefile: create a build directory
I have a C++ project with the source files spanning many directories, and with interdependencies in the headers (the headers of one directory include headers from other directory source). I have a makefile looking like this :
CXX = g++ CFLAGS = -Wall -std=c++17 SRC = *.cpp ./num_utils/*.cpp ./shapes/*.cpp ./data_utils/*.cpp all: $(SRC) $(CXX) $(CFLAGS) $(SRC) -o main $(LDFLAGS)
I want to create a build directory where .o files get output, and modify the makefile so that I do not recompile every single file with every call to make I get confused reading the docs, please help or point me to a beginner-friendly tutorial
2 Answers 2
This makefile is probably only dedicated to gnu-make (because of the wildcard , patsubst , dir builtin commands and ifeq ) in a unix environment (because of mkdir -p , rm -rf ).
It recreates below the build directory the same directory hierarchy as the source files.
According to further questions in the comments, the ability to choose between a debug build ( opt=0 by default) and a release build ( opt=1 on the command line) has been added.
opt=0 CXX=g++ CFLAGS=-Wall -std=c++17 SRC=$ OBJ=$> ifeq ($,0) CFLAGS+=-g else CFLAGS+=-O3 endif all: $ $ $ $ -o main $ build/%.o : %.cpp mkdir -p $ $ -o $@ $ < -c $clean : rm -rf main build
$ make clean rm -rf main build $ tree . ├── data_utils │ ├── f3.cpp │ └── f33.cpp ├── main.cpp ├── makefile ├── num_utils │ ├── f1.cpp │ └── f11.cpp └── shapes ├── f2.cpp └── f22.cpp 3 directories, 8 files $ make mkdir -p build/ g++ -o build/main.o main.cpp -c -Wall -std=c++17 -g mkdir -p build/./num_utils/ g++ -o build/./num_utils/f1.o num_utils/f1.cpp -c -Wall -std=c++17 -g mkdir -p build/./num_utils/ g++ -o build/./num_utils/f11.o num_utils/f11.cpp -c -Wall -std=c++17 -g mkdir -p build/./shapes/ g++ -o build/./shapes/f2.o shapes/f2.cpp -c -Wall -std=c++17 -g mkdir -p build/./shapes/ g++ -o build/./shapes/f22.o shapes/f22.cpp -c -Wall -std=c++17 -g mkdir -p build/./data_utils/ g++ -o build/./data_utils/f3.o data_utils/f3.cpp -c -Wall -std=c++17 -g mkdir -p build/./data_utils/ g++ -o build/./data_utils/f33.o data_utils/f33.cpp -c -Wall -std=c++17 -g g++ -g build/main.o build/./num_utils/f1.o build/./num_utils/f11.o build/./shapes/f2.o build/./shapes/f22.o build/./data_utils/f3.o build/./data_utils/f33.o -o main $ make clean rm -rf main build $ make opt=1 mkdir -p build/ g++ -o build/main.o main.cpp -c -Wall -std=c++17 -O3 mkdir -p build/./num_utils/ g++ -o build/./num_utils/f1.o num_utils/f1.cpp -c -Wall -std=c++17 -O3 mkdir -p build/./num_utils/ g++ -o build/./num_utils/f11.o num_utils/f11.cpp -c -Wall -std=c++17 -O3 mkdir -p build/./shapes/ g++ -o build/./shapes/f2.o shapes/f2.cpp -c -Wall -std=c++17 -O3 mkdir -p build/./shapes/ g++ -o build/./shapes/f22.o shapes/f22.cpp -c -Wall -std=c++17 -O3 mkdir -p build/./data_utils/ g++ -o build/./data_utils/f3.o data_utils/f3.cpp -c -Wall -std=c++17 -O3 mkdir -p build/./data_utils/ g++ -o build/./data_utils/f33.o data_utils/f33.cpp -c -Wall -std=c++17 -O3 g++ -Wall -std=c++17 -O3 build/main.o build/./num_utils/f1.o build/./num_utils/f11.o build/./shapes/f2.o build/./shapes/f22.o build/./data_utils/f3.o build/./data_utils/f33.o -o main $ ./main in main() in f1() in f2() in f3() in f11() in f22() in f33() $ make --version GNU Make 4.3 Built for x86_64-pc-linux-gnu .