小安派学习(一)——环境搭建,交叉编译器环境报错解决

[复制链接]
查看1088 | 回复7 | 2023-9-9 19:58:45 | 显示全部楼层 |阅读模式

环境搭建

这是一篇基于Windows子系统搭建小安派编译环境的帖子!我本以为不需要记录这个的,但是每个人遇到的问题还真不一样。

至于怎么安装Windows子系统,这不是我所关心的。子系统是ubuntu18.04

常用开发软件安装和配置

sudo apt install git vim gcc make cmake ninja-build 

配置git

git config --global user.name "like"  // 此处like需要替换为自己的名字
git config --global user.name "like@example.com"  // 此处“[email]like@example.com[/email]”替换为自己的邮箱

生成ssh-key

ssh-keygen -t rsa -C "like@example.com"  // 此处“[email]like@example.com[/email]”替换为自己的邮箱

这里遇到设置密码什么的都不用管,一路回车。

克隆Ai-Thinker仓库

打开子系统终端,找一个存放你自己代码的路径,输入:

git clone https://github.com/Ai-Thinker-Open/AiPi-Open-Kits.git

image-20230909064601322

拉取子模块

cd AiPi-Open-Kits
git submodule init
git submodule update

image-20230909064823683

配置子模块提来关系和项目环境变量

进入aithinker_Ai-M6X_SDK拉取SDK子模块

cd aithinker_Ai-M6X_SDK

image-20230909065217131

有经验的小伙伴一眼就能看出install.shexport.sh脚本没有可执行权限。

查看当前目录下所有文件权限,发现需要执行的脚本权限不够:

image-20230909065401228

# 先给两个脚本添加可执行权限
chmod a+x install.sh export.sh

image-20230909065528783

# 然后分别执行两个脚本
./install.sh
./export.sh

install

install.sh

export

export.sh

编译和烧录

上面的环境搭建完成之后就可以进行编译了,首先来编译一下helloworld程序,进入helloworld目录:

cd examples/helloworld

编译:

首次编译之前可以先进行一次make clean 来清楚上次的编译产物

make

image-20230909070152226

是的,就是在这里遇到了问题,不过问题比较清晰:当前配置中给出的交叉编译工具路径不是绝对路径,反正就是找不到!

解决交叉编译工具找不到或路径不全的问题

以下是报错原文:

CMake Error at /home/like/code/AiPi-Open-Kits/aithinker_Ai-M6X_SDK/cmake/bouffalo_sdk-config.cmake:31 (enable_language):
  The CMAKE_C_COMPILER:

    riscv64-unknown-elf-gcc

  is not a full path and was not found in the PATH.

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.
Call Stack (most recent call first):
  CMakeLists.txt:5 (find_package)


CMake Error at /home/like/code/AiPi-Open-Kits/aithinker_Ai-M6X_SDK/cmake/bouffalo_sdk-config.cmake:31 (enable_language):
  The CMAKE_CXX_COMPILER:

    riscv64-unknown-elf-g++

  is not a full path and was not found in the PATH.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.
Call Stack (most recent call first):
  CMakeLists.txt:5 (find_package)


CMake Error at /home/like/code/AiPi-Open-Kits/aithinker_Ai-M6X_SDK/cmake/bouffalo_sdk-config.cmake:31 (enable_language):
  The CMAKE_ASM_COMPILER:

    riscv64-unknown-elf-gcc

  is not a full path and was not found in the PATH.

  Tell CMake where to find the compiler by setting either the environment
  variable "ASM" or the CMake cache entry CMAKE_ASM_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.
Call Stack (most recent call first):
  CMakeLists.txt:5 (find_package)

可以看到找不到交叉编译工具gcc和g++。我有一个习惯,遇到问题,先解决它,然后再优化它,所以:

先解决它

遇到工具找不到的问题,首先应该考虑是环境变量配置的时候出问题了,所以我直接去export.sh寻找答案,发现交叉编译环境变量配置的语句是:

export PATH=$PATH:$PWD/toolchain/bin
echo $PATH

主要是第一条语句,把当前目录($PWD)下的/toolchain/bin添加到环境变量中去,然后第二句把当前所有环境变量打印出来。

所以看到这里我们最起码指导了交叉编译工具在哪里了对吧:

/home/like/code/AiPi-Open-Kits/aithinker_Ai-M6X_SDK/toolchain/bin

image-20230909071210793

那我们便去这个路径下看一看:

cd /home/like/code/AiPi-Open-Kits/aithinker_Ai-M6X_SDK/toolchain/bin

image-20230909071402328

可以看到刚刚编译的时候报错的两个文件在这个路径下都能找到,并且这个路径已经添加到环境变量了。所以说明,上面的报错,不是找不到工具,而是CMake中配置的路径不全的问题。

那么好办,我们先把它补全,然后再试试。最直接粗暴的再Makefile中把交叉编译工具的路径写死:

# CROSS_COMPILE ?= riscv64-unknown-elf-
CROSS_COMPILE ?= /home/like/code/AiPi-Open-Kits/aithinker_Ai-M6X_SDK/toolchain/bin/riscv64-unknown-elf-

image-20230909072232446

然后编译验证:

make clean
make

image-20230909072825018

编译成功了

然后优雅的解决它

上面的问题虽然成功解决了,但是站在分享者的角度去看,别人拿到这个代码还是不能用的,所以这个地方最好是相对路径。所以做以下修改:

# CROSS_COMPILE ?= riscv64-unknown-elf-
CROSS_COMPILE ?= $(PWD)/../../toolchain/bin/riscv64-unknown-elf-

本帖被以下淘专辑推荐:

回复

使用道具 举报

可乐klelee | 2023-9-9 20:01:28 | 显示全部楼层
基于windows子系统ubuntu18.04的小安派开发环境搭建!
回复 支持 反对

使用道具 举报

ai_mcu | 2023-9-9 22:59:29 | 显示全部楼层
很详细了
明天总会更好
回复

使用道具 举报

jkernet | 2023-9-10 10:51:09 来自手机 | 显示全部楼层
学习打卡
回复

使用道具 举报

王乐乐 | 2023-9-10 22:36:40 | 显示全部楼层
打卡学习
回复

使用道具 举报

爱笑 | 2023-9-11 08:56:16 | 显示全部楼层
打卡学习
用心做好保姆工作
回复

使用道具 举报

chengfgc | 2023-9-11 08:59:52 | 显示全部楼层
打卡学习
回复

使用道具 举报

CHENQIGUANG1998 | 2023-9-11 10:29:11 | 显示全部楼层
打卡学习
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则