Skip to main content

Cross Compiler

A cross compiler is a compilation tool that runs on a host platform (such as Ubuntu or a virtual machine) but generates executable files for a target platform (such as ARM or MIPS architectures). Since development boards have limited resources, Buildroot does not support compiling C programs locally by default, and a cross-compilation toolchain must be used.

Because the Ubuntu and Buildroot root file systems on the development board are based on different toolchains, users need to select the corresponding cross compiler on a Ubuntu 22.04 PC to ensure that the compiled programs run correctly on the target system.

1. Preparation Work

  1. Obtain the cross-compilation toolchain:

    VersionDescriptionDownload
    glibcThe cross-compilation toolchain for Ubuntu system needs to be obtained from ARM's official website corresponding to the cross-compilerDownload
    uclibcThe cross-compilation toolchain for Buildroot system can be obtained in the SDKDownload
  2. Unzip the corresponding compressed package:

    • Ubuntu Execute the command:

      #-C ~/ specifies the unzip path as the user's home directory
      tar zxvf gcc-arm-11.2-2022.02-x86_64-arm-none-linux-gnueabihf.tar.gz -C ~/
    • Buildroot Execute the command:

      #-C ~/ specifies the unzip path as the user's home directory
      tar zxvf arm-rockchip830-linux-uclibcgnueabihf.tar.gz -C ~/
  3. Take the program hello.c as an example to compile the corresponding executable file. The file execution effect is to print hello world on the terminal. The hello.c code is as follows:

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    printf("hello world\n");
    return 0;
    }

2. Compile to Generate Executable Files for Buildroot System

[NOTE]

Choose one of Method One/Method Two to generate the corresponding files.

Method One: Modify Environment Variables

  1. Set the environment variable for the cross-compilation tool. If you need it to take effect permanently, edit the configuration file (e.g., .bashrc or .profile).

    vim ~/.bashrc
  2. Add the toolchain path, add the path of the cross-compilation tool to the system's PATH environment variable:

    export PATH=/home/buildroot/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin:$PATH
    • Note: Please fill in according to your own SDK path
  3. Update the environment variable, execute the following command to make the configuration take effect.

    source ~/.bashrc
  4. Use the cross-compilation tool to compile the program.

    arm-rockchip830-linux-uclibcgnueabihf-gcc hello.c -o hello
  5. Finally, transfer the executable file to the development board.

    scp hello root@192.168.9.128:/root
    • Note: Please fill in according to the actual IP address of your development board

Method Two: Using Makefile

  1. Create and edit the Makefile.

    vi Makefile
    • Note: This Makefile needs to be created in the same directory as the hello.c file
  2. Write the Makefile.

    #Luckfox-pico-ultra-w-buildroot
    CC := /home/buildroot/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin/arm-rockchip830-linux-uclibcgnueabihf-gcc

    hello:hello.c
    -$(CC) $^ -o $@
    • Note: Please fill in according to your own SDK path
  3. Execute the make command, and an executable file will be generated in the current directory.

    make
  4. Finally, transfer the executable file to the development board.

    scp hello root@192.168.9.128:/root
    • Note: Please fill in according to the actual IP address of your development board

3. Compile to Generate Executable Files for Ubuntu System

Note:Choose one of Method One/Method Two to generate the corresponding files.

Method One: Modify Environment Variables

  1. Set the environment variable for the cross-compilation tool. If you need it to take effect permanently, edit the configuration file (e.g., .bashrc or .profile).

    vim ~/.bashrc
  2. Add the toolchain path, add the path of the cross-compilation tool to the system's PATH environment variable:

    export PATH=/home/ubuntu/luckfox-pico/gcc-arm-11.2-2022.02-x86_64-arm-none-linux-gnueabihf/bin:$PATH
    • Note: Please fill in according to your own SDK path
  3. Update the environment variable, execute the following command to make the configuration take effect.

    source ~/.bashrc
  4. Use the cross-compilation tool to compile the program.

    arm-none-linux-gnueabihf-gcc hello.c -o hello
  5. Finally, transfer the executable file to the development board.

    scp hello pico@192.168.9.128:/home/pico
    • Note: Please fill in according to the actual IP address of your development board

Method Two: Using Makefile

  1. Create and edit the Makefile.

    vi Makefile
    • Note: This Makefile needs to be created in the same directory as the hello.c file
  2. Write the Makefile.

    #Luckfox-pico-ultra-w-ubuntu
    CC := /home/ubuntu/luckfox-pico/gcc-arm-11.2-2022.02-x86_64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc

    hello:hello.c
    -$(CC) $^ -o $@
    • Note: Please fill in according to your own SDK path
  3. Execute the make command, and an executable file will be generated in the current directory.

    make
  4. Finally, transfer the executable file to the development board.

    scp hello pico@192.168.9.128:/home/pico
    • Note: Please fill in according to the actual IP address of your development board

4. Run the Executable File

  • Execution in Buildroot:
    xxx
  • Execution in Ubuntu:
    xxx