From 4e2e87dd2cfccc664a288b2c14f45005fb1ca9c3 Mon Sep 17 00:00:00 2001 From: sanysun Date: Fri, 23 Jul 2021 08:50:35 +0000 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E7=94=A8=E6=B3=95=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E4=B8=AD=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AAHelloWorld?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arm_virt/README_zh.md | 2 + arm_virt/example.md | 121 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/arm_virt/README_zh.md b/arm_virt/README_zh.md index 0fba57b..6db9dbc 100755 --- a/arm_virt/README_zh.md +++ b/arm_virt/README_zh.md @@ -105,3 +105,5 @@ Explanation for our system configuration: - [向内核传递调试参数](example.md#sectiondebug) - [用FAT映像传递文件](example.md#sectionfatfs) + +- [添加一个Hello World程序](example.md#sectiondebug) diff --git a/arm_virt/example.md b/arm_virt/example.md index 699365f..af5c76c 100644 --- a/arm_virt/example.md +++ b/arm_virt/example.md @@ -79,3 +79,124 @@ sudo umount some_dir sudo losetup -d /dev/loop1 # 宿主机时 sudo losetup -d /dev/loop0 # 宿主机时 ``` +## 添加一个HelloWorld程序 +--- +1. 创建helloworld目录 +``` +helloworld目录结构如下: +applications/sample/helloworld +applications/sample/helloworld/src +``` + +2. 创建helloworld.c文件 +``` +在 applications/sample/helloworld/src下创建helloworld.c文件,并添加如下代码: +``` +``` +#include + +int main(int argc, char **argv) +{ + printf("\n************************************************\n"); + printf("\n\t\tHello OHOS!\n"); + printf("\n************************************************\n\n"); + + return 0; +} +``` + +3. 为helloword创建BUILD.gn文件 +``` +在 applications/sample/helloworld下添加BUILD.gn文件,并添加如下代码: +``` +``` +import("//build/lite/config/component/lite_component.gni") +lite_component("hello-OHOS") { + features = [ ":helloworld" ] +} +executable("helloworld") { + output_name = "helloworld" + sources = [ "src/helloworld.c" ] + include_dirs = [] + defines = [] + cflags_c = [] + ldflags = [] +} +``` + +**提示**:hellworld最后目录结构为 +``` +applications/sample/helloworld +applications/sample/helloworld/BUILD.gn +applications/sample/helloworld/src +applications/sample/helloworld/src/helloworld.c +``` + +4. 在build/lite/components中新建配置文件helloworld.json,并添加如下代码: +``` +{ + "components": [ + { + "component": "hello_world_app", + "description": "Communication related samples.", + "optional": "true", + "dirs": [ + "applications/sample/helloworld" + ], + "targets": [ + "//applications/sample/helloworld:hello-OHOS" + ], + "rom": "", + "ram": "", + "output": [], + "adapted_kernel": [ "liteos_a" ], + "features": [], + "deps": { + "components": [], + "third_party": [] + } + } + ] +} +``` +**注意**:helloworld.json中dirs和targets的属性值是不带src的 + +5. 在vendor/ohos/display_qemu_liteos_a/config.json配置文件中找到subsystems属性,并下面追加helloworld的subsystem配置,配置参考如下: +``` + ... + "subsystems": [ + ... + { + "subsystem": "helloworld", + "components": [ + { "component": "hello_world_app", "features":[] } + ] + } + ], + ... +``` +**注意**:vendor/ohos/display_qemu_liteos_a/config.json对应的编译模板为 display_qemu,后面编译时需要选择这个模板;另外修改JSON配置的时候一定要将多余的逗号去掉,否则编译时会报错 + +6. 编译并构建qemu虚拟环境 + +参考链接: [编译方法](README_zh.md) + +**注意**:helloworld 正常编译后会出现在 out/arm_virt/display_qemu/bin中,如果没有,请返回检查相关配置文件中的路径和名称是否有误,并尝试重新编译直到出现helloword + +``` +提示: qemu-init和qemu-run两个文件已经包含qemu相关的操作指令,可以先执行./qemu-init 然后再执行./qemu-run +``` + +7. 运行helloworld + +helloworld在qemu虚拟机的bin目录下面,进入qemu虚拟机环境后,在bin目录下执行 ./helloword,会出现如下信息,表示Hello World程序添加成功 + +``` +OHOS # ./helloworld +OHOS # +************************************************ + + Hello OHOS! + +************************************************ +``` -- Gitee