1# 编写“Hello World”程序 2 3 4下方将通过修改源码的方式展示如何编写简单程序,输出“Hello world”。请在下载的源码目录中进行下述操作。 5 6 71. 确定目录结构。 8 9 开发者编写业务时,务必先在./applications/sample/wifi-iot/app路径下新建一个目录(或一套目录结构),用于存放业务源码文件。 10 11 例如:在app下新增业务my_first_app,其中hello_world.c为业务代码,BUILD.gn为编译脚本,具体规划目录结构如下: 12 13 14 ``` 15 . 16 └── applications 17 └── sample 18 └── wifi-iot 19 └── app 20 └── my_first_app 21 │── hello_world.c 22 └── BUILD.gn 23 ``` 24 252. 编写业务代码。 26 27 新建./applications/sample/wifi-iot/app/my_first_app下的hello_world.c文件,在hello_world.c中新建业务入口函数HelloWorld,并实现业务逻辑。并在代码最下方,使用OpenHarmony启动恢复模块接口SYS_RUN()启动业务。(SYS_RUN定义在ohos_init.h文件中) 28 29 ``` 30 #include <stdio.h> 31 #include "ohos_init.h" 32 #include "ohos_types.h" 33 34 void HelloWorld(void) 35 { 36 printf("[DEMO] Hello world.\n"); 37 } 38 SYS_RUN(HelloWorld); 39 ``` 40 413. 编写用于将业务构建成静态库的BUILD.gn文件。 42 43 新建./applications/sample/wifi-iot/app/my_first_app下的BUILD.gn文件,并完成如下配置。 44 45 如步骤1所述,BUILD.gn文件由三部分内容(目标、源文件、头文件路径)构成,需由开发者完成填写。 46 47 48 ``` 49 static_library("myapp") { 50 sources = [ 51 "hello_world.c" 52 ] 53 include_dirs = [ 54 "//utils/native/lite/include" 55 ] 56 } 57 ``` 58 59 - static_library中指定业务模块的编译结果,为静态库文件libmyapp.a,开发者根据实际情况完成填写。 60 - sources中指定静态库.a所依赖的.c文件及其路径,若路径中包含"//"则表示绝对路径(此处为代码根路径),若不包含"//"则表示相对路径。 61 - include_dirs中指定source所需要依赖的.h文件路径。 62 634. 添加新组件。 64 65 修改文件build/lite/components/communication.json,添加组件hello_world_app的配置,如下所示为communication.json文件片段,"\#\#start\#\#"和"\#\#end\#\#"之间为新增配置("\#\#start\#\#"和"\#\#end\#\#"仅用来标识位置,添加完配置后删除这两行): 66 67 68 ``` 69 { 70 "components": [ 71 { 72 "component": "camera_sample_communication", 73 "description": "Communication related samples.", 74 "optional": "true", 75 "dirs": [ 76 "applications/sample/camera/communication" 77 ], 78 "targets": [ 79 "//applications/sample/camera/communication:sample" 80 ], 81 "rom": "", 82 "ram": "", 83 "output": [], 84 "adapted_kernel": [ "liteos_a" ], 85 "features": [], 86 "deps": { 87 "components": [], 88 "third_party": [] 89 } 90 }, 91 ##start## 92 { 93 "component": "hello_world_app", 94 "description": "hello world samples.", 95 "optional": "true", 96 "dirs": [ 97 "applications/sample/wifi-iot/app/my_first_app" 98 ], 99 "targets": [ 100 "//applications/sample/wifi-iot/app/my_first_app:myapp" 101 ], 102 "rom": "", 103 "ram": "", 104 "output": [], 105 "adapted_kernel": [ "liteos_m" ], 106 "features": [], 107 "deps": { 108 "components": [], 109 "third_party": [] 110 } 111 }, 112 ##end## 113 { 114 "component": "camera_sample_app", 115 "description": "Camera related samples.", 116 "optional": "true", 117 "dirs": [ 118 "applications/sample/camera/launcher", 119 "applications/sample/camera/cameraApp", 120 "applications/sample/camera/setting", 121 "applications/sample/camera/gallery", 122 "applications/sample/camera/media" 123 ], 124 ``` 125 1265. 修改单板配置文件。 127 128 修改文件vendor/hisilicon/hispark_pegasus/config.json,新增hello_world_app组件的条目,如下所示代码片段为applications子系统配置,"\#\#start\#\#"和"\#\#end\#\#"之间为新增条目("\#\#start\#\#"和"\#\#end\#\#"仅用来标识位置,添加完配置后删除这两行): 129 130 131 ``` 132 { 133 "subsystem": "applications", 134 "components": [ 135 ##start## 136 { "component": "hello_world_app", "features":[] }, 137 ##end## 138 { "component": "wifi_iot_sample_app", "features":[] } 139 ] 140 }, 141 ``` 142