Sometimes we need our own version of gcc (often the latest versions) to use the new features in C++.
This means we need to compile the source code on the machine, which can be difficult.
General Steps
- Download the source code.
- Perform a out-of-source build.
- Installing dependencies:
mpc,mpfr,gmp,isl - Configure with
configure. - Compile & Link with
make
Dependency
The relationship between all packages are
Therefore, we can install the packages in the following order,
gmp -> isl -> mpfr -> mpc -> gcc
Building
GNU Autotools
The building process or all preceding libraries are handled by the GNU Autotools, and we typically interact with the building system via the following command:
./configure
make
sudo make install
Afterwards, the package will be installed to the default directory /usr/local. This is the reason why we need to use sudo privilege in the final step. Because we need to copy the building results into places like /usr/local/bin and /usr/local/lib, which require sudo privilege.
If we want to install the library/binary into a custom place, we need to specify the --prefix option. My personal reference is to install all custom libraries into $HOME/.local, with command
./configure --prefix=$HOME/.local
make
make install
Since the packages are installed into user–owned directories, we can do make install directly without sudo privilege. This is extremely helpful if we were working on a cluster and we want customised environment1.
I highly recommend to read the help information for the configuration by,
./configure --help
I always find ways to solve my building problems by reading it carefully.
gmp
./configure --prefix=$HOME/.local
make
make install
isl
./configure --prefix=$HOME/.local --with-gmp-prefix=$HOME/.local
make
make install
mpfr
./configure --prefix=$HOME/.local --with-gmp=$HOME/.local
make
make install
mpc
./configure --prefix=$HOME/.local --with-gmp=$HOME/.local --with-mpfr=$HOME/.local
make
make install
gcc
./configure \
--prefix=$HOME/.local \
--with-gmp=$HOME/.local \
--with-mpc=$HOME/.local \
--with-mpfr=$HOME/.local \
--with-isl=$HOME/.local \
--enable-languages=c,c++,fortran \
--disable-multilib
make
make install
Notes
- We need to unset C/C++ related flags with command
unset.
-
The administrator may get pissed off and refuse to debug my code. But I find it worthwhile. ↩