Tag Archive : compiler

/ compiler

Building GCC MinGW Toolchain Cross Compiler

December 11, 2015 | Article | No Comments

Windows, up until today, is still popular Operating System and have large market. This Microsoft’s product is designed for end-user and reducing complexity of Operating System. Even when some other OS such as Linux and Mac OS starts invading, this Operating System is still major and fanatically used by some people in the world.

In this article we will about how to build GCC Win32 and Win64 Toolchain for Win32 and Win64 architecture, which is a cross compiler to build Win32 and Win64 program.

Requirement set for this article:

    1. GCC (Native platform)
    2. Binutils (Native platform)
    3. Bison
    4. Flex
    5. gperf

CoreutilsMake (>= 3.8.1)

  1. SVN & CVS client

Also the test is done on Slackware64 current, though it is optional. You can use other Linux distribution which suit you.

While the previous list is requirement on your host system (your system which will build the cross compiler), we have another list. Some of the component mentioned below will be taken out from their respective repository.

  1. MinGW-w64 (SVN repository)
  2. GCC 4.9.2
  3. Binutils 2.24
  4. GDB 7.8.1
  5. GMP 6.0.0
  6. MPFR 3.1.2
  7. MPC 1.0.2

The Target

There are two architecture: Win32 and Win64. You are free to choose what MinGW system you will build. However, in this article, we will build MinGW Cross Compiler for Windows 64-bit. If you read other cross compiler article, we need a target alias (or simply target) which is the target of the toolchain.

Preparation

We will need some disk space (~2GB should be enough). We also need root access to install it on “system-wide”.

We will create a working directory. We will refer it as $MINGWGCC, so $MINGWGCC/src should really be something like ~/MINGWGCC/src (the ~ means your home directory). So go ahead and create following directory structure:

# set aliases
export MINGWGCC=~/
export PREFIX=/usr/local
export BUILD=$(gcc -dumpmachine)
export TARGET=x86_64-w64-mingw32

export BINUTILS_BUILD=${MINGWGCC}/build/binutils-build
export GCC_BUILD=${MINGWGCC}/build/gcc-build
export GDB_BUILD=${MINGWGCC}/build/gdb-build
export MINGW_BUILD=${MINGWGCC}/build/mingw-build
export MINGW_H_BUILD=${MINGWGCC}/build/mingw-h-build

export BINUTILS_SRC=${MINGWGCC}/src/binutils-2.24
export GCC_SRC=${MINGWGCC}/src/gcc-4.9.2
export GDB_SRC=${MINGWGCC}/src/gdb-7.8.1
export MINGW_SRC=${MINGWGCC}/src/mingw/
export MINGW_HEADERS=${MINGW_SRC}/mingw-w64-headers

# make base dir, original archives dir (orig), source code dir (src), 
# and working / building dir (build) 
mkdir ${MINGWGCC}{,/{orig,src,build}}

# Make build directory for each component
mkdir ${MINGWGCC}/build/{binutils,gcc,gdb,mingw{,-h}}-build

Acquiring the Materials

A cross compiler for Win32 and Win64 are provided by MinGW-w64 project. Download latest packages of GCC, GDB, binutils, and MinGW-w64 source code. You can download it using wget or alternatively download via browser and move it to $MINGWGCC/orig.

cd ${MINGWGCC}/orig

wget ftp://ftp.gnu.org/pub/gnu/gcc/gcc-4.9.2/gcc-4.9.2.tar.bz2
wget ftp://ftp.gnu.org/pub/gnu/gdb/gdb-7.8.1.tar.xz
wget ftp://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.bz2

wget ftp://ftp.gnu.org/pub/gnu/gmp/gmp-6.0.0a.tar.xz
wget ftp://ftp.gnu.org/pub/gnu/mpc/mpc-1.0.2.tar.gz
wget ftp://ftp.gnu.org/pub/gnu/mpfr/mpfr-3.1.2.tar.gz

Then, extract them to src directory.

cd ${MINGWGCC}/src

tar -jxf ${MINGWGCC}/orig/gcc-4.9.2.tar.bz2
tar -Jxf ${MINGWGCC}/orig/gdb-7.8.1.tar.xz
tar -jxf ${MINGWGCC}/orig/binutils-2.24.tar.bz2

Next, the MinGW and GCC codes.

cd ${MINGWGCC}/src
svn co 'https://svn.code.sf.net/p/mingw-w64/code/trunk' mingw

Next we need to download some prerequisites, especially GMP (GNU Multiple Precision), MPC (Multiple-Precision Complex), and MPFR (Multiple-Precision Floating Point Reliably) which is used for computation. The prerequisites can be downloaded by invoking following command:

The MPFR, GMP, and MPC is not the latest version, but at least we can build GCC. Suppose your working directory at /home/xathrya/ARMGCC, to obtain MPDR, GMP, and MPC invoke following command:

cd ${GCC_SRC}
./contrib/download_prerequisites

However, the version of GMP, MPC, and MPFR is not the latest version. We can switch to the latest version by download the latest version and create symbolic link to it.

cd ${GCC_SRC}

rm {gmp,mpc,mpfr}

tar -Jxf ${MINGWGCC}/orig/gmp-6.0.0a.tar.xz
tar -zxf ${MINGWGCC}/orig/mpc-1.0.2.tar.gz
tar -zxf ${MINGWGCC}/orig/mpfr-3.1.2.tar.gz

