Preparing GCC Build: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
m (Replace deprecated enclose attributes)
 
(43 intermediate revisions by 24 users not shown)
Line 1: Line 1:
{{Rating|1}}

<noinclude>'''Note that this article is meant to be included into the [[GCC Cross-Compiler]] and [[Building GCC]] as they share this common section.'''</noinclude>
<noinclude>'''Note that this article is meant to be included into the [[GCC Cross-Compiler]] and [[Building GCC]] as they share this common section.'''</noinclude>


The GNU Compiler Collection is an advanced piece of software with dependencies. You need to install certain dependencies in order to build gcc. You need to install GNU make, GNU bison, flex, and of course an existing system compiler you wish to replace. In addition, you also ''need'' the packages GNU GMP, GNU MPFR, and MPC that are used by GCC for floating point support.
The GNU Compiler Collection is an advanced piece of software with dependencies. You need the following in order to build GCC:


* A Unix-like environment (Windows users can use the Windows Subsystem for Linux or Cygwin)
You need a host system with a working GCC installation, and enough memory as well as hard drive space. How much qualifies as "enough" is depending on the versions of the software involved, but GCC is a big piece of software, so don't be surprised when 128 or 256 MByte are not sufficient.
* Enough memory and hard disk space (it depends, 256 MiB will not be enough).

* GCC (existing release you wish to replace), or another system C compiler
In short you need the following that you can install manually or through package management:
* G++ (if building a version of GCC >= 4.8.0), or another system C++ compiler
* An Unix-like environment (Windows users)
* Make
* GCC (existing release you wish to replace)
* Bison
* G++ (if building a version of GCC >= 4.8.0)
* GNU Make
* GNU Bison
* Flex
* Flex
* GNU GMP
* GMP
* GNU MPFR
* MPFR
* GNU MPC
* MPC
* Texinfo
* Texinfo
* ISL (optional)
* ISL (optional)
* CLooG (optional)


