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