ln -s gmp-6.0.0 gmp
ln -s mpc-1.0.2 mpc
ln -s mpfr-3.1.2 mpfr

Building the Toolchains

Building stage is pretty simple. Honestly speaking, the tricky part is about how GCC and built concept and ordering them in correct order.

Binutils

First, built binutils. This will provide lots of useful tools in building MinGW libraries and binaries (like objdump, ld, ranlib, etc). Invoke following commands:

cd ${BINUTILS_BUILD}
${BINUTILS_SRC}/configure \
   --target=${TARGET} \
   --enable-targets=x86_64-w64-mingw32,i686-w64-mingw32 \
   --prefix=${PREFIX} \
   --disable-nls
make -j4 all
make install
make clean

MinGW Headers

MinGW headers is set of headers used to build MinGW application. It is different beast with Linux GCC headers, although we use the same vendor compiler.

cd ${MINGW_H_BUILD}
${MINGW_HEADERS}/configure \
    --build=${BUILD} \
    --host=${TARGET} \
    --prefix=${PREFIX}/${TARGET}
make install

For v3 and later trunk, we have to append ${TARGET} to our prefix. If not, your native GCC will be contaminated. Please note it.

This will install MinGW headers into ${PREFIX}/${TARGET}/include/ or in our case it would be /usr/local/x86_64-w64-mingw32/include/.

MinGW Directories and Symlinks

We should end up with a ${PREFIX}/${TARGET}/ directory. Manually create lib/ directory and create symbolic link as lib64/

GCC also requires the ${TARGET} directory to be mirrored as a directory ‘mingw’ in the same root (it’s ${PREFIX} directory). Therefore, we need to make a soft link there.

n -s ${PREFIX}/${TARGET} ${PREFIX}/mingw
mkdir -p ${PREFIX}/${TARGET}/lib{,32}
ln -s ${PREFIX}/${TARGET}/lib ${PREFIX}/${TARGET}/lib64

GCC Core

Next we will build the GCC. But please note that we are only intereset on C and C++ so we will ignore other language. To built GCC, do this:

cd ${GCC_BUILD}
${GCC_SRC}/configure \
   --build=${BUILD} \
   --target=${TARGET} \
   --enable-targets=all \
   --prefix=${PREFIX} \
   --enable-interwork \
   --enable-multilib \
   --enable-languages="c,c++" \
   --enable-shared \
   --enable-fully-dynamic-string \
   --with-system-zlib \
   --disable-nls
make -j4 all-gcc install-gcc
make clean

C RunTime (CRT) Library

 Next we will build the C runtime library or the MinGW itself. In the short: building libs required. The static and dynamic lib built with GCC core we have built.

cd ${MINGW_BUILD}
${MINGW_SRC}/configure \
    --host=${TARGET} \
    --prefix=${PREFIX}/${TARGET} \
    --enable-multilib \
    --enable-shared \
    --enable-lib32
make -j4 all
make install

For v3 and later trunk, we have to append ${TARGET} to our prefix. If not, your native GCC will be contaminated. Please note it.

Make sure you have following directories in the ${PREFIX}:

# ${PREFIX} => /usr/local
# ${TARGET} => x86_64-w64-mingw32

${PREFIX}/${TARGET}
${PREFIX}/${TARGET}/include
${PREFIX}/${TARGET}/lib
${PREFIX}/${TARGET}/lib32
${PREFIX}/${TARGET}/lib64 [link to lib]
${PREFIX}/mingw [link to ${TARGET}]
${PREFIX}/mingw/include
${PREFIX}/mingw/lib
${PREFIX}/mingw/lib32
${PREFIX}/mingw/lib64 [link to lib]

GCC (again)

Now that we have our runtime libraries, go back and ginish the GCC build:

cd ${GCC_BUILD}
make -j4
make install

GDB

Optionally you can build GDB. To do so, invoke following:

cd ${GDB_BUILD}
${GDB_SRC}/configure \
   --target=${TARGET} \
   --prefix=${PREFIX} \
   --enable-interwork \
   --enable-multilib
make -j4 all 
make install

And that’s it. You should have them now.

Testing

The toolchain can be tested with the minimal code here, save as demo.c:

int main()
{
   return 0;
}

void exit(int code)
{
   while(1);
}

Invoke following command to compile:

x86_64-w64-mingw32-gcc demo.c -o demo.exe

In this article we will about how to build GCC ARM Toolchain, which is a cross compiler to build ARM program. The built GCC can be used to compile application into ARM’s machine codes. The toolchain then reside on /usr/local path.

For this article, I use:

  1. Slackware64 14.0
  2. GCC 4.9.2
  3. GDB 7.8.1
  4. Binutils 2.24
  5. Newlib 2.1
  6. GMP 6.0.0
  7. MPFR 3.1.2
  8. MPC 1.0.2

Deciding the Target

In ARM, there is a naming convention which explain what toolchain will do. The target alias, or target, is the target of the toolchain. It’s more than just an architecture. The tools on this toolchain will have some profix, such as: arm-none-eabi-gcc, arm-linux-eabi-gcc, etc.

This distinguish one toolchain from native host compiler and have different purpose for each name.