=== Downloading the Source Code ===
=== Installing Dependencies ===
You can download the desired binutils release by visiting the [https://gnu.org/software/binutils/ Binutils website] or directly accessing the [ftp://ftp.gnu.org/gnu/binutils/ GNU main ftp mirror].


{| {{wikitable}}
You can download the desired gcc release by visiting the [https://gnu.org/software/gcc/ GCC website] or directly accessing the [ftp://ftp.gnu.org/gnu/gcc/ GNU main ftp mirror].
|-
! &darr; Dependency / OS &rarr;
| Source Code
| Debian (Ubuntu, Mint, WSL, ...)
| Gentoo
| Fedora
| Cygwin
| OpenBSD
| Arch
|-
|How to install
|Normally
|'''<tt>sudo apt install</tt> ''foo'''''
|'''<tt>sudo emerge --ask</tt> ''foo'''''
|'''<tt>sudo dnf install</tt> ''foo'''''
|Cygwin GUI setup
|'''<tt>doas pkg_add</tt> ''foo'''''
|'''<tt>pacman -Syu</tt> ''foo'''''
|-
|Compiler
|N/A
|'''<tt>build-essential</tt>'''
|'''<tt>sys-devel/gcc</tt>'''
|'''<tt>gcc gcc-c++</tt>'''
|'''<tt>mingw64-x86_64-gcc-g++</tt>''' / '''<tt>mingw64-i686-gcc-g++</tt>'''
|Preinstalled
|'''<tt>base-devel</tt>'''
|-
|Make
|N/A
|'''<tt>build-essential</tt>'''
|'''<tt>dev-build/make</tt>'''
|'''<tt>make</tt>'''
|'''<tt>make</tt>'''
|Preinstalled
|'''<tt>base-devel</tt>'''
|-
|[https://www.gnu.org/software/bison/ Bison]
|[https://ftp.gnu.org/gnu/bison/]
|'''<tt>bison</tt>'''
|'''<tt>sys-devel/bison</tt>'''
|'''<tt>bison</tt>'''
|'''<tt>bison</tt>'''
|?
|'''<tt>base-devel</tt>'''
|-
|[https://github.com/westes/flex Flex]
|[https://github.com/westes/flex/releases]
|'''<tt>flex</tt>'''
|'''<tt>sys-devel/flex</tt>'''
|'''<tt>flex</tt>'''
|'''<tt>flex</tt>'''
|?
|'''<tt>base-devel</tt>'''
|-
|[https://gmplib.org/ GMP]
|[https://ftp.gnu.org/gnu/gmp/]
|'''<tt>libgmp3-dev</tt>'''
|'''<tt>dev-libs/gmp</tt>'''
|'''<tt>gmp-devel</tt>'''
|'''<tt>libgmp-devel</tt>'''
|'''<tt>gmp</tt>'''
|'''<tt>gmp</tt>'''
|-
|MPC
|[https://ftp.gnu.org/gnu/mpc/]
|'''<tt>libmpc-dev</tt>'''
|'''<tt>dev-libs/mpc</tt>'''
|'''<tt>libmpc-devel</tt>'''
|'''<tt>libmpc-devel</tt>'''
|'''<tt>libmpc</tt>'''
|'''<tt>libmpc</tt>'''
|-
|[https://www.mpfr.org/ MPFR]
|[https://ftp.gnu.org/gnu/mpfr/]
|'''<tt>libmpfr-dev</tt>'''
|'''<tt>dev-libs/mpfr</tt>'''
|'''<tt>mpfr-devel</tt>'''
|'''<tt>libmpfr-devel</tt>'''
|'''<tt>mpfr</tt>'''
|'''<tt>mpfr</tt>'''
|-
|[https://www.gnu.org/software/texinfo/ Texinfo]
|[https://ftp.gnu.org/gnu/texinfo/]
|'''<tt>texinfo</tt>'''
|'''<tt>sys-apps/texinfo</tt>'''
|'''<tt>texinfo</tt>'''
|'''<tt>texinfo</tt>'''
|'''<tt>texinfo</tt>'''
|'''<tt>base-devel</tt>'''
|-
|[http://isl.gforge.inria.fr/ ISL] (Optional)
|[http://isl.gforge.inria.fr/]
|'''<tt>libisl-dev</tt>'''
|'''<tt>dev-libs/isl</tt>'''
|'''<tt>isl-devel</tt>'''
|'''<tt>libisl-devel</tt>'''
|N/A
|N/A
|}


You need to have Texinfo installed to build Binutils. You need to have GMP, MPC, and MPFR installed to build GCC. GCC optionally can make use of the ISL library.
In addition, to build GCC you need to have installed GNU GMP, GNU MPFR, GNU MPC and the ISL library. You may already have these libraries and the development files installed, but this tutorial builds them as part of GCC. If you don't need this, simply don't build them as part of GCC. Note that not all GMP, MPFR and MPC combinations are compatible with a given GCC release. You also need texinfo to build binutils.


For instance, you can install '''<tt>libgmp3-dev</tt>''' on Debian by running the shell command: '''<tt>sudo apt install libgmp3-dev</tt>'''
You can download [http://gmplib.org/ GNU GMP from its website]. (libgmp3-dev on apt-based systems, dev-libs/gmp on Gentoo, gmp-devel on Fedora, libgmp-devel on Cygwin)


'''Note:''' Version 5.x (or later) of Texinfo is known to be incompatible with the current Binutils 2.23.2 release (and older). You can check your current version using <tt>makeinfo --version</tt>. If your version is too new and you encounter problems during the build, you will need to either use Binutils 2.24 release (or newer) or install an older version of Texinfo - perhaps through building from source - and add it to your <tt>PATH</tt> prior and during the Binutils build.
You can download [http://mpfr.org/ GNU MPFR from its website]. (libmpfr-dev on apt-based systems, dev-libs/mpfr on Gentoo, mpfr-devel on Fedora, libmpfr-devel on Cygwin)


'''Note:''' GCC has dropped support for CLooG in the 5.x releases [https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=r5-2770-geae1a5d419d30e], and many distributions have dropped the CLooG package (e.g. Gentoo in December 2022).
You can download [http://isl.gforge.inria.fr/ ISL from its website] (optional). (libisl-dev on apt-based systems, libisl-devel on Cygwin)


'''Note:''' GCC requires at least ISL version 0.15.
You can download [http://www.cloog.org/ ClooG from its website] (optional). (libcloog-isl-dev on apt-based systems, libcloog-isl-devel on Cygwin)


'''Note:''' If you are using '''Cygwin''', it is recommended to install the <tt>'''libintl-devel'''</tt> package (I couldn't build a cross-compiler without this package)
You can download [http://multiprecision.org/ GNU MPC from its website]. (libmpc-dev on apt-based systems, dev-libs/mpc on Gentoo, libmpc-devel on Fedora, libmpc-devel on Cygwin)


=== Downloading the Source Code ===
You can download [https://www.gnu.org/software/texinfo/ GNU Texinfo from its website]. (texinfo on apt-based systems, texinfo on Arch Linux, sys-apps/texinfo on Gentoo, texinfo on Cygwin)


Download the needed source code into a suitable directory such as <tt>$HOME/src</tt>.
Download the needed source code into a suitable directory such as <tt>$HOME/src</tt>:


* You can download the desired Binutils release by visiting the [https://gnu.org/software/binutils/ Binutils website] or directly accessing the [https://ftp.gnu.org/gnu/binutils/ GNU main mirror].
An alternative way to download GMP, MPFR and MPC is to go to the top level directory of the GCC source code (i.e. gcc-x.y.z/) and type the following:<source lang="bash">contrib/download_prerequisites</source> The download_prerequisites script will run and will download GMP, MPFR and MPC.


* You can download the desired GCC release by visiting the [https://gnu.org/software/gcc/ GCC website] or directly accessing the [https://ftp.gnu.org/gnu/gcc/ GNU main mirror].
'''Note:''' The versioning scheme used is that each fullstop separates a full number, i.e. binutils 2.20.0 is newer than 2.9.0. This may be confusing, if you have not encountered this (quite common) versioning scheme yet, when looking at an alphanumerically sorted list of tarballs: The file at the bottom of the list is not the latest version! An easy way of getting the latest version is to sort by the last modified date and scrolling to the bottom.


'''Note:''' The versioning scheme used is that each fullstop separates a full number, i.e. Binutils 2.20.0 is newer than 2.9.0. This may be confusing, if you have not encountered this (quite common) versioning scheme yet, when looking at an alphanumerically sorted list of tarballs: The file at the bottom of the list is not the latest version! An easy way of getting the latest version is to sort by the last modified date and scrolling to the bottom.
'''Note:''' Version 5.x (or later) of texinfo is known to be incompatible with the current binutils 2.23.2 release (and older). You can check your current version using <tt>makeinfo --version</tt>. If your version is too new and you encounter problems during the build, you will need to either use binutils 2.24 release (or newer) or install an older version of texinfo - perhaps through building from source - and add it to your <tt>PATH</tt> prior and during the binutils build.


=== Linux Users building a System Compiler ===
'''Note:''' Version 0.13 (or later) of ISL is incompatible with the current CLooG 0.18.1 release (and older). Use version 0.12.2 of ISL or the build '''will''' fail.
Your distribution may ship its own patched GCC and Binutils that is customized to work on your particular Linux distribution. You should be able to build a working cross-compiler using the above source code, but you might not be able to build a new system compiler for your current Linux distribution. In that case, try a newer GCC release or get the patched source code.


=== Linux Users ===
=== Gentoo Users ===
Your distribution may ship its own patched GCC and Binutils that is customized to work on your particular Linux distribution. You may not be able to build a functional system compiler using the upstream sources you downloaded above. In that case, try a newer GCC release or get the patched source code. For instance, some GCC releases are known to not understand the new Debian multiarch directory structure. However, if the compiler we are about to build is a cross-compiler targetting another operating system (such as your new one), then this is much less a worry.


Gentoo offers [https://wiki.gentoo.org/wiki/Crossdev crossdev] to set up a cross-development toolchain:
'''Note for all Gentoo users:'''
Gentoo, being a source-based distribution, makes it almost ridiculously easy to set up a cross-development toolchain:


emerge -av crossdev
emerge -av crossdev
Line 60: Line 157:
This will install a GCC cross-compiler into a "slot", i.e. alongside already-existing compiler versions. You can install several cross-compilers that way, simply by changing target designations. An unfortunate downside is that it will also pull in gentoo patches and pass additional configure options that differ from the official [[GCC Cross-Compiler]] setup, and they might behave differently.
This will install a GCC cross-compiler into a "slot", i.e. alongside already-existing compiler versions. You can install several cross-compilers that way, simply by changing target designations. An unfortunate downside is that it will also pull in gentoo patches and pass additional configure options that differ from the official [[GCC Cross-Compiler]] setup, and they might behave differently.


After the compilation ran, you can now use your cross-compiler by calling <target>-gcc. You can also use gcc-config to toggle between compiler versions should you need to do so. Don't replace your system compiler with a cross-compiler however. The package manager will also suggest updates as soon as they become available.
After the compilation finishes successfully, your cross-compiler is callable via <target>-gcc. You can also use gcc-config to toggle between compiler versions should you need to do so. Don't replace your system compiler with a cross-compiler. The package manager will also suggest updates as soon as they become available.


You can uninstall the cross-compiler by calling ''crossdev --clean <target>''. Read the [http://www.gentoo.org/proj/en/base/embedded/cross-development.xml cross-development] document for additional information.
You can uninstall the cross-compiler by calling ''crossdev --clean <target>''. Read the [https://wiki.gentoo.org/wiki/Cross_build_environment cross-development] document for additional information.


Note that the version numbers to binutils and gcc are ''Gentoo package versions'', i.e. there might be a suffix to the "official" (GNU) version that addresses additional patchsets supplied by the Gentoo maintainers. (For example, ''--binutils 2.24-r3 --gcc 4.8.3'' is the latest stable package pair at the time of this writing.) You can omit the version numbers to use the latest package available.
Note that the version numbers to binutils and gcc are ''Gentoo package versions'', i.e. there might be a suffix to the "official" (GNU) version that addresses additional patchsets supplied by the Gentoo maintainers. (For example, ''--binutils 2.24-r3 --gcc 4.8.3'' is the latest stable package pair at the time of this writing.) You can omit the version numbers to use the latest package available.


Portage uses overlays to store packages that are not part of the original package management. Crossdev needs one overlay where it can store its binutils and gcc packages before building them. You can configure one properly, or you can use PORTDIR_OVERLAY to point at where it should keep its package manager files. Using PORTDIR_OVERLAY is not a good idea with existing overlays, but by then you should know how you have personally set them up earlier anyway and how to do it properly.
Portage uses overlays to store packages that are not part of the original package management. Crossdev needs one overlay where it can store its binutils and gcc packages before building them. You can configure one properly, or you can use PORTDIR_OVERLAY to point at where it should keep its package manager files. Using PORTDIR_OVERLAY is not a good idea with existing overlays, but by then you should know how you have personally set them up earlier anyway and how to do it properly. See [https://wiki.gentoo.org/wiki/Custom_repository#Crossdev].


=== Mac OS X Users ===
=== macOS Users ===
Additionally, Mac OS X users need a replacement libiconv because the system libiconv is seriously out of date. Mac OS X users can download the latest libiconv release by visiting the [https://gnu.org/software/libiconv/ libiconv website] or directly accessing the [ftp://ftp.gnu.org/gnu/libiconv/ GNU main ftp mirror].
macOS users need a replacement libiconv because the system libiconv is seriously out of date. macOS users can download the latest libiconv release by visiting the [https://gnu.org/software/libiconv/ libiconv website] or directly accessing the [https://ftp.gnu.org/gnu/libiconv/ GNU main FTP mirror]. Otherwise you may get unresolved symbol errors related to libiconv when compiling GCC 4.3 or higher on OS X 10.4 and 10.5.


When compiling GCC 4.3 or higher on OS X 10.4 and 10.5, you may get unresolved symbol errors related to libiconv. This is because the version shipped with OS X is seriously out of date. Install a new version (compile it yourself or use macports) and add <tt>--with-libiconv-prefix=/opt/local</tt> (or <tt>/usr/local</tt> if you compiled it yourself) to GCC's <tt>./configure</tt> line. Alternatively you may place the libiconv source as gcc-x.y.z/libiconv and it will be compiled as part of the GCC compilation process. (This trick also works for mpfr, gmp, and mpc).
Install a new version (compile it yourself or use MacPorts) and add <tt>--with-libiconv-prefix=/opt/local</tt> (or <tt>/usr/local</tt> if you compiled it yourself) to GCC's <tt>./configure</tt> line. Alternatively you may place the libiconv source as gcc-x.y.z/libiconv and it will be compiled as part of the GCC compilation process. (This trick also works for MPFR, GMP, and MPC).


The makefiles of binutils and GCC use the <source lang="make" enclose="none">$(CC)</source> variable to invoke the compiler. On OS X, this resolves to <source lang="text" enclose="none">gcc</source> by default, which is actually not the "real thing", but <source lang="bash" enclose="none">clang</source>. Note that since at least OS X 10.8, Xcode's Command Line Tools package comes with clang, and this version of clang does indeed work to compile a working version of GCC, unlike what these instructions previously reflected.
The makefiles of Binutils and GCC use the <syntaxhighlight lang="make" inline>$(CC)</syntaxhighlight> variable to invoke the compiler. On OS X, this resolves to <syntaxhighlight lang="text" inline>gcc</syntaxhighlight> by default, which is actually <syntaxhighlight lang="bash" inline>clang</syntaxhighlight>. Prior to OS X 10.8, the Clang that came with Xcode's Command Line Tools package was not able to build a working GCC. Users running OS X 10.7 or below may need to find and install GCC, either from [https://brew.sh Homebrew], or from somewhere on Apple's website. You can try with the old GCC that comes preinstalled on some macOS versions.


<syntaxhighlight lang="bash">
Note that users running OS X 10.7 may need to find and install gcc, either from [http://brew.sh homebrew], or from somewhere on Apple's website.
Thus, the instructions below are really only relevant for these users, but your mileage may vary.
<source lang="bash">
# This is only necessary for OS X users running 10.7 or below.
# This is only necessary for OS X users running 10.7 or below.
export CC=/usr/bin/gcc-4.2
export CC=/usr/bin/gcc-4.2
Line 83: Line 178:
export CPP=/usr/bin/cpp-4.2
export CPP=/usr/bin/cpp-4.2
export LD=/usr/bin/gcc-4.2
export LD=/usr/bin/gcc-4.2
</syntaxhighlight>
</source>


You might want to unset these exports once you compiled and installed the cross compiler, as it might confuse other builds. '''Do not''' make these permanent!
You will want to unset these exports once you compiled and installed the cross compiler.


'''Note for Lion users:''' If you're on Lion (or above) chances are that you don't have the "real" gcc since Apple removed it from the Xcode package, but you can still install it. You can do it via Homebrew or by compiling from source, both are perfectly described on [http://apple.stackexchange.com/a/38247 a StackExchange answer].
'''Note for Lion users:''' If you're on Lion (or above) chances are that you don't have the "real" GCC since Apple removed it from the Xcode package, but you can still install it. You can do it via Homebrew or by compiling from source, both are perfectly described on [https://apple.stackexchange.com/a/38247 a StackExchange answer].

'''Note for Maverick users:''' You can build binutils-2.24 and gcc-4.8.3 (possible other version) with Xcode 5.1.1. Note that building GCC with LLVM is not officially supported and may cause interesting bugs, if you are willing to take this risk and save time building host-gcc just to compile a cross-gcc, follow this.
Install GMP, MPFR, Mpc with [https://www.macports.org/ MacPorts].


'''Note for Maverick users:''' You can build binutils-2.24 and gcc-4.8.3 (possible other version) with xcode 5.1.1. Note that building gcc with llvm is not officially supported and may cause interesting bugs, if you are willing to take this risk and save time building host-gcc just to compile a cross-gcc, follow this.
Install GMP, MPFR, MPC with [http://http://www.macports.org/ macport].
sudo port install gmp mpfr libmpc
sudo port install gmp mpfr libmpc


Line 105: Line 201:
--with-gmp=/usr --with-mpc=/opt/local --with-mpfr=/opt/local
--with-gmp=/usr --with-mpc=/opt/local --with-mpfr=/opt/local


*Note that there is issue with port's gmp, we use the version from mac os x from /usr instead.
'''Note:''' There is an issue with port's GMP, so the version from OS X from /usr is used instead.
'''Note2:''' If you still have some errors, try making a case-sensitive APFS disk image using disk utility app and build from there


=== Windows Users ===
=== Windows Users ===
Windows users need to set up a Unix-like enviroment such as [[MinGW]] or [[Cygwin]]. It may well be worth looking into systems such as Linux and see if they fit your needs, as you commonly use a lot of Unix-like tools in operating systems development and this is much easier from a Unix-like operating system. '''If you have just installed the basic [[Cygwin]] package, you have to run the setup.exe again and install the following packages:''' GCC, G++, Make, Flex, Bison, Diffutils, libintl-devel, libgmp-devel, libmpfr-devel, libmpc-devel, texinfo
Windows users need to set up a Unix-like enviroment such as [[MinGW]] or [[Cygwin]]. It may well be worth looking into systems such as Linux and see if they fit your needs, as you commonly use a lot of Unix-like tools in operating systems development and this is much easier from a Unix-like operating system. '''If you have just installed the basic [[Cygwin]] package, you have to run the setup.exe again and install the following packages:''' GCC, G++, Make, Flex, Bison, Diffutils, libintl-devel, libgmp-devel, libmpfr-devel, libmpc-devel, Texinfo


MinGW + MSYS is an option, and as it addresses the native Windows API instead of a POSIX emulation layer, results in a slightly faster toolchain. Some software packages will not build properly under MSYS as they were not designed for use with Windows. As far as this tutorial is concerned, everything that applies to Cygwin also applies to MSYS unless otherwise specified. Make sure you install the C and C++ compilers, and the MSYS Basic System.
MinGW + MSYS is an option, and as it addresses the native Windows API instead of a POSIX emulation layer, results in a slightly faster toolchain. Some software packages will not build properly under MSYS as they were not designed for use with Windows. As far as this tutorial is concerned, everything that applies to Cygwin also applies to MSYS unless otherwise specified. Make sure you install the C and C++ compilers, and the MSYS Basic System.


The "Windows Subsystem for Linux (Beta)", released with the Windows 10 Anniversary update is also an option for using a cross compiler. (Tested 08/08/2016 with GCC 6.1.0 and binutils 2.27) This cross-compiler works reasonably fast, although being in beta state, it may not be ideal permanent development platform.
The "Windows Subsystem for Linux (Beta)", released with the Windows 10 Anniversary update is also an option for using a cross compiler. (Tested 08/08/2016 with GCC 6.1.0 and Binutils 2.27) This cross-compiler works reasonably fast, although being in beta state, it may not be ideal permanent development platform.


'''Cygwin note:''' Cygwin includes your Windows <tt>%PATH%</tt> in its bash <tt>$PATH.</tt> If you were using DJGPP before, this could result in confusion as e.g. calling <tt>gcc</tt> on the Cygwin bash command line would still call the DJGPP compiler. After uninstalling DJGPP, you should delete the DJGPP environment variable and clear the <tt>C:\djgpp</tt> entry (or wherever you installed it) from your <tt>%PATH%</tt>. Likewise, it might be a bad idea to mix build environments in your system PATH variable.
'''Cygwin note:''' Cygwin includes your Windows <tt>%PATH%</tt> in its bash <tt>$PATH.</tt> If you were using DJGPP before, this could result in confusion as e.g. calling <tt>GCC</tt> on the Cygwin bash command line would still call the DJGPP compiler. After uninstalling DJGPP, you should delete the DJGPP environment variable and clear the <tt>C:\djgpp</tt> entry (or wherever you installed it) from your <tt>%PATH%</tt>. Likewise, it might be a bad idea to mix build environments in your system PATH variable.


'''MinGW note:''' Some MinGW-specific information on building a cross-toolchain can be found on the [http://www.mingw.org/wiki/HostedCrossCompilerHOWTO hosted cross-compiler how-to page] on the MinGW homepage.
'''MinGW note:''' Some MinGW-specific information on building a cross-toolchain can be found on the [http://www.mingw.org/wiki/HostedCrossCompilerHOWTO hosted cross-compiler how-to page] on the MinGW homepage.


'''Windows Subsystem for Linux (Beta) Note:''' You cannot have your cross compiler in the /mnt/c/ (or /mnt/"x") areas, as trying to compile your cross-compiler there will generate errors, whereas building to $HOME/opt/cross works perfectly. This is fixed with Windows Update KB3176929
'''Windows Subsystem for Linux (Beta) Note:''' You cannot have your cross compiler in the /mnt/c/ (or /mnt/"x") areas, as trying to compile your cross-compiler there will generate errors, whereas building to $HOME/opt/cross works perfectly. This is fixed with Windows Update KB3176929

=== OpenBSD Users ===
OpenBSD users might need to install "gcc" package from ports because base system's GCC is very outdated. If you want to build GCC, try to use the ports' version instead of the latest version available and apply all patches from ports to your build. Also, if the build fails during compiling lto-plugin, a temporary solution is to disable LTO altogether during configure stage of building GCC by adding <tt>--disable-lto</tt>

[[Category:Compilers]]

Latest revision as of 06:27, 9 June 2024

Difficulty level

Beginner

Note that this article is meant to be included into the GCC Cross-Compiler and Building GCC as they share this common section.

The GNU Compiler Collection is an advanced piece of software with dependencies. You need the following in order to build GCC:

  • A Unix-like environment (Windows users can use the Windows Subsystem for Linux or Cygwin)
  • Enough memory and hard disk space (it depends, 256 MiB will not be enough).
  • GCC (existing release you wish to replace), or another system C compiler
  • G++ (if building a version of GCC >= 4.8.0), or another system C++ compiler
  • Make
  • Bison
  • Flex
  • GMP
  • MPFR
  • MPC
  • Texinfo
  • ISL (optional)

Installing Dependencies

↓ Dependency / OS → Source Code Debian (Ubuntu, Mint, WSL, ...) Gentoo Fedora Cygwin OpenBSD Arch
How to install Normally sudo apt install foo sudo emerge --ask foo sudo dnf install foo Cygwin GUI setup doas pkg_add foo pacman -Syu foo
Compiler N/A build-essential sys-devel/gcc gcc gcc-c++ mingw64-x86_64-gcc-g++ / mingw64-i686-gcc-g++ Preinstalled base-devel
Make N/A build-essential dev-build/make make make Preinstalled base-devel
Bison [1] bison sys-devel/bison bison bison ? base-devel
Flex [2] flex sys-devel/flex flex flex ? base-devel
GMP [3] libgmp3-dev dev-libs/gmp gmp-devel libgmp-devel gmp gmp
MPC [4] libmpc-dev dev-libs/mpc libmpc-devel libmpc-devel libmpc libmpc
MPFR [5] libmpfr-dev dev-libs/mpfr mpfr-devel libmpfr-devel mpfr mpfr
Texinfo [6] texinfo sys-apps/texinfo texinfo texinfo texinfo base-devel
ISL (Optional) [7] libisl-dev dev-libs/isl isl-devel libisl-devel N/A N/A

You need to have Texinfo installed to build Binutils. You need to have GMP, MPC, and MPFR installed to build GCC. GCC optionally can make use of the ISL library.

For instance, you can install libgmp3-dev on Debian by running the shell command: sudo apt install libgmp3-dev

Note: Version 5.x (or later) of Texinfo is known to be incompatible with the current Binutils 2.23.2 release (and older). You can check your current version using makeinfo --version. If your version is too new and you encounter problems during the build, you will need to either use Binutils 2.24 release (or newer) or install an older version of Texinfo - perhaps through building from source - and add it to your PATH prior and during the Binutils build.

Note: GCC has dropped support for CLooG in the 5.x releases [8], and many distributions have dropped the CLooG package (e.g. Gentoo in December 2022).

Note: GCC requires at least ISL version 0.15.

Note: If you are using Cygwin, it is recommended to install the libintl-devel package (I couldn't build a cross-compiler without this package)

Downloading the Source Code

Download the needed source code into a suitable directory such as $HOME/src:

Note: The versioning scheme used is that each fullstop separates a full number, i.e. Binutils 2.20.0 is newer than 2.9.0. This may be confusing, if you have not encountered this (quite common) versioning scheme yet, when looking at an alphanumerically sorted list of tarballs: The file at the bottom of the list is not the latest version! An easy way of getting the latest version is to sort by the last modified date and scrolling to the bottom.

Linux Users building a System Compiler

Your distribution may ship its own patched GCC and Binutils that is customized to work on your particular Linux distribution. You should be able to build a working cross-compiler using the above source code, but you might not be able to build a new system compiler for your current Linux distribution. In that case, try a newer GCC release or get the patched source code.

Gentoo Users

Gentoo offers crossdev to set up a cross-development toolchain:

   emerge -av crossdev
   crossdev --help
   PORTDIR_OVERLAY="/usr/local/crossdev" crossdev --stage1 --binutils <binutils-version> --gcc <gcc-version> --target <target>

This will install a GCC cross-compiler into a "slot", i.e. alongside already-existing compiler versions. You can install several cross-compilers that way, simply by changing target designations. An unfortunate downside is that it will also pull in gentoo patches and pass additional configure options that differ from the official GCC Cross-Compiler setup, and they might behave differently.

After the compilation finishes successfully, your cross-compiler is callable via <target>-gcc. You can also use gcc-config to toggle between compiler versions should you need to do so. Don't replace your system compiler with a cross-compiler. The package manager will also suggest updates as soon as they become available.

You can uninstall the cross-compiler by calling crossdev --clean <target>. Read the cross-development document for additional information.

Note that the version numbers to binutils and gcc are Gentoo package versions, i.e. there might be a suffix to the "official" (GNU) version that addresses additional patchsets supplied by the Gentoo maintainers. (For example, --binutils 2.24-r3 --gcc 4.8.3 is the latest stable package pair at the time of this writing.) You can omit the version numbers to use the latest package available.

Portage uses overlays to store packages that are not part of the original package management. Crossdev needs one overlay where it can store its binutils and gcc packages before building them. You can configure one properly, or you can use PORTDIR_OVERLAY to point at where it should keep its package manager files. Using PORTDIR_OVERLAY is not a good idea with existing overlays, but by then you should know how you have personally set them up earlier anyway and how to do it properly. See [9].

macOS Users

macOS users need a replacement libiconv because the system libiconv is seriously out of date. macOS users can download the latest libiconv release by visiting the libiconv website or directly accessing the GNU main FTP mirror. Otherwise you may get unresolved symbol errors related to libiconv when compiling GCC 4.3 or higher on OS X 10.4 and 10.5.

Install a new version (compile it yourself or use MacPorts) and add --with-libiconv-prefix=/opt/local (or /usr/local if you compiled it yourself) to GCC's ./configure line. Alternatively you may place the libiconv source as gcc-x.y.z/libiconv and it will be compiled as part of the GCC compilation process. (This trick also works for MPFR, GMP, and MPC).

The makefiles of Binutils and GCC use the $(CC) variable to invoke the compiler. On OS X, this resolves to gcc by default, which is actually clang. Prior to OS X 10.8, the Clang that came with Xcode's Command Line Tools package was not able to build a working GCC. Users running OS X 10.7 or below may need to find and install GCC, either from Homebrew, or from somewhere on Apple's website. You can try with the old GCC that comes preinstalled on some macOS versions.

# This is only necessary for OS X users running 10.7 or below.
export CC=/usr/bin/gcc-4.2
export CXX=/usr/bin/g++-4.2
export CPP=/usr/bin/cpp-4.2
export LD=/usr/bin/gcc-4.2

You will want to unset these exports once you compiled and installed the cross compiler.

Note for Lion users: If you're on Lion (or above) chances are that you don't have the "real" GCC since Apple removed it from the Xcode package, but you can still install it. You can do it via Homebrew or by compiling from source, both are perfectly described on a StackExchange answer.

Note for Maverick users: You can build binutils-2.24 and gcc-4.8.3 (possible other version) with Xcode 5.1.1. Note that building GCC with LLVM is not officially supported and may cause interesting bugs, if you are willing to take this risk and save time building host-gcc just to compile a cross-gcc, follow this. Install GMP, MPFR, Mpc with MacPorts.

sudo port install gmp mpfr libmpc
../binutils-2.24/configure --prefix=$PREFIX \
--target=$TARGET \
--enable-interwork --enable-multilib \
--disable-nls --disable-werror
../gcc-4.8.3/configure --prefix=$PREFIX \
--target=$TARGET \
--disable-nls \
--enable-languages=c,c++ --without-headers \
--enable-interwork --enable-multilib \
--with-gmp=/usr --with-mpc=/opt/local --with-mpfr=/opt/local

Note: There is an issue with port's GMP, so the version from OS X from /usr is used instead. Note2: If you still have some errors, try making a case-sensitive APFS disk image using disk utility app and build from there

Windows Users

Windows users need to set up a Unix-like enviroment such as MinGW or Cygwin. It may well be worth looking into systems such as Linux and see if they fit your needs, as you commonly use a lot of Unix-like tools in operating systems development and this is much easier from a Unix-like operating system. If you have just installed the basic Cygwin package, you have to run the setup.exe again and install the following packages: GCC, G++, Make, Flex, Bison, Diffutils, libintl-devel, libgmp-devel, libmpfr-devel, libmpc-devel, Texinfo

MinGW + MSYS is an option, and as it addresses the native Windows API instead of a POSIX emulation layer, results in a slightly faster toolchain. Some software packages will not build properly under MSYS as they were not designed for use with Windows. As far as this tutorial is concerned, everything that applies to Cygwin also applies to MSYS unless otherwise specified. Make sure you install the C and C++ compilers, and the MSYS Basic System.

The "Windows Subsystem for Linux (Beta)", released with the Windows 10 Anniversary update is also an option for using a cross compiler. (Tested 08/08/2016 with GCC 6.1.0 and Binutils 2.27) This cross-compiler works reasonably fast, although being in beta state, it may not be ideal permanent development platform.

Cygwin note: Cygwin includes your Windows %PATH% in its bash $PATH. If you were using DJGPP before, this could result in confusion as e.g. calling GCC on the Cygwin bash command line would still call the DJGPP compiler. After uninstalling DJGPP, you should delete the DJGPP environment variable and clear the C:\djgpp entry (or wherever you installed it) from your %PATH%. Likewise, it might be a bad idea to mix build environments in your system PATH variable.

MinGW note: Some MinGW-specific information on building a cross-toolchain can be found on the hosted cross-compiler how-to page on the MinGW homepage.

Windows Subsystem for Linux (Beta) Note: You cannot have your cross compiler in the /mnt/c/ (or /mnt/"x") areas, as trying to compile your cross-compiler there will generate errors, whereas building to $HOME/opt/cross works perfectly. This is fixed with Windows Update KB3176929

OpenBSD Users

OpenBSD users might need to install "gcc" package from ports because base system's GCC is very outdated. If you want to build GCC, try to use the ports' version instead of the latest version available and apply all patches from ports to your build. Also, if the build fails during compiling lto-plugin, a temporary solution is to disable LTO altogether during configure stage of building GCC by adding --disable-lto