在Ubuntu中配置适配泰山派的交叉编译环境

1 完整代码

c 复制代码
#include <stdio.h>

int main()
{
    printf("Hello, TaiShan-Pi (RK3566)!\n");
#ifdef __aarch64__
    printf("I was compiled for ARM64.\n");
#else
    printf("I was NOT compiled for ARM64.\n");
#endif
    return 0;
}

2 编译程序

在 Ubuntu 中可以执行以下命令编译、执行:

bash 复制代码
$ gcc hello.c -o hello
$ ./hello
Hello, TaiShan-Pi (RK3566)!
I was NOT compiled for ARM64.

上述命令编译得到的可执行程序 hello 可以在 Ubuntu 中运行,但是如果把它放到泰山派上去,它是无法执行的。因为它是使用 gcc 编译的,是给 PC 机编译的,里面的机器指令是 x86 的。

bash 复制代码
$ file hello
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, ..., not stripped

要想给泰山派编译出 hello 程序,需要使用对应的交叉编译工具链。

3 交叉编译

3.1 安装交叉编译工具

(1)在线安装

bash 复制代码
sudo apt update
sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

安装完成之后可以输入以下命令验证gcc、g++的安装是否成功。

gcc-aarch64-linux-gnu:

bash 复制代码
$ aarch64-linux-gnu-gcc --version
aarch64-linux-gnu-gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 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.

g+±aarch64-linux-gnu:

bash 复制代码
$ aarch64-linux-gnu-g++ --version
aarch64-linux-gnu-g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 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.

(2)离线安装

从官网下载软件包后(以gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu为例),要将交叉编译工具的路径添加到系统的 PATH 环境变量中,以便可以在任何地方使用交叉编译工具,可以在shell配置文件中添加以下行(通常是 ~/.bashrc~/.bash_profile~/.zshrc,具体取决于使用的shell),注意 PATH= 后的路径为交叉编译工具所在的目录。

  • gcc路径
bash 复制代码
<SDK Directory>/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
  • 打开shell配置文件
bash 复制代码
vi ~/.bashrc 
  • 将交叉编译工具的路径添加到系统的PATH环境变量中,将 <SDK Directory> 修改为自己的 SDK 路径
bash 复制代码
export PATH=$PATH:<SDK Directory>/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin
  • 重新加载shell配置文件,使更改生效
bash 复制代码
source ~/.bashrc  
  1. 使用交叉编译工具编译程序
bash 复制代码
aarch64-linux-gnu-gcc hello.c -o hello
  1. 交叉编译成功后,将在当前目录下生成可在开发板运行的可执行文件
bash 复制代码
$ ls
hello  hello.c
$ file hello
hello: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, ..., not stripped

4 运行程序

将 hello 程序上传到开发板,运行程序。

bash 复制代码
# ./hello
Hello, TaiShan-Pi (RK3566)!
I was compiled for ARM64.
相关推荐
wuyk5552 分钟前
59.嵌入式C语言高级宏定义实战:多行宏、字符串化与符号拼接
c语言·开发语言·stm32·单片机·嵌入式硬件·物联网·51单片机
jsons11 小时前
autofs挂载
linux·服务器·网络
liwulin05061 小时前
【ollama】自定义结构化输出
linux·前端·数据库·ollama
无相求码2 小时前
C程序的内存到底怎么分的?栈、堆、全局区一张图讲清楚
c语言
时空无限2 小时前
vllm 大模型启动缓存相关环境变量 export
linux·缓存·vllm
Echo_cy_2 小时前
基于ZYNQ-7000的Ethernet驱动配置
linux·驱动开发·嵌入式硬件·fpga开发·ethernet·zynq
天空'之城3 小时前
C 语言工业级通用组件手写 08:精准软件消抖
c语言·嵌入式开发·工业级组件·软件消抖·按键防抖
ShineWinsu3 小时前
对于Linux:模版方法类的解析以及socket、TcpSocket的封装
linux·c++·面试·socket·模板方法模式·封装·tcpsocket
无足鸟ICT3 小时前
【RHCA+】查看变量
linux·运维·服务器
Kina_C4 小时前
Linux DNS 服务器-从高速缓存到辅助 DNS 部署指南
linux·运维·服务器·dns