Basically, “arm-none-eabi” is consist of three part:

  1. architecture / processor name. Which is ARM architecture (ARM, Thumb, Thumb-2, etc)
  2. something about OS/libraries. This part is varies. Some have none and other has linux, etc. The ‘none’ means that the toolchains are not targetting specific operating system (aka ‘bare-metal’). ‘unknown’ is the same as ‘none’ while ‘linux’ designed to be used to build programs with glibc under a Linux environment, usually for built for embedded linux ARM devices.
  3. ABIs. This part describes how binary files (libraries, etc) are stored (the actual file formats), and calling conventions, register usage, etc. Some common are gnuabi, eabi, etc.

In this article, we will build arm-none-eabi to make bare metal arm compiler.

Preparation

We will need some disk space (~2GB should be enough). We also need root access to install it on “system-wide”.

Slackware64 14.0 is optional. You can use any linux distribution you like.

We will create a working directory. We will refer it as $ARMGCC, so $ARMGCC/src should really be something like ~/ARMGCC/src (the ~ means your home directory). So go ahead and create following directory structure:

# set aliases
export ARMGCC=~/ARMGCC
export TARGET=arm-none-eabi
export PREFIX=/usr/local

export BINUTILS_BUILD=${ARMGCC}/build/binutils-build
export GCC_BUILD=${ARMGCC}/build/gcc-build
export GDB_BUILD=${ARMGCC}/build/gdb-build

export BINUTILS_SRC=${ARMGCC}/src/binutils-2.24
export GCC_SRC=${ARMGCC}/src/gcc-4.9.2
export GDB_SRC=${ARMGCC}/src/gdb-7.8.1
export NEWLIB_SRC=${ARMGCC}/src/newlib-2.1.0

# make base dir, original archives dir (orig), source code dir (src), 
# and working / building dir (build) 
mkdir ${ARMGCC}{,/{orig,src,build}}

# Make build directory for each component
mkdir ${ARMGCC}/build/{binutils,gcc,gdb}-build

Acquiring the Materials

Download latest packages of GCC, GDB, binutils, and Newlib.You can download it using wget or alternatively download via browser and move it to $ARMGCC/orig. The version we will use is GCC 4.9.0, GDB 7.7, binutils 2.24, and newlib 2.1.0.

cd ${ARMGCC}/orig
wget ftp://ftp.gnu.org/pub/gnu/gcc/gcc-4.9.2/gcc-4.9.2.tar.bz2
wget ftp://ftp.gnu.org/pub/gnu/gdb/gdb-7.8.1.tar.xz
wget ftp://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.bz2
wget ftp://sources.redhat.com/pub/newlib/newlib-2.1.0.tar.gz

wget ftp://ftp.gnu.org/pub/gnu/gmp/gmp-6.0.0a.tar.xz
wget ftp://ftp.gnu.org/pub/gnu/mpc/mpc-1.0.2.tar.gz
wget ftp://ftp.gnu.org/pub/gnu/mpfr/mpfr-3.1.2.tar.gz

Then, extract them to src directory.

cd ${ARMGCC}/src

tar -jxf ${ARMGCC}/orig/gcc-4.9.2.tar.bz2
tar -Jxf ${ARMGCC}/orig/gdb-7.8.1.tar.xz
tar -jxf ${ARMGCC}/orig/binutils-2.24.tar.bz2
tar -zxf ${ARMGCC}/orig/newlib-2.1.0.tar.gz

Next we need to download some prerequisites, especially GMP (GNU Multiple Precision), MPC (Multiple-Precision Complex), and MPFR (Multiple-Precision Floating Point Reliably) which is used for computation. The prerequisites can be downloaded by invoking following command:

The MPFR, GMP, and MPC is not the latest version, but at least we can build GCC. Suppose your working directory at /home/xathrya/ARMGCC, to obtain MPDR, GMP, and MPC invoke following command:

cd ${GCC_SRC}
./contrib/download_prerequisites

However, the version of GMP, MPC, MPFR, PPL, CLOOG, and ISL is not the latest version. We can switch to the latest version by download the latest version and create symbolic link to it.

cd ${GCC_SRC}

rm {gmp,mpc,mpfr}

tar -Jxf ${ARMGCC}/orig/gmp-6.0.0a.tar.xz
tar -zxf ${ARMGCC}/orig/mpc-1.0.2.tar.gz
tar -zxf ${ARMGCC}/orig/mpfr-3.1.2.tar.gz

ln -s gmp-6.0.0 gmp
ln -s mpc-1.0.2 mpc
ln -s mpfr-3.1.2 mpfr

Building the Toolchains

Building stage is pretty simple. Honestly speaking, the tricky part is about how GCC and built concept and ordering them in correct order.

Binutils

First, built binutils. This will provide lots of useful tools in building ARM libraries and binaries (like objdump, ld, ranlib, etc). Invoke following commands:

cd ${BINUTILS_BUILD}
${BINUTILS_SRC}/configure \
   --target=${TARGET} \
   --prefix=${PREFIX} \
   --enable-interwork \
   --enable-multilib \
   --with-gnu-as \
   --with-gnu-ld \
   --disable-werror \
   --disable-nls
make configure-host
make -j4 all
make install
make clean

