1e41f4b71Sopenharmony_ci# Writing a Hello World Program 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciThe 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. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Prerequisites 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciA project for the Hi3861 development board has been created as instructed in [Creating a Project and Obtaining Source Code](quickstart-ide-import-project.md). 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## Procedure 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci1. Determine the directory structure. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci Before writing service code, you must create a directory (or a directory structure) in **./applications/sample/wifi-iot/app** to store source code files. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci 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: 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci ``` 22e41f4b71Sopenharmony_ci . 23e41f4b71Sopenharmony_ci └── applications 24e41f4b71Sopenharmony_ci └── sample 25e41f4b71Sopenharmony_ci └── wifi-iot 26e41f4b71Sopenharmony_ci └── app 27e41f4b71Sopenharmony_ci └── my_first_app 28e41f4b71Sopenharmony_ci │── hello_world.c 29e41f4b71Sopenharmony_ci └── BUILD.gn 30e41f4b71Sopenharmony_ci ``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci2. Write the service code. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci 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.) 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci ``` 37e41f4b71Sopenharmony_ci #include <stdio.h> 38e41f4b71Sopenharmony_ci #include "ohos_init.h" 39e41f4b71Sopenharmony_ci #include "ohos_types.h" 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci void HelloWorld(void) 42e41f4b71Sopenharmony_ci { 43e41f4b71Sopenharmony_ci printf("[DEMO] Hello world.\n"); 44e41f4b71Sopenharmony_ci } 45e41f4b71Sopenharmony_ci SYS_RUN(HelloWorld); 46e41f4b71Sopenharmony_ci ``` 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci3. Compile the **BUILD.gn** file for building services into a static library. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci Create the **BUILD.gn** file in **./applications/sample/wifi-iot/app/my_first_app** and configure the file as follows: 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci 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. 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci ``` 56e41f4b71Sopenharmony_ci static_library("myapp") { 57e41f4b71Sopenharmony_ci sources = [ 58e41f4b71Sopenharmony_ci "hello_world.c" 59e41f4b71Sopenharmony_ci ] 60e41f4b71Sopenharmony_ci include_dirs = [ 61e41f4b71Sopenharmony_ci "//utils/native/lite/include" 62e41f4b71Sopenharmony_ci ] 63e41f4b71Sopenharmony_ci } 64e41f4b71Sopenharmony_ci ``` 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci - Specify the compilation result named **libmyapp.a** in **static_library**. You can fill in this part based on your need. 67e41f4b71Sopenharmony_ci - 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. 68e41f4b71Sopenharmony_ci - Specify the path of .h file on which **sources** depends in **include_dirs**. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci4. Add a component. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci 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.) 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci >  **NOTE** 75e41f4b71Sopenharmony_ci > 76e41f4b71Sopenharmony_ci > 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**. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci ``` 80e41f4b71Sopenharmony_ci { 81e41f4b71Sopenharmony_ci "components": [ 82e41f4b71Sopenharmony_ci { 83e41f4b71Sopenharmony_ci "component": "camera_sample_communication", 84e41f4b71Sopenharmony_ci "description": "Communication related samples.", 85e41f4b71Sopenharmony_ci "optional": "true", 86e41f4b71Sopenharmony_ci "dirs": [ 87e41f4b71Sopenharmony_ci "applications/sample/camera/communication" 88e41f4b71Sopenharmony_ci ], 89e41f4b71Sopenharmony_ci "targets": [ 90e41f4b71Sopenharmony_ci "//applications/sample/camera/communication:sample" 91e41f4b71Sopenharmony_ci ], 92e41f4b71Sopenharmony_ci "rom": "", 93e41f4b71Sopenharmony_ci "ram": "", 94e41f4b71Sopenharmony_ci "output": [], 95e41f4b71Sopenharmony_ci "adapted_kernel": [ "liteos_a" ], 96e41f4b71Sopenharmony_ci "features": [], 97e41f4b71Sopenharmony_ci "deps": { 98e41f4b71Sopenharmony_ci "components": [], 99e41f4b71Sopenharmony_ci "third_party": [] 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci }, 102e41f4b71Sopenharmony_ci ##start## 103e41f4b71Sopenharmony_ci { 104e41f4b71Sopenharmony_ci "component": "hello_world_app", 105e41f4b71Sopenharmony_ci "description": "hello world samples.", 106e41f4b71Sopenharmony_ci "optional": "true", 107e41f4b71Sopenharmony_ci "dirs": [ 108e41f4b71Sopenharmony_ci "applications/sample/wifi-iot/app/my_first_app" 109e41f4b71Sopenharmony_ci ], 110e41f4b71Sopenharmony_ci "targets": [ 111e41f4b71Sopenharmony_ci "//applications/sample/wifi-iot/app/my_first_app:myapp" 112e41f4b71Sopenharmony_ci ], 113e41f4b71Sopenharmony_ci "rom": "", 114e41f4b71Sopenharmony_ci "ram": "", 115e41f4b71Sopenharmony_ci "output": [], 116e41f4b71Sopenharmony_ci "adapted_kernel": [ "liteos_m" ], 117e41f4b71Sopenharmony_ci "features": [], 118e41f4b71Sopenharmony_ci "deps": { 119e41f4b71Sopenharmony_ci "components": [], 120e41f4b71Sopenharmony_ci "third_party": [] 121e41f4b71Sopenharmony_ci } 122e41f4b71Sopenharmony_ci }, 123e41f4b71Sopenharmony_ci ##end## 124e41f4b71Sopenharmony_ci { 125e41f4b71Sopenharmony_ci "component": "camera_sample_app", 126e41f4b71Sopenharmony_ci "description": "Camera related samples.", 127e41f4b71Sopenharmony_ci "optional": "true", 128e41f4b71Sopenharmony_ci "dirs": [ 129e41f4b71Sopenharmony_ci "applications/sample/camera/launcher", 130e41f4b71Sopenharmony_ci "applications/sample/camera/cameraApp", 131e41f4b71Sopenharmony_ci "applications/sample/camera/setting", 132e41f4b71Sopenharmony_ci "applications/sample/camera/gallery", 133e41f4b71Sopenharmony_ci "applications/sample/camera/media" 134e41f4b71Sopenharmony_ci ], 135e41f4b71Sopenharmony_ci ``` 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci5. Modify the board configuration file. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci 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.) 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci ``` 143e41f4b71Sopenharmony_ci { 144e41f4b71Sopenharmony_ci "subsystem": "applications", 145e41f4b71Sopenharmony_ci "components": [ 146e41f4b71Sopenharmony_ci ##start## 147e41f4b71Sopenharmony_ci { "component": "hello_world_app", "features":[] }, 148e41f4b71Sopenharmony_ci ##end## 149e41f4b71Sopenharmony_ci { "component": "wifi_iot_sample_app", "features":[] } 150e41f4b71Sopenharmony_ci ] 151e41f4b71Sopenharmony_ci }, 152e41f4b71Sopenharmony_ci ``` 153