To get an idea of what is in a typical toolchain, I want to examine the crosstool-NG toolchain you have just created.
The toolchain is in the directory ~/x-tools/arm-cortex_a8-linux-gnueabihf/bin. In there you will find the cross compiler, arm-cortex_a8-linux-gnueabihf-gcc. To make use of it, you need to add the directory to your path using the following command:
You can confirm that it has been cross compiled by using the file command to print the type of the file:
$ file helloworldhelloworld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.15.4, not stripped
Finding out about your cross compiler
Imagine that you have just received a toolchain, and that you would like to know more about how it was configured. You can find out a lot by querying gcc. For example, to find the version, you use --version:
$ arm-cortex_a8-linux-gnueabi-gcc --versionarm-cortex_a8-linux-gnueabi-gcc (crosstool-NG 1.20.0) 4.9.1Copyright (C) 2014 Free Software Foundation, Inc.This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
There is a lot of output there but the interesting things to note are:
--with-sysroot=/home/chris/x-tools/arm-cortex_a8-linux-gnueabihf/arm-cortex_a8-linux-gnueabihf/sysroot: This is the default sysroot directory, see the following section for an explanation
--enable-languages=c,c++: Using this we have both C and C++ languages enabled
--with-arch=armv7-a: The code is generated using the ARM v7a instruction set
--with-cpu=cortex-a8 and --with-tune=cortex-a8: The the code is further tweaked for a Cortex A8 core
--with-float=hard: Generate opcodes for the floating point unit and uses the VFP registers for parameters
--enable-threads=posix: Enable POSIX threads
These are the default settings for the compiler. You can override most of them on the gcc command line so, for example, if you want to compile for a different CPU, you can override the configured setting, –-with-cpu, by adding -mcpu to the command line, as follows:
You can print out the the range of architecture-specific options available using --target-help, as follows:
$ arm-cortex_a8-linux-gnueabihf-gcc --target-help
You may be wondering if it matters whether or not you get the exact configuration right at the time you generate the toolchain if you can change it later on, and the answer depends on the way you anticipate using it. If you plan to create a new toolchain for each target, then it makes sense to set everything up at the beginning because it will reduce the risks of getting it wrong later on. Jumping ahead a little to Chapter 6, Selecting a Build System, I call this the Buildroot philosophy. If, on the other hand, you want to build a toolchain that is generic and you are prepared to provide the correct settings when you build for a particular target, then you should make the base toolchain generic, which is the way the Yocto Project handles things. The preceding examples follow the Buildroot philosophy.
The sysroot, library, and header files
The toolchain sysroot is a directory which contains subdirectories for libraries, header files, and other configuration files. It can be set when the toolchain is configured through --with-sysroot= or it can be set on the command line, using --sysroot=. You can see the location of the default sysroot by using -print-sysroot:
lib: Contains the shared objects for the C library and the dynamic linker/loader, ld-linux
usr/lib: the static library archives for the C library and any other libraries that may be installed subsequently
usr/include: Contains the headers for all the libraries
usr/bin: Contains the utility programs that run on the target, such as the ldd command
/usr/share: Used for localization and internationalization
sbin: Provides the ldconfig utility, used to optimize library loading paths
Plainly, some of these are needed on the development host to compile programs, and others – for example the shared libraries and ld-linux – are needed on the target at runtime.