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