1# 编写“Hello World”程序 2 3 4下方将展示如何在单板上运行第一个应用程序,其中包括新建应用程序、编译、烧写、运行等步骤,最终输出“Hello World!”。 5 6 7## 示例目录 8 9示例完整目录如下: 10 11 12 13``` 14applications/sample/hello 15│── BUILD.gn 16└── src 17 └── helloworld.c 18``` 19 20 21## 开发步骤 22 23请在源码目录中通过以下步骤创建“Hello World”应用程序。 24 251. 新建目录及源码。 26 27 新建applications/sample/hello/src/helloworld.c目录及文件,代码如下所示,用户可以自定义修改打印内容(例如:修改OHOS为World)。当前应用程序可支持标准C及C++的代码开发。 28 29 30 ``` 31 #include <stdio.h> 32 33 int main(int argc, char **argv) 34 { 35 printf("\n\n"); 36 printf("\n\t\tHello OHOS!\n"); 37 printf("\n\n\n"); 38 39 return 0; 40 } 41 ``` 42 432. 新建编译组织文件。 44 45 新建applications/sample/hello/BUILD.gn文件,内容如下所示: 46 47 48 ``` 49 import("//build/lite/config/component/lite_component.gni") 50 lite_component("hello-OHOS") { 51 features = [ ":helloworld" ] 52 } 53 executable("helloworld") { 54 output_name = "helloworld" 55 sources = [ "src/helloworld.c" ] 56 } 57 ``` 58 593. 添加新组件。 60 61 修改文件build/lite/components/communication.json,添加组件hello_world_app的配置,如下所示为**communication.json**文件片段,"\#\#start\#\#"和"\#\#end\#\#"之间为新增配置("\#\#start\#\#"和"\#\#end\#\#"仅用来标识位置,添加完配置后删除这两行): 62 63 64 ``` 65 { 66 "components": [ 67 { 68 "component": "camera_sample_communication", 69 "description": "Communication related samples.", 70 "optional": "true", 71 "dirs": [ 72 "applications/sample/camera/communication" 73 ], 74 "targets": [ 75 "//applications/sample/camera/communication:sample" 76 ], 77 "rom": "", 78 "ram": "", 79 "output": [], 80 "adapted_kernel": [ "liteos_a" ], 81 "features": [], 82 "deps": { 83 "components": [], 84 "third_party": [] 85 } 86 }, 87 ##start## 88 { 89 "component": "hello_world_app", 90 "description": "hello world samples.", 91 "optional": "true", 92 "dirs": [ 93 "applications/sample/hello" 94 ], 95 "targets": [ 96 "//applications/sample/hello:hello-OHOS" 97 ], 98 "rom": "", 99 "ram": "", 100 "output": [], 101 "adapted_kernel": [ "liteos_a" ], 102 "features": [], 103 "deps": { 104 "components": [], 105 "third_party": [] 106 } 107 }, 108 ##end## 109 { 110 "component": "camera_sample_app", 111 "description": "Camera related samples.", 112 "optional": "true", 113 "dirs": [ 114 "applications/sample/camera/launcher", 115 "applications/sample/camera/cameraApp", 116 "applications/sample/camera/setting", 117 "applications/sample/camera/gallery", 118 "applications/sample/camera/media" 119 ], 120 ``` 121 1224. 修改单板配置文件。 123 124 修改文件vendor/hisilicon/hispark_taurus/config.json,新增hello_world_app组件的条目,如下所示代码片段为applications子系统配置,"\#\#start\#\#"和"\#\#end\#\#"之间为新增条目("\#\#start\#\#"和"\#\#end\#\#"仅用来标识位置,添加完配置后删除这两行): 125 126 127 ``` 128 { 129 "subsystem": "applications", 130 "components": [ 131 { "component": "camera_sample_app", "features":[] }, 132 { "component": "camera_sample_ai", "features":[] }, 133 ##start## 134 { "component": "hello_world_app", "features":[] }, 135 ##end## 136 { "component": "camera_screensaver_app", "features":[] } 137 ] 138 }, 139 ``` 140