How to install GCC piece by piece with GMP, MPFR, MPC, ELF, without shared libraries?
Solution 1
The Easy Way
If you're a typical developer, you can install the easy way, using instructions at http://gcc.gnu.org/wiki/InstallingGCC or your system package manager such as:
apt install gcc # for Debian, Ubuntu, etc.
yum install gcc # for RedHat, CentOS, etc.
brew install gcc # for Mac OS X
The Hard Way
GCC says the answer here is "the hard way" because it builds everything piece by piece, and does not use shared libraries.
GCC infrastructure
Get the GCC infrastructure:
ftp://gcc.gnu.org/pub/gcc/infrastructure/
Put downloads in a temp directory (you can use whatever directory you want).
/opt/downloads
Build the infrastructure in a temp directory that is different than the downloads directory or its subdirectories:
/tmp/gcc
Configure the infrastructure using static libraries like this:
./configure --disable-shared --enable-static --prefix=/tmp/gcc
The --disable-shared flag may be worth a closer look depending on your needs. I use --disable-shared because I am building static code only, and I want to make sure that I don't build any shared code. My need is to move the resulting GCC around my drives easily, so I want all static code, and I do not want any shared code. If you prefer shared code, omit the --disable-shared flag.
Versions
When you run any of the commands in this answer, be sure to update the commands to use the current GCC version number that matches your needs. The commands in this answer are for GCC 4.6.2.
Note that the GCC documentation says: "While any sufficiently new version of required tools usually work, library requirements are generally stricter. Newer versions may work in some cases, but it's safer to use the exact versions documented."
GMP
GMP is the GNU Multiple Precision Arithmetic Library.
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2
bunzip2 gmp-4.3.2.tar.bz2
tar xvf gmp-4.3.2.tar
cd gmp-4.3.2
./configure --disable-shared --enable-static --prefix=/tmp/gcc
make && make check && make install
MPFR
MPFR is the GNU Multiple-precision floating-point rounding library. It depends on GMP.
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2
bunzip2 mpfr-2.4.2.tar.bz2
tar xvf mpfr-2.4.2.tar
cd mpfr-2.4.2
./configure --disable-shared --enable-static --prefix=/tmp/gcc --with-gmp=/tmp/gcc
make && make check && make install
MPC
MPC is the GNU Multiple-precision C library. It depends on GMP and MPFR.
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz
tar zxvf mpc-0.8.1.tar.gz
cd mpc-0.8.1
./configure --disable-shared --enable-static --prefix=/tmp/gcc --with-gmp=/tmp/gcc --with-mpfr=/tmp/gcc
make && make check && make install
ELF
ELF stands for Executable and Linkable Format. This library provides architecture-independent size and endian support.
wget http://www.mr511.de/software/libelf-0.8.13.tar.gz
tar zxvf libelf-0.8.13.tar.gz
cd libelf-0.8.13
./configure --disable-shared --enable-static --prefix=/tmp/gcc
make && make check && make install
GCC
GCC is the GNU Compiler Collection. It depends on GMP, MPFR, MPC, and ELF.
wget http://www.netgull.com/gcc/releases/gcc-4.6.2/gcc-4.6.2.tar.gz
tar zxvf gcc-4.6.2.tar.gz
Build gcc in a scratch directory on the same mount point. (Building it within /tmp would trigger cross compile host issues)
mkdir -p /opt/downloads/gcc-4.6.2-scratch
cd /opt/downloads/gcc-4.6.2-scratch
The configure command and its flags must be all on one command line (this post shows them on separate lines just because of web page width).
Note: We use the full path to configure and do not set library path environment variables. We choose to disable bootstrap and disable shared libraries because we don't want them (typical users may want both), and to use posix threads and to flags that are defaults because we want them (typical users may want to use other threads or skip default flags). YMMV and read about the flags here
/opt/downloads/gcc-4.6.2/configure
--disable-shared
--disable-bootstrap
--disable-libstdcxx-pch
--enable-languages=all
--enable-libgomp
--enable-lto
--enable-threads=posix
--enable-tls
--with-gmp=/tmp/gcc
--with-mpfr=/tmp/gcc
--with-mpc=/tmp/gcc
--with-libelf=/tmp/gcc
--with-fpmath=sse
make && make install
This page is great for GCC installation info, how to build it, various flags, and more:
http://www.acsu.buffalo.edu/~charngda/cc_build.html
Updates
The ppl libraries can be used for memory optimizations: see bugseng.com/products/ppl/Download (Thanks Paul in comments)
You could run ./contrib/download_prerequisites from the gcc source directory. (Thanks to N7P on reddit)
Solution 2
The accepted answer makes it far more complicated than necessary and is not correct for all versions. Building GCC with --disable-shared
is usually a very bad idea. See http://gcc.gnu.org/wiki/InstallingGCC for an easier approach.
The entire process should be no more difficult than this (replacing 4.6.2 with the version you want to build):
tar xzf gcc-4.6.2.tar.gz
cd gcc-4.6.2
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
$PWD/../gcc-4.6.2/configure --prefix=$HOME/GCC-4.6.2
make
make install
(But please read the link above anyway, it contains useful information.)
Apparently some people on Ubuntu have tons of crap set in their environment which interferes with the GCC build process, and they need to remove that first:
unset LIBRARY_PATH CPATH C_INCLUDE_PATH PKG_CONFIG_PATH CPLUS_INCLUDE_PATH INCLUDE LD_LIBRARY_PATH
Solution 3
I work on a cluster. Only the master node is connected to the Internet. The software on nodes is outdated and generally not maintained. I don't have root access. I have two options:
- build static software I need (computational packages); or
- build static compilers.
I chose the second and built gcc, g++ and gfortran.
I built everything in
PREFIX=$HOME/cmp/soft/sft
and for make
I used
THREADS=8
Below, gcc is built with
- GMP
- MPFR
- MPC
- ISL
- CLOOG
One can get the latest gcc from here: ftp://gcc.gnu.org/pub/gcc/releases
Dependencies are found here: ftp://gcc.gnu.org/pub/gcc/infrastructure
Dependencies
I get dependencies with the following script:
#!/bin/sh
# ===========
## variables:
GMP=gmp-4.3.2.tar.bz2
MPFR=mpfr-2.4.2.tar.bz2
MPC=mpc-0.8.1.tar.gz
ISL=isl-0.12.2.tar.bz2
CLOOG=cloog-0.18.1.tar.gz
MIRROR=ftp://gcc.gnu.org/pub/gcc/infrastructure
# ===========
## functions:
extract() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "I don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
# ======================
## download and extract:
wget $MIRROR/$GMP
extract $GMP
wget $MIRROR/$MPFR
extract $MPFR
wget $MIRROR/$MPC
extract $MPC
wget $MIRROR/$ISL
extract $ISL
wget $MIRROR/$CLOOG
extract $CLOOG
The following bash function is used below:
mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
Each of the commands below is to be issued in the dir of the lib just downloaded.
GMP
mkdircd build
../configure --disable-shared --enable-static --prefix=$PREFIX/gmp
make -j $THREADS && make check && make install
MPFR
mkdircd build
../configure --with-gmp=$PREFIX/gmp --disable-shared --enable-static --prefix=$PREFIX/mpfr
make -j $THREADS && make install
MPC
mkdircd build
../configure --with-gmp=$PREFIX/gmp --with-mpfr=$PREFIX/mpfr --disable-shared --enable-static --prefix=$PREFIX/mpc
make -j $THREADS && make install
ISL
mkdircd build
../configure --with-gmp-prefix=$PREFIX/gmp --disable-shared --enable-static --prefix=$PREFIX/isl
make -j $THREADS && make install
CLOOG
mkdircd build
../configure --with-gmp-prefix=$PREFIX/gmp --with-isl-prefix=$PREFIX/isl --disable-shared --enable-static --prefix=$PREFIX/cloog
make -j $THREADS && make install
gcc
mkdircd build
export LD_LIBRARY_PATH=$PREFIX/gmp/lib:$PREFIX/mpfr/lib:$PREFIX/mpc/lib:$PREFIX/isl/lib:$PREFIX/cloog/lib
export C_INCLUDE_PATH=$PREFIX/gmp/include:$PREFIX/mpfr/include:$PREFIX/mpc/include:$PREFIX/isl/include:$PREFIX/cloog/include
export CPLUS_INCLUDE_PATH=$PREFIX/gmp/include:$PREFIX/mpfr/include:$PREFIX/mpc/include:$PREFIX/isl/include:$PREFIX/cloog/include
../configure --with-gmp=$PREFIX/gmp --with-mpfr=$PREFIX/mpfr --with-mpc=$PREFIX/mpc --with-isl=$PREFIX/isl --with-cloog=$PREFIX/cloog --disable-shared --enable-static --disable-multilib --prefix=$PREFIX/gcc --enable-languages=c,c++,fortran
make -j $THREADS bootstrap && make install
Solution 4
Used what Jonathan above has mentioned except that I had to download gmp and mpfr manually and create soft link (gcc 4.4.2 distribution probably does not have "download_prerequisites")
cd src/gcc-4.4.2
ln -s ~/linux64/gmp-4.2.1 gmp
ln -s ~/linux64/mpfr-2.3.0 mpfr
Btw, using "-with-gmp" and "with-mpfr" with "./configure" gave me "configure: error: cannot compute suffix of object files: cannot compile". Hence I downloaded the srcs of gmp and mpfr and then created the soft link to it from within gcc src top leve dir
Solution 5
After trying to install GCC dependencies including GMP, MPFR and MPC. I ran into additional install requirements, Errors and missing files; such as gmp.h header file required by MPFR to install. There are quit a number of issues you will run into in the process. However, There is an Easier way to Build and Install GCC-X.0.0 or later version with an automatic linking.
OPTION ONE.
To save the trouble of Building with make, make install and Linking the dynamic libraries, Simply:
-
Download your GCC-X.0.0 -version (with latest version from: https://gcc.gnu.org/)
-
Extract the gcc-X-000.tar.gz files to a location /somepath/.
-
Once you have Extracted .tar.gz file, run ./contrib/download_prerequisites script which is located on /somepath/ or the source directory.
-
This script will download support libraries including: GMP, MPFR and MPC and will create a Symlinks for you, that will BUILD all gcc dependencies automatically as part of gcc Installation process.
No need to Build and Link support libraries that were downloaded to /somepath/ by issuing Make, Make Install or running ./configure file or adding links such as --with-gmp=/gmp_path/.../....., --with-mpfr=/mpfr_path/.../... because this was done when you ran script that created symlinks.
OPTION TWO.
- Use your OS Package management system to install the support libraries in standard system location. For Debian based system including Ubuntu, Install libgmp-dev, libmpfr-dev and libmpc-dev packages. For RPM based system including Fedora and SUSE, install gmp-devel, and libmpc-devel(mpc-devel on SUSE) packages.
- This option will install libraries and header files in a standard system directory where they will be found automically when building GCC.
- This is the advantage of OS Package installation Management when you invoke "sudo apt-get install libgmp-dev" or "sudo apt-get install libmpfr-dev" install and linking is done for you. In addition, you don`t have to build the support libraries with Make, Make install or ./configure. Plus, the process takes a few minutes to complete the all process.
- Now you can proceed on with GCC Installation.
CONFIGURATION:
This is the GCC config Process, with gcc-X-000.tar.gz
Issue:
tar -xvf gcc-X-000.tar.gz
cd gcc-X-000
./contrib/download_prerequisites
cd ..
mkdir objdir
/../gcc-X-000/configure --prefix=$HOME/gcc-X-000 --enable-languages=c,c++,fortran,go --disable-multilib
make -j 2
make install
NB:
--enable-languages such as c++ or c.
--disable-multilib; disable multilib based on your system and OS, you will be prompted about multilib to proceed.
Make will take long time to complete. However, you can issue the option -j #no_pro. This will run Make concurrently in parallel based on the number of processors on your PC or Mac.
For detailed information on how to execute this process you can visit: https://gcc.gnu.org/wiki/InstallingGCC.
joelparkerhenderson
Joel Parker Henderson: software consultant for Agile, Ruby, SQL, NoSQL, iOS, Android, and LAMP stack. JoelParkerHenderson.com SixArm.com Software Solutions GitHub/joelparkerhenderson Facebook/joelparkerhenderson NumCommand.com
Updated on April 25, 2020Comments
-
joelparkerhenderson about 4 years
How do I install GCC (the GNU Compiler Collection) piece by piece, using the current version,
using the correct versions of dependencies, not using a package manager (like yum, rpm, apt, dpkg), and not using shared libraries?Typical developers will likely want to install GCC the typical way, use your package manager (yum, rpm, apt, dpkg, port, brew, etc.) or follow the instructions here (http://gcc.gnu.org/wiki/InstallingGCC).
My question is how to install GCC piece by piece, without shared libraries.
- I want a fully-independent GCC that can be moved around my system without using any shared libraries, and without creating any shared libraries.
- This is what GCC calls "the difficult way" and it is not recommended for typical users.
GCC depends on:
- GMP: GNU Multiple Precision Arithmetic Library
- MPFR: GNU Multiple-precision floating-point rounding library
- MPC: GNU Multiple-precision C library
- ELF: Executable and Linkable Format library
- PPL: Parma Polyhedra Library (optional, for memory optimizations)
-
joelparkerhenderson over 12 yearsFYI be sure to update the above version numbers for the new GCC 4.6.3 (or whatever's current when you're doing this)
-
Paul Fultz II over 12 yearsThe ppl libraies are also required to build gcc: bugseng.com/products/ppl/Download. You are missing that step also.
-
joelparkerhenderson over 12 yearsYou could run ./contrib/download_prerequisites from the gcc source directory. This comment is thanks to N7P on Reddit.
-
Jonathan Wakely about 12 years@Paul, The PPL libs are not required to build GCC, only if you want to use the optional Graphite optimisations
-
Jonathan Wakely about 12 yearsWhy build gcc with
--disable-shared
? That seems like a very bad idea. -
joelparkerhenderson about 12 yearsThanks, I clarified the question that it does want disable-shared, and to add your link for the typical instructions.
-
joelparkerhenderson about 12 years@Johnathan Wakely: using --disable-shared is fine if you know what you're doing. The bootstrapping setup for GCC depends on it. In my case, I'm doing it because I want non-shared libraries.
-
Jonathan Wakely about 12 yearsNo, the bootstrapping does not depend on it. It makes sense to use --disable-shared for building GMP, MPFR and MPC but not for GCC itself (and using the download_prerequisites script means you don't need to build GMP, MPFR and MPC manually anyway). Building GCC always builds static libs and shared libs by default, --disable-shared just means you get no shared libs, which is the wrong choice for most people.
-
Jonathan Wakely about 12 years
--disable-shared
is wrong for most people,--disable-bootstrap
might speed up the build, but might cause it to fail depending on the existing gcc version,--enable-libgomp
enabled by default anyway,--enable-lto
enabled by default,--enable-thread-safe
not a valid option,--enable-threads=posix
enabled by default on some platforms, not valid on others,--enable-tls
enabled by default, ... basically these instructions are pretty poor -
joelparkerhenderson about 12 yearsThanks Jonathan, they're my best-guess at what worked for me to build a fully-movable GCC without any shared libaries. Can you suggest what specific flags you would use?
-
jocull over 10 yearsConfigure still complains after running
download_prerequisites
that they are not installed. Is there a missing step here? -
Jonathan Wakely over 10 yearsNo, there is no missing step
-
Li Dong about 10 years@JonathanWakely Did you run
make check
, since I got an error aboutundefined reference to __gmp_get_memory_functions
. -
Jonathan Wakely about 10 years@LiDong, I run
make check
ten times or more every day without problems. -
0xC0000022L about 9 years@JonathanWakely: is there a particular reason one should be using those rather old packages from the
infrastructure
subfolder on the FTP server? I'm wondering, because if you ask the GCC folks they'll also regularly point you to a newer GCC, pointing out that it fixes so many issues. What's so different with the dependencies? -
Jonathan Wakely about 9 years@0xC0000022L, those versions are the most well tested and are known to work. Newer versions fix some bugs and add features, but the old versions do everything correctly that GCC uses. If you build them in-tree then it's irrelevant what new features they have, because you're not using those libraries directly, so all that matters is that the set of features GCC actually uses work correctly.
-
0xC0000022L about 9 years@JonathanWakely: Got it, thanks. Probably would be sensible if that explanation was part of the documentation :)
-
0xC0000022L about 9 years@JonathanWakely: I see you have edit rights over at the Wiki page you linked and are the last to have it edited. Would you please be so kind to mention somewhere the facts that I explained over here as they seem to be directly caused by the fact that an old GMP version is the default. Thanks. I tried to register on the Wiki, but apparently one also needs to be unlocked to contribute. Easier this way.
-
Jonathan Wakely about 9 years@0xC0000022L It's already part of the documentation, at gcc.gnu.org/install/prerequisites.html: "While any sufficiently new version of required tools usually work, library requirements are generally stricter. Newer versions may work in some cases, but it's safer to use the exact versions documented. We appreciate bug reports about problems with newer versions, though." As for the other issue, disabling libstdc++ is a fairly specialist option that is out of place in the "dumbest instructions that can possibly work". It would be more appropriate to fix the LfS docs.
-
Jonathan Wakely over 8 yearsWhy bother installing the prerequisite libs instead of just building them in-tree, which is much simpler and equivalent?
-
user2023370 over 8 yearsThis is pretty much how I've always done it; I'd then add
/opt/gcc-4.6.2/bin
to the PATH. I'm curious though - if I also want to use the libraries and headers which come with my new GCC version, should I also update my LD_LIBRARY_PATH, INCLUDE_PATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH... Any others? Or does GCC automatically find all this? -
Jonathan Wakely over 8 years@user2023370, GCC knows how to find all its own headers and libraries, of course. You need to set LD_LIBRARY_PATH to tell the dynamic linker how to find the libraries (because when you run an executable GCC is no longer involved), see gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.how_to_set_paths
-
Jonathan Wakely over 8 yearsThis is unnecessarily complicated, the steps in my answer also build a statically-linked GCC, without root access, and you don't need to set
LD_LIBRARY_PATH
to use the resulting GCC. -
Jonathan Wakely over 8 yearsHaving to update your LD_LIBRARY_PATH just to use the compiler is dumb. If you use stackoverflow.com/a/10662297/981959 instead that isn't necessary.
-
einpoklum over 8 yearsWhen I tried something like this (albeit with gcc 4.9.3) I got error messages about missing those prerequisite libraries.
-
einpoklum over 8 yearsThat doesn't really sound like building everything from scratch. Don't these instructions require some other working C compiler on the system?
-
Jonathan Wakely over 8 years@einpoklum, then your "something like this" was not the same. The steps above work. They work for me regularly, with all version of GCC. They works for hundreds of people.
-
joelparkerhenderson over 8 years@einpoklum You're right, they do. What would you suggest as better wording for the headline and also the question?
-
kevinarpe about 8 yearsShould this answer be updated for newer versions of GCC? Example: GCC v6.1 now requires librarly ISL by default. Even Linux from Scratch has not updated instructions yet to include this library.
-
joelparkerhenderson about 8 years@kevinarpe Yes please, good idea. Do you have edit capabilities? If so, please feel free. Or if you prefer to send me info and/or post it here, I can update the answer.
-
Shaun Jackman almost 8 yearsYou can use Linuxbrew to install gcc (and optionally glibc) in your home directory with
brew install gcc
. See linuxbrew.sh -
Cantfindname over 7 yearsFor future reference, this worked for me for gcc-5.4.0. The only thing not mentioned was that I had to
unset LIBRARY_PATH CPATH C_INCLUDE_PATH PKG_CONFIG_PATH CPLUS_INCLUDE_PATH INCLUDE LD_LIBRARY_PATH
before everything else as suggested here: stackoverflow.com/questions/12255058/… -
David Doria over 7 yearsThis worked for me with gcc 6.2.0 and should definitely be the accepted answer! (It works across operating systems/versions, etc.)
-
Michael Goldshteyn about 7 yearsThis also works in situations where there are conflicts with whatever is installed in "well defined" paths (i.e., cases where the standard procedure outright fails to work). Latest versions as of 2017-06-19: gmp-6.1.2 mpfr-3.1.5 mpc-1.0.3 isl-0.16.1 cloog-0.18.4 gcc-7.1.0
-
Jonathan Wakely almost 7 years@shuhalo in the
bin
subdirectory of the--prefix
of course. -
Dinesh Gowda about 5 yearsI want to install gcc 4.3.4, but there is no download_prerequisites file.
-
Jonathan Wakely about 5 years@DineshGowda the script isn't magic, it just downloads some files, unpacks them, and creates symlinks. You can do the same by hand with the right prerequisites for 4.3.4