RK3568 OH5.1 编译运行程序hellworld

编写helloworld

  1. 代码根目录创建sample子系统文件夹
  2. 在子系统目录下创建hello部件文件夹
  3. 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
  1. 构建文件BUILD.gn及部件配置文件bundle.json。

sample/hello/BUILD.gn:

import("//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": []
        }
    }
}

  1. 修改子系统配置文件

build/subsystem_config.json:

"sample": {
    "path": "sample",
    "name": "sample"
  },

添加自定义的sample子系统

  1. 修改产品配置文件

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 system

hdc shell连接调试终端,执行helloworld:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欲盖弥彰1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值