How to install GCC piece by piece with GMP, MPFR, MPC, ELF, without shared libraries?
Asked Answered
M

7

129

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)
Madalena answered 26/2, 2012 at 3:53 Comment(0)
M
145

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)

Madalena answered 26/2, 2012 at 3:53 Comment(17)
FYI be sure to update the above version numbers for the new GCC 4.6.3 (or whatever's current when you're doing this)Madalena
The ppl libraies are also required to build gcc: bugseng.com/products/ppl/Download. You are missing that step also.Nitrile
You could run ./contrib/download_prerequisites from the gcc source directory. This comment is thanks to N7P on Reddit.Madalena
@Paul, The PPL libs are not required to build GCC, only if you want to use the optional Graphite optimisationsSuppurative
Why build gcc with --disable-shared? That seems like a very bad idea.Suppurative
@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.Madalena
No, 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.Suppurative
--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 poorSuppurative
Thanks 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?Madalena
That doesn't really sound like building everything from scratch. Don't these instructions require some other working C compiler on the system?Quickwitted
@Quickwitted You're right, they do. What would you suggest as better wording for the headline and also the question?Madalena
Should 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.Viveca
@Viveca 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.Madalena
You can use Linuxbrew to install gcc (and optionally glibc) in your home directory with brew install gcc. See linuxbrew.shShindig
I get "stdio.h" not found after compilation, any suggestions? Trying to build this the static way to move it to an incomplete embedded systemHandfasting
Well, I found out that I could find those libs in the "sysroot" download from my distributor of the gcc source code. But, now it says that it can't find "as" the gcc assembler, and my distributor hasn't distributed thatHandfasting
2 things... the receipt for ELF library didn't work. The link is broken... I tried without it by installing 'libelf-dev' package. The main problem was gcc compilation. In order to build gcc you need a C/C++ compiler (gcc). My stock-installed gcc refuses to build gcc sources. Would be nice if the guide mention this.Brumbaugh
S
134

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
Suppurative answered 19/5, 2012 at 4:28 Comment(21)
Thanks, I clarified the question that it does want disable-shared, and to add your link for the typical instructions.Madalena
Configure still complains after running download_prerequisites that they are not installed. Is there a missing step here?Templet
No, there is no missing stepSuppurative
@JonathanWakely Did you run make check, since I got an error about undefined reference to __gmp_get_memory_functions.Jaffna
@LiDong, I run make check ten times or more every day without problems.Suppurative
@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?Flavine
@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.Suppurative
@JonathanWakely: Got it, thanks. Probably would be sensible if that explanation was part of the documentation :)Flavine
@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.Flavine
@Flavine 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.Suppurative
Why bother installing the prerequisite libs instead of just building them in-tree, which is much simpler and equivalent?Suppurative
This 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?Bannock
@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_pathsSuppurative
When I tried something like this (albeit with gcc 4.9.3) I got error messages about missing those prerequisite libraries.Quickwitted
@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.Suppurative
For 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/…Resident
This worked for me with gcc 6.2.0 and should definitely be the accepted answer! (It works across operating systems/versions, etc.)Spancake
@shuhalo in the bin subdirectory of the --prefix of course.Suppurative
I want to install gcc 4.3.4, but there is no download_prerequisites file.Haematoid
@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.4Suppurative
Does this even build a static gcc like the question asks? It's confusing bc Wakely is commenting on everyone else's answers, but I think the question was changed after he wrote this answerHandfasting
C
27

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
Coup answered 26/2, 2014 at 17:2 Comment(2)
This 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.Suppurative
This 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.0Dauphin
J
7

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

Jessee answered 16/11, 2012 at 20:2 Comment(0)
L
6

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.

Ligament answered 24/4, 2018 at 20:46 Comment(0)
D
1

I followed the top accepted answer by joelparkerhenderson. This is the best answer that I could find for installing on a *NIX system over the internet.

For posterity I want to add that if you get an error like "install error: cannot compute suffix of object files: cannot compile"

then update your LD_LIBRBARY_PATH with /tmp/gcc/lib (See joelparkerhenderson's full answer for context)

/tmp/gcc/lib contains all the required mpfr/mpc/gmp so files and the run time linker cannt find them unless you add them to LD_LIBRARY_PATH. Also dont forget to export LD_LIBRARY_PATH ;). More context here: https://gcc.gnu.org/wiki/FAQ#configure_suffix

Destitution answered 18/8, 2015 at 20:26 Comment(1)
Having to update your LD_LIBRARY_PATH just to use the compiler is dumb. If you use https://mcmap.net/q/15358/-how-to-install-gcc-piece-by-piece-with-gmp-mpfr-mpc-elf-without-shared-libraries instead that isn't necessary.Suppurative
I
0

For GCC-13.2.0 latest release.

For latest RPM based (Red-hat, Fedora, Rocky-Linux or CentOS)

sudo dnf install libgmp-dev libmpfr-dev libmpc-devel packages.

tar xzf gcc-13.2.0.tar.gz
cd gcc-13.2.0
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
$PWD/../gcc-13.2.0/configure --prefix=$HOME/GCC-13.2.0 --enable-languages=all
make
make install

Inhume answered 21/10, 2023 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.