user2485710
user2485710

Reputation: 9811

Can I build gcc for ARM with an X64 one?

I need a definitive answer because this is still unclear to me and I can't find an explicit answer anywhere in the docs.

Assuming that I have a working gcc toolchain where

host    x86_64-linux-gnu
target  x86_64-linux-gnu

, the question is can I possibly configure and build gcc from sources with ?

host    x86_64-linux-gnu
build   x86_64-linux-gnu
target  arm-gnu-eabi

The reason why I would like an answer on this is about whether or not I should spend time trying different configurations for my libraries and whether or whether or not the scripts used to build gcc are capable of some implicit stage 1 build that can potentially bootstrap an ARM compiler for me temporarily on this x64, so I can generate the toolchain that I need for the target that I want .

Upvotes: 2

Views: 5221

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

"Can I build gcc for ARM with an X64 one?"

Yes, you can. I have described this process for a suse linux host development system in a blog post of mine.

================================================================================== I'm going to replicate the steps here:

1. Ensure to have the necessary headers & libraries installed

I have used YAST's 'Install Software' feature, to install the following packages that will be necessary to complete all the build steps (just search for the package names, select and accept):


  • gmp-devel
  • mpfr-devel
  • mpc-devel
  • texinfo
  • ncurses-devel
  • termcap

2. Create a directory skeleton


cd ~
mkdir arm-none-eabi arm-none-eabi-src
cd arm-none-eabi
mkdir src build
cd ~/arm-none-eabi-src
mkdir src build

3. Download the the source packages and extract them

I'm using gcc-4.7.1 here, but the same process will of course apply for newer versions of GCC.


cd ~/arm-none-eabi-src/src

wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.7.1/gcc-4.7.1.tar.bz2
wget ftp://ftp.gnu.org/gnu/binutils/binutils-2.22.tar.bz2
wget ftp://ftp.gnu.org/gnu/gdb/gdb-7.4.tar.bz2
wget ftp://sources.redhat.com/pub/newlib/newlib-1.20.0.tar.gz

tar -xf gcc-4.7.1.tar.bz2
tar -xf binutils-2.22.tar.bz2
tar -xf gdb-7.4.tar.bz2
tar -xf newlib-1.20.0.tar.gz

4. Build the binutils


cd ~/arm-none-eabi-src/build
mkdir binutils-2.22
cd binutils-2.22
../../src/binutils-2.22/configure \
  --target=arm-none-eabi \
  --prefix=$HOME/arm-none-eabi \
  --with-cpu=cortex-m3 \
  --with-no-thumb-interwork \
  --with-mode=thumb
make all install
export PATH="$PATH:$HOME/arm-none-eabi/bin"

5. Build GCC (Part1)


cd ~/arm-none-eabi-src/build
mkdir gcc-4.7.1
cd gcc-4.7.1
../../src/gcc-4.7.1/configure --target=arm-none-eabi \
  --prefix=$HOME/arm-none-eabi --with-cpu=cortex-m3 \
  --with-mode=thumb --disable-multilib \
  --with-no-thumb-interwork \
  --enable-languages="c,c++" --with-newlib \
  --with-headers=../../src/newlib-1.20.0/newlib/libc/include
make all-gcc install-gcc

The --enable-cxx-flags configure option might be additionally used to control the build flags of the libstdc++ (included in this step):

--enable-cxx-flags='-fno-exceptions \
    -ffunction-sections -fno-omit-frame-pointer'

In general the same C++ compile flags should be used as they'll appear when building the intended target code.


6. Build GCC newlib with the cross compiler (Part2)


cd ~/arm-none-eabi-src/build
mkdir newlib-1.20.0
cd newlib-1.20.0
../../src/newlib-1.20.0/configure --target=arm-none-eabi \
  --prefix=$HOME/arm-none-eabi --disable-multilib \
  --disable-newlib-supplied-syscalls
make all install

A note about the --disable-newlib-supplied-syscalls option:
Disabling the default newlib syscall stub implementation is generally a good idea when you intend to compile for targets without using a linux like operating system, or no OS at all. It will leave you with linker errors on unimplemented stub functions you'll need to provide for newlib. Removing the option will still enable you to override the newlib provided stubs with your own implementations.

Though, when you plan to use the cross-toolchain in conjunction with CMake, you should omit this option. CMake does some basic tests using the specified compiler definitions (e.g. from a toolchain.cmake file), that'll fail without the default stub implementations supplied.


7. Complete installing GCC


cd ~/arm-none-eabi-src/build/gcc-4.7.1
make all install

8. Build GDB


cd ~/arm-none-eabi-src/build
mkdir gdb-7.4
cd gdb-7.4
../../src/gdb-7.4/configure --target=arm-none-eabi \
  --prefix=$HOME/arm-none-eabi
make all install

UPDATE
The same works pretty well for GCC 4.8.2 also.

Upvotes: 3

Related Questions