环境搭建
这是一篇基于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](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909064601322.png)
拉取子模块
cd AiPi-Open-Kits
git submodule init
git submodule update
![image-20230909064823683](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909064823683.png)
配置子模块提来关系和项目环境变量
进入aithinker_Ai-M6X_SDK拉取SDK子模块
cd aithinker_Ai-M6X_SDK
![image-20230909065217131](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909065217131.png)
有经验的小伙伴一眼就能看出install.sh
和export.sh
脚本没有可执行权限。
查看当前目录下所有文件权限,发现需要执行的脚本权限不够:
![image-20230909065401228](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909065401228.png)
# 先给两个脚本添加可执行权限
chmod a+x install.sh export.sh
![image-20230909065528783](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909065528783.png)
# 然后分别执行两个脚本
./install.sh
./export.sh
install
![install.sh](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909065653816.png)
export
![export.sh](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909065743940.png)
编译和烧录
上面的环境搭建完成之后就可以进行编译了,首先来编译一下helloworld程序,进入helloworld目录:
cd examples/helloworld
编译:
首次编译之前可以先进行一次make clean
来清楚上次的编译产物
make
![image-20230909070152226](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909070152226.png)
是的,就是在这里遇到了问题,不过问题比较清晰:当前配置中给出的交叉编译工具路径不是绝对路径,反正就是找不到!
解决交叉编译工具找不到或路径不全的问题
以下是报错原文:
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](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909071210793.png)
那我们便去这个路径下看一看:
cd /home/like/code/AiPi-Open-Kits/aithinker_Ai-M6X_SDK/toolchain/bin
![image-20230909071402328](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909071402328.png)
可以看到刚刚编译的时候报错的两个文件在这个路径下都能找到,并且这个路径已经添加到环境变量了。所以说明,上面的报错,不是找不到工具,而是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](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909072232446.png)
然后编译验证:
make clean
make
![image-20230909072825018](https://klelee-images.oss-cn-qingdao.aliyuncs.com/typora/image-20230909072825018.png)
编译成功了
然后优雅的解决它
上面的问题虽然成功解决了,但是站在分享者的角度去看,别人拿到这个代码还是不能用的,所以这个地方最好是相对路径。所以做以下修改:
# CROSS_COMPILE ?= riscv64-unknown-elf-
CROSS_COMPILE ?= $(PWD)/../../toolchain/bin/riscv64-unknown-elf-