• 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
8## Example Directory
9
10Obtain the OpenHarmony project code. From the source code root directory, add the **sample/hello** directory, and then create therein the **hello** source code directory, the build file **BUILD.gn**, and the component configuration file **bundle.json**.
11The complete code directory is as follows:
12
13
14```
15sample/hello
16│── BUILD.gn
17│── include
18│   └── helloworld.h
19│── src
20│   └── helloworld.c
21├── bundle.json
22build
23└── subsystem_config.json
24vendor/hihope
25└── rk3568
26    └── config.json
27```
28
29
30## How to Develop
31
32Perform the steps below in the source code root directory:
33
341. Create a directory and write the service code.
35
36   Create the **sample/hello/src/helloworld.c** file, with the sample code as follows. In this example, the content to be printed is **World**, which you can change to any string that you prefer, for example, **OHOS**. The print function **HelloPrint** is declared in the included **helloworld.h** file. You can use either C or C++ to develop a program.
37
38
39   ```
40   #include <stdio.h>
41   #include "helloworld.h"
42
43   int main(int argc, char **argv)
44   {
45       HelloPrint();
46       return 0;
47   }
48
49   void HelloPrint()
50   {
51       printf("\n\n");
52       printf("\n\t\tHello World!\n");
53       printf("\n\n");
54   }
55   ```
56
57   Add the header file **sample/hello/include/helloworld.h**. The sample code is as follows:
58
59
60   ```
61   #ifndef HELLOWORLD_H
62   #define HELLOWORLD_H
63   #ifdef __cplusplus
64   #if __cplusplus
65   extern "C" {
66   #endif
67   #endif
68
69   void HelloPrint();
70
71   #ifdef __cplusplus
72   #if __cplusplus
73   }
74   #endif
75   #endif
76   #endif // HELLOWORLD_H
77   ```
78
792. Create a build file.
80
81   Create the **sample/hello/BUILD.gn** file. For details, see [Module](../subsystems/subsys-build-module.md).
82
83   The content of the **BUILD.gn** file is as follows:
84
85   ```
86   import("//build/ohos.gni") # Import the build template.
87   ohos_executable("helloworld") {# Executable module.
88     sources = [       # Source code of the module.
89       "src/helloworld.c"
90     ]
91     include_dirs = [  # Directory of header files on which the module depends.
92       "include"
93     ]
94     cflags = []
95     cflags_c = []
96     cflags_cc = []
97     ldflags = []
98     configs = []
99     deps =[]    # Internal dependencies of a component.
100     part_name = "hello"    # Component name. This parameter is mandatory.
101     install_enable = true # Whether to install the software by default. This parameter is optional. By default, the software is not installed.
102   }
103   ```
104
1053. Create a component configuration file.
106
107   Create the **sample/hello/bundle.json** file and add the **sample** component description therein. For details, see [Component](../subsystems/subsys-build-component.md).
108
109   The content of the **bundle.json** file is as follows:
110
111   ```
112   {
113       "name": "@ohos/hello",
114       "description": "Hello world example.",
115       "version": "3.1",
116       "license": "Apache License 2.0",
117       "publishAs": "code-segment",
118       "segment": {
119           "destPath": "sample/hello"
120       },
121       "dirs": {},
122       "scripts": {},
123       "component": {
124           "name": "hello",
125           "subsystem": "sample",
126           "syscap": [],
127           "features": [],
128           "adapted_system_type": [ "mini", "small", "standard" ],
129           "rom": "10KB",
130           "ram": "10KB",
131           "deps": {
132               "components": [],
133               "third_party": []
134           },
135           "build": {
136               "sub_component": [
137                   "//sample/hello:helloworld"
138               ],
139               "inner_kits": [],
140               "test": []
141           }
142       }
143   }
144   ```
145
146   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 build configuration for the component. When adding a component, you must specify the **sub_component** of the component. Add the APIs provided for other components, if any, in **inner_kits**. Add the test cases, if any, in **test**.
147
1484. Modify the subsystem configuration file.
149
150   Add the configuration of the new subsystem to the **build/subsystem_config.json** file. For details, see [Subsystem](../subsystems/subsys-build-subsystem.md).
151
152   The configuration of the new subsystem is as follows:
153
154   ```
155   "sample": {
156       "path": "sample",
157       "name": "sample"
158     },
159   ```
160
1615. Modify the product configuration file.
162
163      > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
164      >
165      > In versions earlier than OpenHarmony-v3.2-Beta2, the RK3568 configuration file is **productdefine/common/products/rk3568.json**. In OpenHarmony-v3.2-Beta2 and later versions, the RK3568 configuration file is **vendor/hihope/rk3568/config.json**.
166
167   - Versions earlier than OpenHarmony-v3.2-Beta2
168
169      In the **productdefine/common/products/rk3568.json** file, add the **hello** part after the existing part.
170
171       ```
172       "usb:usb_manager_native":{},
173       "applications:prebuilt_hap":{},
174       "sample:hello":{},
175       "wpa_supplicant-2.9:wpa_supplicant-2.9":{},
176       ```
177
178   - OpenHarmony-v3.2-Beta2 and later versions
179
180
181     In the **vendor/hihope/rk3568/config.json** file, add the **hello** part after the existing part.
182
183         ```
184         {
185           "subsystem": "sample",
186           "components": [
187             {
188               "component": "hello",
189               "features": []
190             }
191           ]
192         },
193         ```
194