You will notice a few configure options that are really critical for getting things to work properly.

  • –target=arm-none-eabi Says we want a compiler to generate binaries for the arm-none-eabi platform.
  • –enable-interwork This allows for assembling Thumb and ARM code mixed into the same binaries (for those chips that support that)
  • –enable-multilib Multilib allows the use of libraries that are compiled multiple times for different targets/build types
  • –enable-shared – enable the creation of the shared libraries.

GCC

Next we will build the GCC. But please note that we are only intereset on C and C++ so we will ignore other language. To built GCC, do this:

cd ${GCC_BUILD}
${GCC_SRC}/configure \
   --target=${TARGET} \
   --prefix=${PREFIX} \
   --enable-interwork \
   --enable-multilib \
   --enable-languages="c,c++" \
   --with-newlib \
   --without-headers \
   --disable-shared \
   --with-system-zlib \
   --with-gnu-as \
   --with-gnu-ld \
   --disable-nls \
   --enable-c99 \
   --enable-long-long \
   --enable-__cxa_atexit
make -j4 all-gcc install-gcc
make clean

Now, the important points are:

  • –enable-languages=”c,c++” – means build C and C++ only.
  • –with-newlib – use Newlib instead of the standard C libraries.
  • –with-headers – this adds a header inlude path during build time and will allow the build to find the newlib header files
  • –enable-__cxa_atexit – allows use of __cxa_atexit, rather than atexit, to register C++ destructors for local statics and global objects and is essential for fully standards-compliant handling of destructors. It also affects the C++ ABI and therefore results in C++ shared libraries and C++ programs that are interoperable with other Linux distributions.
  • –enable-c99 – enable C99 support for C programs.
  • –enable-long-long – enables long long support in the compiler.

Newlib

Now build our newlib

cd ${NEWLIB_BUILD}
${NEWLIB_SRC}/configure \
  --target=${TARGET} \
  --prefix=${PREFIX} \
  --enable-interwork \
  --enable-multilib \
  --with-gnu-as \
  --with-gnu-ld \
  --disable-nls \
  --enable-target-optspace \
  --enable-newlib-reent-small \
  --enable-newlib-io-c99-formats \
  --enable-newlib-io-long-long \
  --disable-newlib-multithread \
  --disable-newlib-supplied-syscalls \
  CFLAGS_FOR_TARGET="
make -j4 all
make install
make clean

GCC (again)

Now that we have our runtime libraries, go back and ginish the GCC build:

cd ${GCC_BUILD}
make all-install

GDB

Optionally you can build GDB. To do so, invoke following:

cd ${GDB_BUILD}
${GDB_SRC}/configure \
   --target=${TARGET} \
   --prefix=${PREFIX} \
   --enable-interwork \
   --enable-multilib
make -j4 all 
make install

And that’s it. You should have them now.

Testing

The toolchain can be tested with the minimal code here:

int main()
{
   return 0;
}

void exit(int code)
{
   while(1);
}

Building GCC AVR Toolchain Cross Compiler

December 11, 2015 | Article, Labs | No Comments

In this article we will about how to build GCC AVR Toolchain, which is a cross compiler to build AVR program. The built GCC can be used to compile application into AVR’s machine codes. The toolchain then reside on /usr/local path.

For this article, I use:

  1. Slackware64 14.0
  2. GCC 4.9.0
  3. GDB 7.7
  4. Binutils 2.24
  5. AVR-libc 1.8
  6. GMP 6.0.0
  7. MPFR 3.1.2
  8. MPC 1.0.2

Preparation

We will need some disk space (~2GB should be enough). We also need root access to install it on “system-wide”.

Slackware64 14.0 is optional. You can use any linux distribution you like.

We will create a working directory. We will refer it as $AVRGCC, so $AVRGCC/src should really be something like ~/AVRGCC/src (the ~ means your home directory). So go ahead and create following directory structure:

# set aliases
export AVRGCC=~/AVRGCC
export TARGET=avr
export PREFIX=/usr/local

export BINUTILS_BUILD=${AVRGCC}/build/binutils-build
export GCC_BUILD=${AVRGCC}/build/gcc-build
export GDB_BUILD=${AVRGCC}/build/gdb-build

export BINUTILS_SRC=${AVRGCC}/src/binutils-2.24
export GCC_SRC=${AVRGCC}/src/gcc-4.9.0
export GDB_SRC=${AVRGCC}/src/gdb-7.7
export AVRLIBC_SRC=${AVRGCC}/src/avr-libc-1.8.0

# make base dir, original archives dir (orig), source code dir (src), 
# and working / building dir (build) 
mkdir ${AVRGCC}{,/{orig,src,build}}

# Make build directory for each component
mkdir ${AVRGCC}/build/{binutils,gcc,gdb}-build

Acquiring the Materials

Download latest packages of GCC, GDB, binutils, and Newlib.You can download it using wget or alternatively download via browser and move it to $AVRGCC/orig. The version we will use is GCC 4.9.0, GDB 7.7, binutils 2.24, and newlib 2.1.0.

cd ${AVRGCC}/orig
wget ftp://ftp.gnu.org/pub/gnu/gcc/gcc-4.9.0/gcc-4.9.0.tar.bz2
wget ftp://ftp.gnu.org/pub/gnu/gdb/gdb-7.7.tar.bz2
wget ftp://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.bz2
wget ftp://ftp.twaren.net/Unix/NonGNU//avr-libc/avr-libc-1.8.0.tar.bz2

