嘉楠堪智 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

相关推荐
Tony Bai18 小时前
“我曾想付钱给 Google 去工作”—— Russ Cox 深度访谈:Go 的诞生、演进与未来
开发语言·后端·golang
sali-tec18 小时前
C# 基于halcon的视觉工作流-章66 四目匹配
开发语言·人工智能·数码相机·算法·计算机视觉·c#
hnlgzb18 小时前
安卓app开发,如何快速上手kotlin和compose的开发?
android·开发语言·kotlin
无敌最俊朗@19 小时前
STL-deque面试剖析(面试复习4)
开发语言
APIshop19 小时前
用 Python 把“API 接口”当数据源——从找口子到落库的全流程实战
开发语言·python
Java Fans19 小时前
Qt Designer 和 PyQt 开发教程
开发语言·qt·pyqt
DIY机器人工房19 小时前
(十三)嵌入式面试题收集:6道
单片机·嵌入式硬件·diy机器人工房
RwTo19 小时前
【源码】-Java线程池ThreadPool
java·开发语言
兮动人19 小时前
EMT4J定制规则版:Java 8→17迁移兼容性检测与规则优化实战
java·开发语言·emt4j
一点★19 小时前
Java中的常量池和字符串常量池
java·开发语言