• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Writing a Hello World Program
2
3
4The following exemplifies how to run the first program on the development board. The created program outputs the message "Hello World!" .
5
6
7## Example Directory
8
9  The complete code directory is as follows:
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
21vendor/hihope
22└── rk3568
23    └── config.json
24```
25
26
27## How to Develop
28
29Perform the steps below in the source code directory:
30
311. Create a directory and write the service code.
32
33   Create the **applications/sample/hello/src/helloworld.c** directory and file whose code is shown in the following example. You can customize the content to be printed. For example, you can change **World** to **OH**. Declare the string printing function **HelloPrint** in the **helloworld.h** file. You can use either C or C++ to develop a program.
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   Add the header file **applications/sample/hello/include/helloworld.h**. The sample code is as follows:
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. Create a build file.
77
78   1. Create the **applications/sample/hello/BUILD.gn** file. The file content is as follows:
79
80       ```
81       import("//build/ohos.gni") # Import the build template.
82       ohos_executable("helloworld") {# Executable module.
83         sources = [       # Source code of the module.
84           "src/helloworld.c"
85         ]
86         include_dirs = [  # Directory of header file on which the module depends.
87           "include"
88         ]
89         cflags = []
90         cflags_c = []
91         cflags_cc = []
92         ldflags = []
93         configs = []
94         deps =[]    # Internal dependencies of a component.
95         part_name = "hello"    # Component name. This parameter is mandatory.
96         install_enable = true # Whether to install the software by default. This parameter is optional. By default, the software is not installed.
97       }
98       ```
99   2. Create the **applications/sample/hello/bundle.json** file and add the description of the **sample** component. The content is as follows:
100
101       ```
102       {
103           "name": "@ohos/hello",
104           "description": "Hello world example.",
105           "version": "3.1",
106           "license": "Apache License 2.0",
107           "publishAs": "code-segment",
108           "segment": {
109               "destPath": "applications/sample/hello"
110           },
111           "dirs": {},
112           "scripts": {},
113           "component": {
114               "name": "hello",
115               "subsystem": "sample",
116               "syscap": [],
117               "features": [],
118               "adapted_system_type": [ "mini", "small", "standard" ],
119               "rom": "10KB",
120               "ram": "10KB",
121               "deps": {
122                   "components": [],
123                   "third_party": []
124               },
125               "build": {
126                   "sub_component": [
127                       "//applications/sample/hello:helloworld"
128                   ],
129                   "inner_kits": [],
130                   "test": []
131               }
132           }
133       }
134       ```
135
136       The **bundle.json** file consists of two parts. The first part describes the information about the subsystem to which the component belongs, and the second part defines the component building configuration. When adding a part, you need to specify the **sub_component** contained in the part. If there are interfaces provided for other components, describe them in **inner_kits**. If there are test cases, describe them in **test**.
137
1383. Modify the subsystem configuration file.
139
140   Add the configuration of the new subsystem to the **build/subsystem_config.json** file.
141
142
143   ```
144   "sample": {
145       "path": "applications/sample/hello",
146       "name": "sample"
147     },
148   ```
149
1504. Modify the product configuration file.
151
152     In the **vendor\hihope\rk3568\config.json** file, add the **hello** part after the existing part.
153
154   ```
155       "usb:usb_manager_native":{},
156       "applications:prebuilt_hap":{},
157       "sample:hello":{},
158       "wpa_supplicant-2.9:wpa_supplicant-2.9":{},
159   ```
160