Before we even start writing “Hello world”, we need a C++ compiler. And no, the g++
you have will not work.
The reason for this is that your linux provided compiler has been specifically built to work on linux, use linux system headers and produce linux binaries.
What we need is a compiler that can generate “generic x86_64” binaries with no expectations of where they are going to run and what they’ll find there (absolute nothingness).
While you might technically use the default compiler you would run in trouble pretty quickly, so let’s just avoid it altogether and do it right.
Building the cross compiler
The strictly minimum tools we need are g++ and ld. We find these in the GNU gcc and binutils packages.
GCC in turn depends on some other libraries: isl, gmp, mpc, mpfr.
Since the steps to build all of these are a bit tedious, and repetitive (if you want to stay up to date), I wrote a script that takes care of everything, from dependencies, to downloading, compiling and installing.
It is now available in the repository at https://github.com/francescobbo/happy/tree/main/scripts.
To follow the code, note that the rough steps are:
- establishing the environment (currently debian or macOS)
- downloading source packages from the GNU FTP repositories
- set the
TARGET
tox86_64-elf
(this is where we say “no linux here”) - extract, compile and install
It will install everything under /opt/cross/x86_64-elf/bin, which you are
encouraged to add to your PATH
.