wget ftp://ftp.gnu.org/pub/gnu/gmp/gmp-6.0.0a.tar.xz
wget ftp://ftp.gnu.org/pub/gnu/mpc/mpc-1.0.2.tar.gz
wget ftp://ftp.gnu.org/pub/gnu/mpfr/mpfr-3.1.2.tar.gz

Then, extract them to src directory.

cd ${AVRGCC}/src

tar -jxf ${AVRGCC}/orig/gcc-4.9.0.tar.bz2
tar -jxf ${AVRGCC}/orig/gdb-7.7.tar.bz2
tar -jxf ${AVRGCC}/orig/binutils-2.24.tar.bz2
tar -jxf ${AVRGCC}/orig/avr-libc-1.8.0.tar.bz2

Next we need to download some prerequisites, especially GMP (GNU Multiple Precision), MPC (Multiple-Precision Complex), and MPFR (Multiple-Precision Floating Point Reliably) which is used for computation. The prerequisites can be downloaded by invoking following command:

The MPFR, GMP, and MPC is not the latest version, but at least we can build GCC. Suppose your working directory at /home/xathrya/AVRGCC, to obtain MPDR, GMP, and MPC invoke following command:

cd ${GCC_SRC}
./contrib/download_prerequisites

However, the version of GMP, MPC, MPFR are not the latest version. We can switch to the latest version by link it to our downloaded one.

cd ${GCC_SRC}

rm {gmp,mpc,mpfr}

tar -Jxf ${AVRGCC}/orig/gmp-6.0.0a.tar.xz
tar -zxf ${AVRGCC}/orig/mpc-1.0.2.tar.gz
tar -zxf ${AVRGCC}/orig/mpfr-3.1.2.tar.gz

ln -s gmp-6.0.0 gmp
ln -s mpc-1.0.2 mpc
ln -s mpfr-3.1.2 mpfr

Building the Toolchains

Building stage is pretty simple. Honestly speaking, the tricky part is about how GCC and built concept and ordering them in correct order.

Binutils

First, built binutils. This will provide lots of useful tools in building ARM libraries and binaries (like objdump, ld, ranlib, etc). Invoke following commands:

cd ${BINUTILS_BUILD}
${BINUTILS_SRC}/configure --target=${TARGET} --prefix=${PREFIX} \
    --enable-interwork --enable-multilib --enable-shared 
make configure-host
make -j4 all
make install

You will notice a few configure options that are really critical for getting things to work properly.

  • –target=avr Says we want a compiler to generate binaries for the avr platform.
  • –enable-multilib Multilib allows the use of libraries that are compiled multiple times for different targets/build types
  • –enable-shared – enable the creation of the shared libraries.

GCC

Next we will build the GCC. But please note that we are only intereset on C and C++ so we will ignore other language. To built GCC, do this:

cd ${GCC_BUILD}
${GCC_SRC}/configure --target=${TARGET} --prefix=${PREFIX} --enable-interwork \
   --enable-multilib --enable-languages="c,c++" --with-dwarf2 --disable-libssp \
   --enable-c99 --enable-long-long --enable-__cxa_atexit --enable-shared
make -j4 all-gcc 
make -j4 all-target-libgcc

make install-gcc install-target-libgcc

Now, the important points are:

  • –enable-languages=”c,c++” – means build C and C++ only.
  • –with-headers – this adds a header inlude path during build time and will allow the build to find the newlib header files
  • –enable-__cxa_atexit – allows use of __cxa_atexit, rather than atexit, to register C++ destructors for local statics and global objects and is essential for fully standards-compliant handling of destructors. It also affects the C++ ABI and therefore results in C++ shared libraries and C++ programs that are interoperable with other Linux distributions.
  • –enable-c99 – enable C99 support for C programs.
  • –enable-long-long – enables long long support in the compiler.

avr-libc

Now build our avr-libc

cd ${AVRLIBC_SRC}
./configure --host=avr --build=`./config.guess` --prefix=${PREFIX}
make
make install

GDB

Optionally you can build GDB. To do so, invoke following:

cd ${GDB_BUILD}
${GDB_SRC}/configure --target=${TARGET} --prefix=${PREFIX} \
  --enable-interwork --enable-multilib
make -j4 all 
make install

And that’s it. You should have them now.

Testing

The toolchain can be tested with the minimal code here:

int main()
{
   return 0;
}

ARM Development Studio 5 is one of the development studio used for cross compiling and developing application for ARM processor. ARM DS-5 is developed by the very expert one on ARM architecture and enable engineers to develop robust and highly optimized embedded software for ARM application processors. The DS-5 toolchain comprises tools such as the best-in-class ARM C/C++ Compiler, powerful Linux/Android/RTOS-aware debugger, ARM Streamline system-wide performance analyzer, and realtime system model simulators. All conveniently packaged in a user friendly integrated development environment (IDE) based on the Eclipse.

In this article we will discuss about how to install ARM Development Studio 5 Community Edition on various operating system.

This article is written based on following:

  1. Slackware64 14.0
  2. Windows 8
  3. JDK 1.7

If you don’t want create a cross compile toolchain by yourselves, this method is suitable for you.

Requirements

