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