Skip to main content

Cross Compilation

A cross-compiler is a compilation tool that runs on a host platform (e.g., Ubuntu or a virtual machine) but generates executable files for a target platform (e.g., ARM, MIPS).Since development boards have limited resources, Buildroot does not support local compilation of C programs by default, so a cross-compilation toolchain is required. The cross-compiler is located in the directory:<SDK directory>/prebuilts/gcc/linux-x86/arm/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/bin.The following guide explains how to configure the cross-compiler to compile C programs.

1. Setting Up the Cross-Compilation Toolchain

  1. Set the environment variable for the cross-compilation tool. To make this permanent, edit the configuration file (e.g., .bashrc or .profile):

    vim ~/.bashrc 
  2. Set the environment variable for the cross-compilation tool. To make this permanent, edit the configuration file (e.g., .bashrc or .profile):

    export PATH=/home/ubuntu/luckfox-lyra-sdk/prebuilts/gcc/linux-x86/arm/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/bin:$PATH
    • Note: Replace the SDK path as per your setup.
  3. Update the environment variables to apply the changes:

    source ~/.bashrc  
  4. Compile a program using the cross-compiler:

    arm-none-linux-gnueabihf-gcc gpio.c -o gpio
  5. Transfer the executable file to the development board:

    scp gpio root@192.168.10.103:/root
    • Note: Replace the IP address with your actual target's IP.

2. CMake

  1. Create a CMakeLists.txt file:

    cmake_minimum_required(VERSION 3.10)

    project(GpioProject)

    set(CMAKE_C_COMPILER /home/ubuntu/luckfox-lyra-sdk/prebuilts/gcc/linux-x86/arm/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc)

    add_executable(gpio gpio.c)
  2. Create a build folder and navigate into it:

    mkdir build && cd build
  3. Compile the program:

    cmake ..
    make