ARM Development Studio 5 requires Java, JRE or JDK. You need this package to run the IDE (Integrated Development Environment), which is a modified Eclipse.

Obtain Materials

Download the ARM DS-5 by visiting this page. There are four types of installer:

  1. Windows 32-bit
  2. Windows 64-bit
  3. Linux 32-bit
  4. Linux 64-bit

Make sure you download the appropriate package for your system.

Installation

I assume you have downloaded the package specific to your platform. Follow the section which suit your platform:

Windows

Aside from the target architecture you download, you should have a zip file with .zip extension. You shall have following items:

  1. Guide e-book (ds-5_license_management_guide.pdf)
  2. ARM logo (armlogo.png)
  3. change log (changes.html)
  4. readme (readme.html)
  5. actual installation file (install_<yourplatform>.exe)

Extract the package and do the installation by running the file.

Linux

Aside from the Linux target version for the package you download, you should have a tar ball package with .tgz extension. Inside you will have following:

  1. Guide e-book (ds-5_license_management_guide.pdf)
  2. ARM logo (armlogo.png)
  3. change log (changes.html)
  4. readme (readme.html)
  5. actual installation file (install_<yourplatform>.sh)

We will refer the installation file as install.sh.

Extract the packages to any directory and make sure you are currently accessing the top directory of the package content.

Change to root account or account with root privilege.

Do installation by executing the install.sh

./install.sh

You will be asked some questions. The questions are straightforward, nothing tricky here. Install it to any location you want. For example, I choose /usr/local.

Once installed, you will have ARM Development Studio installed on /usr/local/DS-5 if you choose /usr/local. You then will be asked to add /usr/local/DS-5/bin to your PATH. Do this by editing your shell startup script (ex: .bashrc).

To start IDE, you can run /usr/local/DS-5/bin/eclipse.

You can also invoke toolchain without running the Eclipse IDE.

License and Activation Code

To use DS-5, it is recommended to activate it. As we have DS-5 Community Edition we can use it freely after activation successful.

When we start ARM Development Studio 5, we can navigate to “Help” menu. If this is your first time using DS-5, then a popup dialog will automatically ask you if you wish to open the license manager.

DS5-0

Click “Add License” and a new dialog will appear. You will be asked to enter your Activation Code. Use the license displayed on this page.

DS5-1

Next, we will be asked by Wizard to select the Host ID to lock our license to.

DS5-2

Next, we need to enter or create ARM account. If you don’t have one, you can create it now on here or following the link given in the dialog box.

DS5-3

Once complete, the license manager can be closed and our DS-5 is licensed now.

A cross compiler is a compiler that runs on one platform/architecture but generates binaries for another platform/architecture. With devices like the Raspberry Pi, where you really don’t have much CPU or memory to work with, if you’re doing any heavy compiling (like when working on the kernel) a cross compiler is the only way to go.

While there are a lot of different methods for building cross-compilers, by far the quickest and easiest is to use crosstool-ng. This is a set of scripts that bring up a menuconfig-like interface to choose your compiler settings, then goes off and downloads what it needs, patches it, configures it, builds it and installs it all for you.

For this article I use:

  1. Slackware64 14.0 (Linux)
  2. crosstool-ng

Crosstool-NG Overview

Crosstool-ng is a set of scripts that bring up a menuconfig-like interface to choose compiler settings. The project aims to build a toolchain. Crosstool-ng also support alternative components such as uClibc, glibc, or eglibc.

Insight

Our example will build a cross compiler toolchain for Raspberry Pi, which use ARMv6 and running Linux as operating system. This will assume our OS on Pi only Linux and we cannot use this toolchain to build for other OS (RISC OS, etc).

Obtain Material

Download crosstool-ng from their official site. The latest version available per August 30th 2013 is crosstool-ng 1.18. Once the download complete, extract the content.

We also need to install crosstool-ng to our system. Let’s say we want to install it to /opt/crosstool-ng

To complete above, do following:

wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.18.0.tar.bz2
tar xjf crosstool-ng-1.18.0.tar.bz2
cd crosstool-ng-1.18.0
./configure --prefix=/opt/crosstool-ng
make
make install
export PATH="${PATH}:/opt/crosstool-ng/bin"

For installation of crosstool-ng, make sure you do `make install` using root privileges.

Preparing Environment

We need a working directory for compiling our cross-compiler. This will contain toolchain configuration, downloaded files, and intermediary build results. This is not where the final location the toolchain will end up. You should reserve some space, at least 3.5GB.

Create a directory, you choose it. For example, I will use ~/working as the working directory. Go to that directory.

mkdir ~/working
cd ~/working

Compilation & Installation

Go to working directory.

Run ct-ng script to display you a configuration menu. Make sure /opt/crosstool-ng/bin is in your search path.

ct-ng menuconfig

You should see some configuration options there.

Here is the important stuff:

Path and misc options

  • Enable “Try features marked as EXPERIMENTAL”.
  • Change the prefix directory, from ${HOME}/x-tools/${CT_TARGET} to /opt/cross/x-tools/${CT_TARGET}

Target options

  • Change the “Target Architecture” to arm
  • Change the “Endiannes” to Little endian
  • Change the “Bitness” to 32-bit

Operating System

  • Change “Target OS” to Linux

Binary utilities

  • Change “binutils version” to the latest one you can find which isn’t marked as experimental (at this time 2.22)

