Detailed explanation of how to compile a cross-platform GCC

Source: Internet
Author: User
Tags gmp mkdir

How to compile a cross-platform gcc, the so-called cross-platform means that GCC compiles in a machine type but the compiled application is on a machine type, which we refer to as a cross-platform compilation of this article is reproduced in: http://preshing.com/ 20141119/how-to-build-a-gcc-cross-compiler/

How to build a GCC cross-compiler

GCC is not just a compiler. It's an open source project this lets you to build all kinds of compilers. Some compilers support multithreading; Some support shared libraries; Some support multilib. It all depends in how do you configure the compiler before building it.

This guide would demonstrate how to build a Cross-compiler, which are a compiler that builds programs for another. All need are a unix-like environment with a recent version of GCC already installed.

In this guide, I'll use the Debian Linux to builds a full C + + Cross-compiler for AArch64, a 64-bit instruction set available in The latest ARM processors. I don ' t actually own an AArch64 device–i just wanted a AArch64 to compiler this bug. Required Packages

Starting with a clean Debian system, must install a few packages:

$ sudo apt-get install g++ make gawk

Everything else is built from source. Create A new directory somewhere, and download the following source packages. (If you ' re following this guide at a later date, there'll be more recent releases of each package available.) Check for newer releases by pasting each URL into your browser without the filename. For example:http://ftpmirror.gnu.org/binutils/)

 $ wget http://ftpmirror.gnu.org/binutils/binutils-2.24.tar.gz $ wget http://ftpmirror.gnu.org/gcc/gcc-4.9.2/ Gcc-4.9.2.tar.gz $ wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.17.2.tar.xz $ wget http:// FTPMIRROR.GNU.ORG/GLIBC/GLIBC-2.20.TAR.XZ $ wget http://ftpmirror.gnu.org/mpfr/mpfr-3.1.2.tar.xz $ wget http:// FTPMIRROR.GNU.ORG/GMP/GMP-6.0.0A.TAR.XZ $ wget http://ftpmirror.gnu.org/mpc/mpc-1.0.2.tar.gz $ wget ftp:// GCC.GNU.ORG/PUB/GCC/INFRASTRUCTURE/ISL-0.12.2.TAR.BZ2 $ wget ftp://gcc.gnu.org/pub/gcc/infrastructure/ Cloog-0.18.1.tar.gz 

The four packages–binutils, GCC, the Linux kernel and glibc–are the main ones. We could have installed the next three packages in binary form using our system's Package Manager instead, but that tends To provide older versions. The last two packages, ISL and Cloog, are optional, but they to enable a few more optimizations in the compiler we ' re about t o Build. How the Pieces fit together

By the time we ' re finished, we'll have built each of the following programs and libraries. The ' ll builds the tools on the left, then we'll use those tools to build the programs and libraries on the right. We won ' t actually build the target system's Linux kernel, but we do need the kernel header files into order to build the tar Get system ' s standard C library.

The compilers on the left would invoke the assembler & Linker as part of their job. All of the other packages we downloaded, such as MPFR, GMP and MPC, 'll be linked into the compilers themselves.

The diagram on the right represents a sample program, A.out, running on the target OS, built using the cross compiler and Linked with the target system ' s standard C and C + + libraries. The standard C + + library makes calls to the standard C library, and the C library makes direct system calls to the AARCH64 Linux kernel.

Note This instead of using GLIBC as the standard C library implementation, we could have used newlib, an alternative imple Mentation. Newlib is a popular C library implementation for embedded devices. Unlike GLIBC, Newlib doesn ' t require a complete OS on target system–just a thin hardware abstraction layer called Li Bgloss. Newlib doesn ' t have regular releases; Instead, you ' re meant to pull the source directly from the Newlib CVS repository. One limitation of Newlib is this currently, it doesn ' t seem to support building-multithreaded for programs. That ' s why I chose. Build Steps

Extract all the source packages.

$ for f in *.tar*; Do tar XF $f; Done

Create Symbolic links from the "GCC directory to some of the" the other directories. These five packages are dependencies of GCC, and then the symbolic links are present, gcc ' build script would build them a Utomatically.

$ cd gcc-4.9.2
$ ln-s. /mpfr-3.1.2 MPFR
$ ln-s. /gmp-6.0.0 GMP
$ ln-s. /mpc-1.0.2 MPC
$ ln-s. /isl-0.12.2 ISL
$ ln-s. /cloog-0.18.1 Cloog
$ CD.

Choose an installation directory, and make sure your have write permission to it. In the steps that follow, I ' ll install the new toolchain To/opt/cross.

$ sudo mkdir-p/opt/cross
$ sudo chown jeff/opt/cross

Throughout the entire build process, make sure the installation's Bin subdirectory is in your pathenvironment. You can remove this directory from your PATH later, but most of the builds steps expect to find AARCH64-LINUX-GCC and other Host tools via the PATH by default.

$ export Path=/opt/cross/bin: $PATH

Pay particular attention to the stuff that gets installed. This directory is considered the system root of imaginary AArch64 Linux target system. A self-hosted AArch64 Linux compiler could, in theory, use the headers and libraries. Obviously, none of the programs built for the host system, such as the Cross-compiler itself, 'll be installed to this DI Rectory. 1. Binutils

This is step builds and installs the Cross-assembler, Cross-linker, and other tools.

$ mkdir build-binutils
$ cd build-binutils
$. /binutils-2.24/configure--prefix=/opt/cross--target=aarch64-linux--disable-multilib
$ make-j4
$ make Install
$ CD.
We ' ve specified aarch64-linux as the target system type. Binutils ' s configure script'll recognize that this target is different from the machine we ' re building on, and configure A cross-assembler and cross-linker as a result. The tools would be installed to/opt/cross/bin and their names prefixed by aarch64-linux-. --disable-multilib means so we only want our binutils installation to work with programs and libraries using the AARCH64 instruction set, and not any related instruction sets such as AArch32. 2. Linux Kernel Headers

This step installs the Linux kernel header files To/opt/cross/aarch64-linux/include, which'll ultimately allow Built using our new toolchain to make system calls to the AARCH64 kernel in the target environment.

$ CD linux-3.17.2
$ make arch=arm64 install_hdr_path=/opt/cross/aarch64-linux headers_install
$ CD.
We could even have do this before installing Binutils. The Linux kernel header files won ' t actually is used until step 6 while we build the standard C library, although the Conf Igure script in step 4 expects them to be already installed. Because the Linux kernel is a different Open-source project from the others, it has a different way of identifying the tar Get CPU architecture:arch=arm64

All of the remaining steps involve building GCC and GLIBC. The trick is this there are parts of GCC which depend on parts of Glibc already, being built, and vice versa. We can ' t build either package into a single step; We need to go back and forth between the two packages and builds their components in a way that satisfies their dependencie S.

3. C + + compilers

This step would build GCC ' C and C + + Cross-compilers only, and install them to/opt/cross/bin. It won ' t invoke those compilers to build any libraries just yet.

$ mkdir-p BUILD-GCC
$ cd BUILD-GCC
$. /gcc-4.9.2/configure--prefix=/opt/cross--target=aarch64-linux--enable-languages=c,c++--disable-multilib
$ Make-j4 ALL-GCC
$ make INSTALL-GCC
$ CD.
Because we ' ve specified

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.