RK3568 OH5.1 编译运行程序hellworld

张开发
2026/4/17 10:02:16 15 分钟阅读

分享文章

RK3568 OH5.1 编译运行程序hellworld
编写helloworld代码根目录创建sample子系统文件夹在子系统目录下创建hello部件文件夹hello文件夹中创建hello源码目录及源码sample/hello/src/helloworld.c#include stdio.h #include helloworld.h void hello_oh(void); int main(int argc, char *argv[]) { hello_oh(); HelloWorld(); return 0; } void HelloWorld(void) { printf(\n\n); printf(Hello World!\n); printf(\n\n); } void hello_oh(void) { printf(\n\n); printf(Hello OpenHarmony!\n); printf(\n\n); }sample/hello/include/helloworld.h#ifndef __HELLOWORLD_H__ #define __HELLOWORLD_H__ #ifdef __cplusplus extern C { #endif void HelloWorld(); #ifdef __cplusplus } #endif #endif构建文件BUILD.gn及部件配置文件bundle.json。sample/hello/BUILD.gnimport(//build/ohos.gni) # 导入编译模板 ohos_executable(helloworld) { # 可执行模块 sources [ # 模块源码 src/helloworld.c ] include_dirs [ # 模块依赖头文件目录 include ] cflags [] cflags_c [] cflags_cc [] ldflags [] configs [] deps [] # 部件内部依赖 part_name hello # 所属部件名称必选 install_enable true # 是否默认安装缺省默认不安装可选 }sample/hello/bundle.json{ name: ohos/hello, description: Hello world example., version: 3.1, license: Apache License 2.0, publishAs: code-segment, segment: { destPath: sample/hello }, dirs: {}, scripts: {}, component: { name: hello, subsystem: sample, syscap: [], features: [], adapted_system_type: [ mini, small, standard ], rom: 10KB, ram: 10KB, deps: { components: [], third_party: [] }, build: { sub_component: [ //sample/hello:helloworld ], inner_kits: [], test: [] } } }修改子系统配置文件build/subsystem_config.jsonsample: { path: sample, name: sample },添加自定义的sample子系统修改产品配置文件vendor/hihope/rk3568/config.json{ subsystem: sample, components: [ { component: hello, features: [] } ] },在产品选中加入自定义的子系统。最后整个sample子系统目录结构如下sample/ └── hello ├── BUILD.gn ├── bundle.json ├── include │ └── helloworld.h └── src └── helloworld.c编译指定编译helloworld模块./build.sh --product-name rk3568 --ccache --build-target helloworld编译完成后目标文件所在位置out/rk3568/sample/hello/helloworld编译好的程序默认安装在system分区中system/system/bin/helloworld每编译一次镜像太久了就算是单独编译镜像也要很久./build.sh --product-name rk3568 --ccache --build-target system_image为了快速验证直接将编译的helloworld拷贝进镜像里mkdir system sudo mount system.img system sudo cp ../../../sample/hello/helloworld system/system/bin/ sudo umount systemhdc shell连接调试终端执行helloworld

更多文章