C Compiler

  • Enable “Show Linaro versions”
  • In the gcc version field, choose the Linaro version of gcc

Once the configuration done, save it.

To build the toolchain, do following:

ct-ng build

This should take some times, minutes in my case.

Once it done, you should have cross toolchain reside on /opt/cross/x-tools/arm-unknown-linux-gnueabi/bin. You might want to add this to your search path.

Testing

First, test the version

arm-unknown-linux-gnueabi-gcc --version

Now test for the compilation. Write this source code as test.c.

#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}

and compile it.

arm-unknown-linux-gnueabi-gcc -o test test.c

Building GCC ARM Toolchain on Slackware64

December 5, 2015 | Article | No Comments

ARM, is one of popular processor architecture on market. ARM is a family of RISC-based computer processor designed and licensed by British company ARM Holdings. First developed in the 1980s. This processors are the major choice for embedded systems such as smartphones, hard disk drives, digital television, microcontrollers, mobile computer, etc.

We know that x86 (and also x86_64) is different to ARM. Therefore, we can not use famous GCC for x86 or x86_64 compiler to compile application for ARM. We need compiler who have the capability to do so. Fortunately, with proper set up GCC can be used to build application for ARM.

In this article we will about how to build GCC ARM Toolchain for ARM architecture. The built GCC can be used to compile application into ARM’s machine codes. The toolchain then reside on /usr/local path.

For this article, I use:

  1. Slackware64 14.0
  2. GCC
  3. Binutils
  4. newlib
  5. MPFR
  6. GMP
  7. MPC

Acquiring the Materials

Before we proceed to main phase, make sure you have at least (2) to (6). Slackware64 14.0 is optional. You can use any linux distribution you like.

Download latest binutils and GCC version. At the time of writing this article, the latest version of binutils is 2.23.1 which you can obtain at here. The latest GCC is 4.7.2 which you can obtain here. Extract them into a working directory, let say /home/xathrya/ARMGCC. You will get binutils-2.23.1 and gcc-4.7.2 directory. And then make two new directory, binutils-obj and gcc-obj. The commands for that (adjust if you have other than .tar.bz2):

cd ~/ARMGCC
tar -Jxf binutils-2.23.0.tar.bz2
tar -Jxf gcc-4.7.2 .tar.bz2
mkdir binutils-obj gcc-obj

Next is obtaining newlib. Newlib is an alternative to GLIBC. At time of writing this article, the latest newlib is 1.20.0 which can be downloaded here. Again, extract the content from archive and create a folder newlib-obj.

cd ~/ARMGCC
tar -xf newlib-1.20.0.tar.gz
mkdir newlib-obj

The MPFR, GMP, and MPC is not the latest version, but at least we can build GCC. Supposing your working directory at /home/xathrya/ARMGCC, to obtain MPDR, GMP, and MPC invoke following command:

cd ~/ARMGCC/gcc-4.7.2
./contrib/download_prerequisites

You then will gave three more directories on ~/ARMGCC/gcc-4.7.2. Rename them with eliminating their version number. Thus you will get three directories: mpfr, gmp, mpc.

Optionally you can build GDB. Download the latest version (at this time is 7.5) at here. Extrace the archive and make sure it is on working directory.

cd ~/ARMGCC
tar -jxf gdb-7.5.tar.bz2
mkdir gdb-obj

Deciding the Target

In ARM, there is a naming convention which explain what toolchain will do. The target alias, or target, is the target of the toolchain. It’s more than just an architecture. The tools on this toolchain will have some profix, such as: arm-none-eabi-gcc, arm-linux-eabi-gcc, etc.

This distinguish one toolchain from native host compiler and have different purpose for each name.

Basically, “arm-none-eabi” is consist of three part:

  1. architecture / processor name. Which is ARM architecture (ARM, Thumb, Thumb-2, etc)
  2. something about OS/libraries. This part is varies. Some have none and other has linux, etc. The ‘none’ means that the toolchains are not targetting specific operating system (aka ‘bare-metal’). ‘unknown’ is the same as ‘none’ while ‘linux’ designed to be used to build programs with glibc under a Linux environment, usually for built for embedded linux ARM devices.
  3. ABIs. This part describes how binary files (libraries, etc) are stored (the actual file formats), and calling conventions, register usage, etc. Some common are gnuabi, eabi, etc.

In this article, we will build both arm-none-eabi and arm-linux-eabi.

Building the Toolchains

Building stage is pretty simple. Honestly speaking, the tricky part is about how GCC and built concept and ordering them in correct order.

First, built binutils. This will provide lots of useful tools in building ARM libraries and binaries (like objdump, ld, ranlib, etc). Invoke following commands:

cd ~/ARMGCC/binutils-obj
../binutils-2.23.1/configure --target=arm-none-eabi --prefix=/usr/local --enable-interwork --enable multilib
make all install

