2

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 .

user2485710
  • 9,451
  • 13
  • 58
  • 102
  • Does [this](http://dev-jungle.blogspot.de/2013/09/building-gcc-471-arm-cross-toolchain-on.html) help? – πάντα ῥεῖ Oct 10 '14 at 18:28
  • 1
    Are you talking about anything special besides a normal [cross compiler](https://gcc.gnu.org/wiki/Building_Cross_Toolchains_with_gcc) ? There's myriads of ways to build one, though it may be easier with the assistance of a helper tool such as [crosstool-ng](http://crosstool-ng.org/) – nos Oct 10 '14 at 18:29
  • @nos when there are a lot of ways to achieve the same goal and there isn't a centralized source of documentation: it's a problem; hence my confusion . I know crosstool-ng, I would like to do everything on my own given my necessities . – user2485710 Oct 10 '14 at 18:30
  • @πάνταῥεῖ looks like that guide is assuming an already working compiler for the given target, especially when you build `binutils` before `gcc`, aside from `gcc`, without an in tree build . No it's not helping . – user2485710 Oct 10 '14 at 18:40
  • @user2485710 _"looks like that guide is assuming an already working compiler for the given target ..."_ Nah, the cross-compiler is build first, `binutils` have to be build separate, and don't affect build of the cross compiler anyways. I use this stuff in production (for different target variants), and it works very well. – πάντα ῥεῖ Oct 10 '14 at 18:45
  • @πάνταῥεῖ I don't understand, in that guide building binutils is point 3, and point 1 and 2 are about getting and unpacking the sources, when you are supposed to build the cross compiler ? Can you expand on that ? – user2485710 Oct 10 '14 at 18:49
  • @user2485710 ***4. Build GCC (Part1)***, ***5. Build GCC newlib with the cross compiler (Part2)*** – πάντα ῥεῖ Oct 10 '14 at 18:50
  • @πάνταῥεῖ I'm still scratching my head ..., So you start with a gcc toolchain for 64/32 x86 and you can produce a toolchain targeting ARM following those instructions ? You are using a toolchain for x86; correct ? – user2485710 Oct 10 '14 at 18:55
  • @user2485710 _"You are using a toolchain for x86; correct "_, as the starting point to get the cross compiler, and the binutils compiled for this target definition, yes. The x86 gcc is already available on my development environment. What's your host system actually, which (x86) GCC version is installed? – πάντα ῥεῖ Oct 10 '14 at 19:02
  • @πάνταῥεῖ sorry for the delay, I had to leave for 5 minutes, I have both 4.9.1 and 4.8.2 supporting the m64/m32/mx32 abis for i686 and amd64 – user2485710 Oct 10 '14 at 19:21
  • I also recommend having a look at buildroot or crosstool-NG: https://stackoverflow.com/questions/10412684/how-to-compile-my-own-glibc-c-standard-library-from-source-and-use-it/52454710#52454710 which automate everything for you (if they support a given GCC version). – Ciro Santilli OurBigBook.com Jul 15 '20 at 14:14

1 Answers1

3

"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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • question: is this supposed to work with an in tree build with the gcc sources beside the binutils ones ? Because I already tried something along this lines and I know for a fact that doesn't work, I don't know if I'm doing something wrong or I just have to build binutils out of the tree in a separate step . – user2485710 Oct 10 '14 at 19:29
  • @user2485710 _"... or I just have to build binutils out of the tree in a separate step.'_ Well, my solution proposes to build and install binutils in a separate step, **before** the cross compiler is build. I don't know if this really matters, but I doubt the in/out of tree argument has something to do with it. – πάντα ῥεῖ Oct 10 '14 at 19:35
  • with my previous steps I always got an error when building libgcc, which is at the stage 2 phase if I recall correctly, which means I probably got the majority of my flags right but the ones about architectures and libraries can be wrong . I think that `with-cpu` might be a game changer, which is probably implicit on x64 . I just finished compiling binutils for ARM following your guidance, I think that this out of tree build does makes a difference . – user2485710 Oct 10 '14 at 19:40
  • @user2485710 _"I think that this out of tree build does makes a difference"_ I'm always finding these terms _out of / in tree build_ a bit whacky. I'm not so sure what these should mean. All what I know about is, I have a source directory where I'm extracting to, and I have a build directory, where I'm going to build the stuff, and take the artifacts as source, for whereever I want to install it. – πάντα ῥεῖ Oct 10 '14 at 19:47
  • the thing is that I have problems with in tree builds ( gcc + binutils + the other libs, with all the sources in the same dir or symlinked in the same dir ) only when switching to ARM targets, probably ARM builds are more picky and need a more precise configuration to work, for example I was using `--with-arch=`, which works totally fine for x86-like targets, but apparently it's not enough for other targets, it's weird, there are some quirks of the gcc build system that I still need to grasp . – user2485710 Oct 10 '14 at 19:58
  • Does this work on Debian? What shall I use to instead of `newlib` on Debian? – JustWe Apr 22 '19 at 08:55
  • @Jiu Can't tell. I did that on a SuSe installation that time. – πάντα ῥεῖ Apr 22 '19 at 08:57
  • I have an error doing the 6th step : Makefile:1031: recipe for target 'libc.a' failed make[5]: *** [libc.a] Error 1 – Dan M. CISSOKHO Oct 20 '20 at 06:47