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