• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Writing a Hello World Program
2
3
4The following exemplifies how to create a program by modifying the source code. The created program outputs the message "Hello world." Perform the steps below in the source code directory.
5
6
7## Prerequisites
8
9A project for the Hi3861 development board has been created as instructed in [Creating a Project and Obtaining Source Code](quickstart-ide-import-project.md).
10
11
12## Procedure
13
141. Determine the directory structure.
15
16   Before writing service code, you must create a directory (or a directory structure) in **./applications/sample/wifi-iot/app** to store source code files.
17
18   For example, to add the **my_first_app** service to the **app** directory, where the **hello_world.c** file stores the service code and **BUILD.gn** is the compilation script, the directory structure can be planned as follows:
19
20
21   ```
22   .
23   └── applications
24       └── sample
25           └── wifi-iot
26               └── app
27                   └── my_first_app
28                     │── hello_world.c
29                     └── BUILD.gn
30   ```
31
322. Write the service code.
33
34   Create the **hello_world.c** file in **./applications/sample/wifi-iot/app/my_first_app**. Then, create the entry point function **HelloWorld** in **hello_world.c** and implement service logic. Call **SYS_RUN()** of OpenHarmony to start the service. (**SYS_RUN** is defined in the **ohos_init.h** file.)
35
36   ```
37   #include <stdio.h>
38   #include "ohos_init.h"
39   #include "ohos_types.h"
40
41   void HelloWorld(void)
42   {
43       printf("[DEMO] Hello world.\n");
44   }
45   SYS_RUN(HelloWorld);
46   ```
47
483. Compile the **BUILD.gn** file for building services into a static library.
49
50   Create the **BUILD.gn** file in **./applications/sample/wifi-iot/app/my_first_app** and configure the file as follows:
51
52   The **BUILD.gn** file consists of three parts, including target, source file, and header file path. You need to fill in all of these parts.
53
54
55   ```
56   static_library("myapp") {
57       sources = [
58           "hello_world.c"
59       ]
60       include_dirs = [
61           "//utils/native/lite/include"
62       ]
63   }
64   ```
65
66   - Specify the compilation result named **libmyapp.a** in **static_library**. You can fill in this part based on your need.
67   - Specify the .c file on which a file depends and its path in **sources**. The path that contains **//** represents an absolute path (the code root path). The path that does not contain **//** is a relative path.
68   - Specify the path of .h file on which **sources** depends in **include_dirs**.
69
704. Add a component.
71
72   Modify the **build/lite/components/applications.json** file and add the configuration of **hello_world_app**. The following code snippet is a snippet of the **applications.json** file, where the configuration between **\#\#start\#\#** and **\#\#end\#\#** is the new entry. (The **\#\#start\#\#** and **\#\#end\#\#** lines are only used to identify the location. After the configuration is complete, delete these lines.)
73
74   > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
75   >
76   > In this example, the OpenHarmony-v3.1-Release version is used, where the component configuration file is **build/lite/components/applications.json**. In OpenHarmony-v3.2-Beta2 and later versions, the component configuration file is **build/lite/components/communication.json**.
77
78
79   ```
80   {
81     "components": [
82       {
83         "component": "camera_sample_communication",
84         "description": "Communication related samples.",
85         "optional": "true",
86         "dirs": [
87           "applications/sample/camera/communication"
88         ],
89         "targets": [
90           "//applications/sample/camera/communication:sample"
91         ],
92         "rom": "",
93         "ram": "",
94         "output": [],
95         "adapted_kernel": [ "liteos_a" ],
96         "features": [],
97         "deps": {
98           "components": [],
99           "third_party": []
100         }
101       },
102   ##start##
103       {
104         "component": "hello_world_app",
105         "description": "hello world samples.",
106         "optional": "true",
107         "dirs": [
108           "applications/sample/wifi-iot/app/my_first_app"
109         ],
110         "targets": [
111           "//applications/sample/wifi-iot/app/my_first_app:myapp"
112         ],
113         "rom": "",
114         "ram": "",
115         "output": [],
116         "adapted_kernel": [ "liteos_m" ],
117         "features": [],
118         "deps": {
119           "components": [],
120           "third_party": []
121         }
122       },
123   ##end##
124       {
125         "component": "camera_sample_app",
126         "description": "Camera related samples.",
127         "optional": "true",
128         "dirs": [
129           "applications/sample/camera/launcher",
130           "applications/sample/camera/cameraApp",
131           "applications/sample/camera/setting",
132           "applications/sample/camera/gallery",
133           "applications/sample/camera/media"
134         ],
135   ```
136
1375. Modify the board configuration file.
138
139   Modify the **vendor/hisilicon/hispark_pegasus/config.json** file and add an entry for the **hello_world_app** component. The following code snippet is the configuration of the **applications** subsystem, where the configuration between **\#\#start\#\#** and **\#\#end\#\#** is the new entry. (The **\#\#start\#\#** and **\#\#end\#\#** lines are only used to identify the location. After the configuration is complete, delete these lines.)
140
141
142   ```
143         {
144           "subsystem": "applications",
145           "components": [
146   ##start##
147             { "component": "hello_world_app", "features":[] },
148   ##end##
149             { "component": "wifi_iot_sample_app", "features":[] }
150           ]
151         },
152   ```
153