Buildroot-ts Cross Compiler: Difference between revisions

From embeddedTS Manuals
(Clean up)
(Easier use)
Line 1: Line 1:
Buildroot normally bootstraps itself by first building a cross-compiler that is used to build all of the target packages. This is Buildroot's "internal toolchain backend." Creating a generic, relocatable, cross-compiler for a Linux workstation and targeting a specific platform is possible with Buildroot but requires some extra steps since this is not one of the goals of Buildroot.
In order to generate a cross-compiler from Buildroot, first configure the target build as outlined in [[#Buildroot - Building|the first steps of the build instructions]]. Once configured, a separate <source inline>make</source> command can be issued to generate a tarball package of the cross-compiler. This can be unpacked to any location on the host Linux workstation's filesystem and then used to cross-compile additional applications for the target. The build, setup, and use of the cross-compiler can be done with the following steps:
<source lang=bash>
# Be sure the target is configured first!
# The following command will output the cross-compiler package as well as build the target image completely if not built already
make sdk


The Buildroot methodology for generating a generic and relocatable cross-compiler first builds a compiler to target the platform. Then, this cross-compiler is configured as an external toolchain in the platform configuration. The result of this is Buildroot being able to use its own prebuilt compiler to build every target application and generate the final image. With the same compiler able to be used externally from Buildroot to cross-compile any other applications for the target later on.
# Unpack the tarball to new directory in the users home directory
# Note that the tarball name may be slightly different depending on how the toolchain is configured in Buildroot
mkdir ~/buildroot-toolchain
tar xf buildroot/output/images/arm-buildroot-linux-gnueabihf_sdk-buildroot.tar.gz -C ~/buildroot-toolchain/


See the [https://buildroot.org/downloads/manual/manual.html#build-toolchain-with-buildroot Buildroot manual] for how to create an external toolchain with Buildroot for general use. We recommend following our [[#Buildroot - Building|configuration instructions]] for building Buildroot, making the necessary modifications to build the external toolchain, and then re-configuring Buildroot to build for the specified target using the previously built cross-compiler.
# Update the path information for the toolchain (must be done when the tarball is unpacked, or if the root folder of the toolchain is moved!)
~/buildroot-toolchain/arm-buildroot-linux-gnueabihf_sdk-buildroot/relocate-sdk.sh
 
# Create a simple Hello World application source
cat << EOF > hello.c
#include <stdio.h>
void main(void) { printf("Hello!\n"); }
EOF
 
# Build a binary from the Hello World source that can be run on the target device
~/buildroot-toolchain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin/arm-linux-gcc hello.c -o hello
</source>
 
The <source inline>hello</source> binary can then be copied to the target device and executed on it.
 
Note that the <source inline>make sdk</source> command can be run at any time to generate the toolchain tarball. Even after Buildroot has generated the output image.
 
 
Buildroot is extremely flexible in its generation and use of a cross-compiler. See the Buildroot manual for more information on [https://buildroot.org/downloads/manual/manual.html#_advanced_usage advanced use of the Buildroot generated toolchain] as well as [https://buildroot.org/downloads/manual/manual.html#build-toolchain-with-buildroot using Buildroot's generated cross-compiler as an external compiler for Buildroot].

Revision as of 13:16, 5 April 2023

In order to generate a cross-compiler from Buildroot, first configure the target build as outlined in the first steps of the build instructions. Once configured, a separate make command can be issued to generate a tarball package of the cross-compiler. This can be unpacked to any location on the host Linux workstation's filesystem and then used to cross-compile additional applications for the target. The build, setup, and use of the cross-compiler can be done with the following steps:

# Be sure the target is configured first!
# The following command will output the cross-compiler package as well as build the target image completely if not built already
make sdk

# Unpack the tarball to new directory in the users home directory
# Note that the tarball name may be slightly different depending on how the toolchain is configured in Buildroot
mkdir ~/buildroot-toolchain
tar xf buildroot/output/images/arm-buildroot-linux-gnueabihf_sdk-buildroot.tar.gz -C ~/buildroot-toolchain/

# Update the path information for the toolchain (must be done when the tarball is unpacked, or if the root folder of the toolchain is moved!)
~/buildroot-toolchain/arm-buildroot-linux-gnueabihf_sdk-buildroot/relocate-sdk.sh

# Create a simple Hello World application source
cat << EOF > hello.c
#include <stdio.h>
void main(void) { printf("Hello!\n"); }
EOF

# Build a binary from the Hello World source that can be run on the target device
~/buildroot-toolchain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin/arm-linux-gcc hello.c -o hello

The hello binary can then be copied to the target device and executed on it.

Note that the make sdk command can be run at any time to generate the toolchain tarball. Even after Buildroot has generated the output image.


Buildroot is extremely flexible in its generation and use of a cross-compiler. See the Buildroot manual for more information on advanced use of the Buildroot generated toolchain as well as using Buildroot's generated cross-compiler as an external compiler for Buildroot.