379 字
2 分钟
为开源鸿蒙哪吒开发板编写程序
使用 C 语言为开源鸿蒙编写第一个程序 Hello NeZha
。
下载编译工具链
前往 玄铁资源中心,在 资源目录
中选择 剑池开发工具集
- 编译工具
- GCC工具链-900系列
。若在 Windows
系统下进行编译,则下载 Xuantie-900-gcc-elf-newlib-mingw-V3.2.0-20250627.tar.gz
;若在 Linux
系统下进行编译,则下载 Xuantie-900-gcc-elf-newlib-x86_64-V3.2.0-20250627.tar.gz
。
以 Windows
系统为例,下载完成后解压,Xuantie-900-gcc-elf-newlib-mingw-V3.2.0\bin\riscv64-unknown-elf-gcc.exe
即为将要使用的编译器。
编写程序
创建 hello_word.c
文件,内容如下:
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Hello NeZha\n");
return 0;
}
编译程序
使用以下命令进行编译,其中 riscv64-unknown-elf-gcc.exe
替换为实际的编译器路径:
.\Xuantie-900-gcc-elf-newlib-mingw-V3.2.0\bin\riscv64-unknown-elf-gcc.exe -o hello_word hello_word.c
编译成功后会在 hello_word.c
同目录下生成 hello_word
可执行文件。
传输程序
NOTE由于哪吒开发板上的开源鸿蒙系统目前不支持通过
hdc
传输文件,此处使用 U 盘进行传输。为保证兼容性,使用FAT32
格式化 U 盘。
将 U 盘插入电脑,复制 hello_word
文件到 U 盘根目录。然后将 U 盘插入哪吒开发板的 USB 接口。
使用以下命令在哪吒开发板上挂载 U 盘:
mkdir -p /mnt/sda
mount -t vfat /dev/block/sda /mnt/sda
NOTE后续可以使用
umount /mnt/sda
命令卸载 U 盘。
将程序复制到 /bin
目录下:
cp -v /mnt/sda/hello_word /bin/
chmod +x /bin/hello_word
运行程序
输入命令 hello_word
即可运行程序,程序运行如下:
# hello_word
Hello NeZha
#