• 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为OHOS)。其中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
102   2. 新建applications/sample/hello/bundle.json文件,添加sample部件描述,内容如下所示。
103
104       ```
105       {
106           "name": "@ohos/hello",
107           "description": "Hello world example.",
108           "version": "3.1",
109           "license": "Apache License 2.0",
110           "publishAs": "code-segment",
111           "segment": {
112               "destPath": "applications/sample/hello"
113           },
114           "dirs": {},
115           "scripts": {},
116           "component": {
117               "name": "hello",
118               "subsystem": "sample",
119               "syscap": [],
120               "features": [],
121               "adapted_system_type": [ "mini", "small", "standard" ],
122               "rom": "10KB",
123               "ram": "10KB",
124               "deps": {
125                   "components": [],
126                   "third_party": []
127               },
128               "build": {
129                   "sub_component": [
130                       "//applications/sample/hello:helloworld"
131                   ],
132                   "inner_kits": [],
133                   "test": []
134               }
135           }
136       }
137       ```
138
139       bundle.json文件包含两个部分,第一部分描述该部件所属子系统的信息,第二部分component则定义该部件构建相关配置。添加的时候需要指明该部件包含的模块sub_component,假如有提供给其它部件的接口,需要在inner_kits中说明,假如有测试用例,需要在test中说明,inner_kits与test没有也可以不添加。
140
1413. 修改子系统配置文件。
142
143build/subsystem_config.json中添加新建的子系统的配置。
144
145
146   ```
147   "sample": {
148       "path": "applications/sample/hello",
149       "name": "sample"
150     },
151   ```
152
1534. 修改产品配置文件。
154
155   在productdefine\common\products\Hi3516DV300.json中添加对应的hello部件,直接添加到原有部件后即可。
156
157
158   ```
159       "usb:usb_manager_native":{},
160       "applications:prebuilt_hap":{},
161       "sample:hello":{},
162       "wpa_supplicant-2.9:wpa_supplicant-2.9":{},
163   ```
164