• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 编写“Hello World”程序
2
3
4下方将展示如何在单板上运行第一个应用程序,其中包括新建应用程序、编译、烧写、运行等步骤,最终输出“Hello World!”。
5
6
7## 示例目录
8
9示例完整目录如下。
10
11
12```
13applications/sample/hello
14│   │── BUILD.gn
15│   │── include
16│   │   └── helloworld.h
17│   │── src
18│   │   └── helloworld.c
19│   └── bundle.json
20build
21└── subsystem_config.json
22productdefine/common
23└── products
24    └── Hi3516DV300.json
25```
26
27
28## 开发步骤
29
30请在源码目录中通过以下步骤创建“Hello World”应用程序:
31
32
331. 创建目录,编写业务代码。
34
35   新建applications/sample/hello/src/helloworld.c目录及文件,代码如下所示,用户可以自定义修改打印内容(例如:修改World为OH)。其中helloworld.h包含字符串打印函数HelloPrint的声明。当前应用程序可支持标准C及C++的代码开发。
36
37
38   ```
39   #include <stdio.h>
40   #include "helloworld.h"
41
42   int main(int argc, char **argv)
43   {
44       HelloPrint();
45       return 0;
46   }
47
48   void HelloPrint()
49   {
50       printf("\n\n");
51       printf("\n\t\tHello World!\n");
52       printf("\n\n");
53   }
54   ```
55
56   再添加头文件applications/sample/hello/include/helloworld.h,代码如下所示。
57
58
59   ```
60   #ifndef HELLOWORLD_H
61   #define HELLOWORLD_H
62   #ifdef __cplusplus
63   #if __cplusplus
64   extern "C" {
65   #endif
66   #endif
67
68   void HelloPrint();
69
70   #ifdef __cplusplus
71   #if __cplusplus
72   }
73   #endif
74   #endif
75   #endif // HELLOWORLD_H
76   ```
77
782. 新建编译组织文件。
79
80   1. 新建applications/sample/hello/BUILD.gn文件,内容如下所示:
81
82       ```
83       import("//build/ohos.gni")  # 导入编译模板
84       ohos_executable("helloworld") { # 可执行模块
85         sources = [       # 模块源码
86           "src/helloworld.c"
87         ]
88         include_dirs = [  # 模块依赖头文件目录
89           "include"
90         ]
91         cflags = []
92         cflags_c = []
93         cflags_cc = []
94         ldflags = []
95         configs = []
96         deps =[]    # 部件内部依赖
97         part_name = "hello"    # 所属部件名称,必选
98         install_enable = true  # 是否默认安装(缺省默认不安装),可选
99       }
100       ```
101   2. 新建applications/sample/hello/bundle.json文件,添加sample部件描述,内容如下所示。
102
103       ```
104       {
105           "name": "@ohos/hello",
106           "description": "Hello world example.",
107           "version": "3.1",
108           "license": "Apache License 2.0",
109           "publishAs": "code-segment",
110           "segment": {
111               "destPath": "applications/sample/hello"
112           },
113           "dirs": {},
114           "scripts": {},
115           "component": {
116               "name": "hello",
117               "subsystem": "sample",
118               "syscap": [],
119               "features": [],
120               "adapted_system_type": [ "mini", "small", "standard" ],
121               "rom": "10KB",
122               "ram": "10KB",
123               "deps": {
124                   "components": [],
125                   "third_party": []
126               },
127               "build": {
128                   "sub_component": [
129                       "//applications/sample/hello:helloworld"
130                   ],
131                   "inner_kits": [],
132                   "test": []
133               }
134           }
135       }
136       ```
137
138       bundle.json文件包含两个部分,第一部分描述该部件所属子系统的信息,第二部分component则定义该部件构建相关配置。添加的时候需要指明该部件包含的模块sub_component,假如有提供给其它部件的接口,需要在inner_kits中说明,假如有测试用例,需要在test中说明,inner_kits与test没有也可以不添加。
139
1403. 修改子系统配置文件。
141
142build/subsystem_config.json中添加新建的子系统的配置。
143
144
145   ```
146   "sample": {
147       "path": "applications/sample/hello",
148       "name": "sample"
149     },
150   ```
151
1524. 修改产品配置文件。
153
154productdefine/common/products/Hi3516DV300.json中添加对应的hello部件,直接添加到原有部件后即可。
155
156
157   ```
158       "usb:usb_manager_native":{},
159       "applications:prebuilt_hap":{},
160       "sample:hello":{},
161       "wpa_supplicant-2.9:wpa_supplicant-2.9":{},
162   ```
163