嘉楠堪智 CanMV K230 进行 C 语言程序开发

本文记录学习、使用 K230 SDK 进行 C 语言程序开发的一些关键步骤,编写程序源代码,如何编译运行在大核和小核的程序,如何使用 SCons 进行编译。

一、编写代码

在 ubuntu 上创建一个 C 文件 hello.c 并加入如下代码:

cpp 复制代码
#include <stdio.h>
int main (void)
{
    printf("hello world\n");
    return 0;
}

将 hello.c 放到与 k230_sdk 同一级目录下:

复制代码
canaan@develop:\~/work$ ls
hello.c   k230_sdk

二、编译:

编译小核程序

编译适用于小核 linux 的可执行程序:

复制代码
k230_sdk/toolchain/Xuantie-900-gcc-linux-5.10.4-glibc-x86_64-V2.6.0/bin/riscv64-unknown-linux-gnu-gcc hello.c -o hello

编译大核程序

编译适用于大核 rt-smart 的可执行程序:

复制代码
k230_sdk/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-unknown-linux-musl-gcc -o hello.o -c -mcmodel=medany -march=rv64imafdcv -mabi=lp64d hello.c
k230_sdk/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-unknown-linux-musl-gcc -o hello.elf -mcmodel=medany -march=rv64imafdcv -mabi=lp64d -T k230_sdk/src/big/mpp/userapps/sample/linker_scripts/riscv64/link.lds -Lk230_sdk/src/big/rt-smart/userapps/sdk/rt-thread/lib -Wl,--whole-archive -lrtthread -Wl,--no-whole-archive -n --static hello.o -Lk230_sdk/src/big/rt-smart/userapps/sdk/lib/risc-v/rv64 -Lk230_sdk/src/big/rt-smart/userapps/sdk/rt-thread/lib/risc-v/rv64 -Wl,--start-group -lrtthread -Wl,--end-group

三、运行程序

将编译好的 hello 以及 hello.elf 拷贝到 sd 卡的 vfat 分区内( sd 卡烧写完镜像后可以在 pc 端看到一个可用的盘符),或通过其他方式(参考 sdk 使用说明文档)将可执行程序拷贝到小核的 /sharefs 目录下。

  • 开发板启动后,在小核端运行测试程序,小核启动后输入 root 进入控制台
复制代码
Welcome to Buildroot
canaan login: root
\[root@canaan \~ \]#cd /sharefs
\[root@canaan /sharefs \]#./hello
hello world
  • 在大核端运行测试程序
复制代码
msh /sharefs\>hello.elf
hello world

四、SCons 编译大核程序

上面例子可以看到,用 musl-gcc 直接编译大核程序的话,编译参数是比较多的,对于初学者来说很不方便,也不太好理解,官方推荐使用 SCons 来编译。

k230_sdk/src/big/rt-smart/userapps 目录下创建一个文件夹,命名为 hello

复制代码
cd k230_sdk/src/big/rt-smart/userapps
mkdir hello
cd hello

创建以下三个文件:

  • hello.c (同上,略)
  • SConscript
python 复制代码
# RT-Thread building script for component

from building import *

cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]

CPPDEFINES = [
    'HAVE_CCONFIG_H',
]
group = DefineGroup('hello', src, depend=[''], CPPPATH=CPPPATH, CPPDEFINES=CPPDEFINES)

Return('group')
  • SConstruct
python 复制代码
import os
import sys

# add building.py path
sys.path = sys.path + [os.path.join('..','..','tools')]
from building import *

BuildApplication('hello', 'SConscript', usr_root = '../')

回到 k230_sdk/src/big/rt-smart/ 目录,配置环境变量:

复制代码
canaan@develop:\~/k230_sdk/src/big/rt-smart$ source smart-env.sh riscv64
Arch         => riscv64
CC           => gcc
PREFIX       => riscv64-unknown-linux-musl-
EXEC_PATH    => /home/canaan/k230_sdk/src/big/rt-smart/../../../toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin

进入 k230_sdk/src/big/rt-smart/userapps 目录,编译程序:

复制代码
canaan@develop:\~/k230_sdk/src/big/rt-smart/userapps$ scons --directory=hello
scons: Entering directory `/home/canaan/k230_sdk/src/big/rt-smart/userapps/hello'
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: build/hello
CC build/hello/hello.o
LINK hello.elf
/home/canaan/k230_sdk/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/../lib/gcc/riscv64-unknown-linux-musl/12.0.1/../../../../riscv64-unknown-linux-musl/bin/ld: warning: hello.elf has a LOAD segment with RWX permissions
scons: done building targets.

编译好的程序在 hello 文件夹下:

复制代码
canaan@develop:\~/k230_sdk/src/big/rt-smart/userapps$ ls hello/
build  cconfig.h  hello.c  hello.elf  SConscript  SConstruct

之后即可将 hello.elf 拷贝到小核 linux 上,然后大核 rt-smart 通过 /sharefs 即可运行该程序。

老徐,2024/5/4

相关推荐
isyangli_blog7 小时前
OpenDayLight (Carbon 版本) 启动与组件安装
开发语言·php
vb2008117 小时前
FastAPI APIRouter
开发语言·python
Benszen7 小时前
KVM虚拟化解决方案
开发语言·perl
会编程的土豆7 小时前
Go 语言反射(Reflection)详解
开发语言·后端·golang
東雪木7 小时前
多线程与并发编程 专属复习笔记
java·开发语言·笔记·java面试
杨充8 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法
噜噜噜阿鲁~8 小时前
python学习笔记 | 11.3、面向对象高级编程-多重继承
java·开发语言
basketball6168 小时前
Go 语言从入门到进阶:4. 数组和MAP使用方法总结
开发语言·后端·golang
春生野草8 小时前
反射、Tomcat执行
java·开发语言
雪的季节9 小时前
企业级 Qt 全功能项目
开发语言·数据库·qt