You will notice a few configure options that are really critical for getting things to work properly.

  • –target=arm-none-eabi Says we want a compiler to generate binaries for the arm-none-eabi platform. You can change to arm-linux-eabi if you want to build arm-linux-eabi.
  • –enable-interwork This allows for assembling Thumb and ARM code mixed into the same binaries (for those chips that support that)
  • –enable-multilib Multilib allows the use of libraries that are compiled multiple times for different targets/build types (see
  • –prefix=/usr/local, our path for installed directory

Next we will build the GCC. But please note that we are only intereset on C and C++ so we will ignore other language. To built GCC, do this:

cd ~/ARMGCC/gcc-obj
../gcc-4.7.2/configure --target=arm-none-eabi --prefix=/usr/local --enable-interwork --enable multilib --enable-languages="c,c++" \
   --with-new-lib --with-headers=../newlib-1.20.0/newlib/libc/include
make all-gcc install-gcc

If you want to build arm-linux-eabi, do this one instead (you should see the difference):

cd ~/ARMGCC/gcc-obj
../gcc-4.7.2/configure --target=arm-none-eabi --prefix=/usr/local --enable-interwork --enable multilib --enable-languages="c,c++"
make all-gcc install-gcc

Now, the important points are:

  • –enable-languages=”c,c++” Only build C and C++.
  • –with-newlib Use Newlib instead of the standard C libraries.
  • –with-headers This adds a header inlude path during build time and will allow the build to find the newlib header files

Now build Newlib using our new cross-compiler

 cd ~/ARMGCC/newlib-obj
 ../newlib-1.20.0/configure --target=arm-none-eabi --prefix=/usr/local --enable-interwork --enable multilib
 make all install

If you build one for arm-linux-eabi, you don’t need to build the newlib.

Optionally you can build GDB. To do so, invoke following:

cd gdb-obj
../gdb-7.5/configure --target=arm-none-eabi --prefix=/usr/local --enable-interwork --enable-multilib
make all install

And that’s it. You should have them now.

Installing Intel Compiler for Linux

December 5, 2015 | Article | No Comments

Using Linux? Often compile application from source? Then you must be familiar with gcc. GCC is an official compiler used by GNU/Linux. But beside gcc there are many alternatives for compile C code in Linux. One of them is Intel’s Compiler.

Why use Intel Compiler? There are many reason, but for me there are two main reasons:

  1. I want to know about Intel’s compiler
  2. Intel made my processor, so why don’t I use compiler made by them?

How to obtain?

Officially Intel Compiler is not free. You must pay to install and use. But there is a choice you can afford. Intel offer Non-commercial version of Intel Compiler. But there is only one condition: you use it for only non-commercial purpose. In other means you are not allowed to use this compiler to produce application and then sell it. You are also not allowed to received any payment regarding your application compiled by this compiler. You can find out more at their site. If you agree to this then you can proceed.

Go to this page: http://software.intel.com/en-us/non-commercial-software-development

I recommend you to pick Intel Parallel Studio XE (at this time I write article is version 2013). Follow the instruction. You will have to fill form. Make sure you write correct email. Intel then will email you for next instruction. You will be given a license key for this non-commercial version and also a download link.

How to Install?

Now extract the downloaded package (assuming the package is parallel_studio_xe_2013_update1_intel64.tgz). Then you will have a folder with similar name. Install it by invoking the script provided at that directory. For a clear explanation, you can see this commands:

tar -xvf parallel_studio_xe_2013_update1_intel64.tgz
cd parallel_studio_xe_2013_update1_intel64
./install.sh

Make sure you have acquired root privileges. Follow the instruction, it is self explained :D. Some linux distro is not support officially, such as Slackware. If so, the installer will complain about unsupported OS. But don’t worry, we can still proceed.

Once you are prompted by activation question, you can enter the license key you obtain from intel. If you do offline installation, you will be prompted again about remote registration and you will be given url and unlock code. Go to that url and enter the given code (the code is different by first license key). You will then given a new code. Copy it and paste it to the installer.

Wait for installation to complete.

Setting the Environment

Before using the compiler, we have to do some setting. Assuming installation on /opt/intel/

Using Intel(R) VTune(TM) Amplifier XE 2013 Update 2:

source /opt/intel/vtune_amplifier_xe_2013/amplxe-vars.sh

There are two ways to use VTune Amplifier XE 2013: GUI and CLI. To use GUI do amplxe-gui, while using CLI do amplxe-cl. The documentation can be found at /opt/intel/vtune_amplifier_xe_2013/documentation/en/welcomepage/get_started.html.

Using Intel(R) Inspector XE 2013 Update 2:

source /opt/intel/inspector_xe_2013/amplxe-vars.sh

There are two ways to use Inspector XE 2013: GUI and CLI. To use GUI do inspxe-gui, while using CLI do inspxe-cl. The documentation can be found at /opt/intel/inspector_xe_2013/documentation/en/welcomepage/get_started.html.

Using Intel(R) Advisor XE 2013 Update 1:

source /opt/intel/advisor_xe_2013/advixe-vars.sh

There are two ways to use Advisor XE 2013: GUI and CLI. To use GUI do advixe-gui, while using CLI do advixe-cl. The documentation can be found at /opt/intel/Advisor_xe_2013/documentation/en/welcomepage/get_started.html.

Using Intel(R) Composer XE 2013 Update 1 for Linux*:

If you run 32 bit platform, replace “intel64” with ia32.

source /opt/intel/bin/compilervars.sh intel64

Now, to invoke compiler from command prompt, issue:
For C++: icpc
For C: icc
For Fortran: ifort

Happy hacking 😀

Social media & sharing icons powered by UltimatelySocial