• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Running a Hello World Program<a name="EN-US_TOPIC_0000001128311062"></a>
2
3-   [Modifying Source Code](#section79601457101015)
4-   [Debugging and Verification](#section1621064881419)
5    -   [printf](#section5204547123316)
6    -   [Locating Exceptions Using the ASM File](#section15919111423416)
7
8-   [Viewing Execution Result](#section18115713118)
9-   [Follow-up Learning](#section9712145420182)
10
11This example shows how to compile a simple service and print  **Hello World**  to help you preliminarily understand how to run OpenHarmony on Hi3861.
12
13## Modifying Source Code<a name="section79601457101015"></a>
14
15The source code needs to be modified when fixing bugs or compiling a new service. The following describes how to modify the source code when compiling a new service, for example,  **my\_first\_app**.
16
171.  Determine the directory structure.
18
19    Before compiling a service, you must create a directory \(or a directory structure\) in  **./applications/sample/wifi-iot/app**  to store source code files.
20
21    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:
22
23    ```
24    .
25    └── applications
26        └── sample
27            └── wifi-iot
28                └── app
29                    │── my_first_app
30                    │  │── hello_world.c
31                    │  └── BUILD.gn
32                    └── BUILD.gn
33    ```
34
352.  Compile the service code.
36
37    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. Use the SYS\_RUN\(\) of the OpenHarmony  **bootstrap**  module to start services. \(**SYS\_RUN**  is defined in the  **ohos\_init.h**  file.\)
38
39    ```
40    #include <stdio.h>
41    #include "ohos_init.h"
42    #include "ohos_types.h"
43
44    void HelloWorld(void)
45    {
46        printf("[DEMO] Hello world.\n");
47    }
48    SYS_RUN(HelloWorld);
49    ```
50
513.  Compile the  **BUILD.gn**  file for building services into a static library.
52
53    Create the  **BUILD.gn**  file in  **./applications/sample/wifi-iot/app/my\_first\_app**  and fill in three parts \(target, source file, and header file path\) of the  **BUILD.gn**  file.
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\), otherwise it is a relative path.
68    -   Specify the path of .h file on which  **sources**  depends in  **include\_dirs**.
69
704.  Compile the  **BUILD.gn**  file and specify the feature modules to be built.
71
72    Configure the  **./applications/sample/wifi-iot/app/BUILD.gn**  file and add an index to the  **features**  field to enable the target to be involved in compilation. Specify the path and target of a service module in  **features**. The following uses  **my\_first\_app**  as an example and the  **features**  is configured as follows:
73
74    ```
75    import("//build/lite/config/component/lite_component.gni")
76
77    lite_component("app") {
78        features = [
79            "my_first_app:myapp",
80        ]
81    }
82    ```
83
84    -   **my\_first\_app**  is a relative path that points to  **./applications/sample/wifi-iot/app/my\_first\_app/BUILD.gn**.
85    -   **myapp**  represents the  **static\_library\("myapp"\)**  in  **./applications/sample/wifi-iot/app/my\_first\_app/BUILD.gn**.
86
87
88## Debugging and Verification<a name="section1621064881419"></a>
89
90Currently, there are two debugging and verification methods: using printf to print logs and using ASM files to locate  **panic**  problems. You can select one of them based on your need.
91
92As the service shown is simple, use the printf method. The following describes the two debugging methods.
93
94### printf<a name="section5204547123316"></a>
95
96Add the  **printf**  function to the code, which helps print data to the serial port. You can add log printing in key service paths or service exception locations, as shown in the following figure.
97
98```
99void HelloWorld(void)
100{
101    printf("[DEMO] Hello world.\n");
102}
103```
104
105### Locating Exceptions Using the ASM File<a name="section15919111423416"></a>
106
107When the system exits abnormally, the call stack information about the abnormal exit is displayed on the serial port. The following is an example: You can locate the exception by parsing the exception stack information.
108
109```
110=======KERNEL PANIC=======
111**********************Call Stack*********************
112Call Stack 0 -- 4860d8 addr:f784c
113Call Stack 1 -- 47b2b2 addr:f788c
114Call Stack 2 -- 3e562c addr:f789c
115Call Stack 3 -- 4101de addr:f78ac
116Call Stack 4 -- 3e5f32 addr:f78cc
117Call Stack 5 -- 3f78c0 addr:f78ec
118Call Stack 6 -- 3f5e24 addr:f78fc
119********************Call Stack end*******************
120```
121
122To parse the call stack information, the  **Hi3861\_wifiiot\_app.asm**  file is required. This file records the symbol addresses of the functions in the code in the flash memory and the disassembly information. The ASM file is built and output together with the version software package and is stored in the  **./out/wifiiot/**  directory.
123
1241.  \(Optional\) Save the call stack information to a TXT file for later editing.
1252.  Open the ASM file, search for the function address in each call stack, and list the corresponding function. Generally, you only need to find the functions matching the first several stacks to locate exceptions.
126
127    ```
128    Call Stack 0 -- 4860d8 addr:f784c -- WadRecvCB
129    Call Stack 1 -- 47b2b2 addr:f788c -- wal_sdp_process_rx_data
130    Call Stack 2 -- 3e562c addr:f789c
131    Call Stack 3 -- 4101de addr:f78ac
132    Call Stack 4 -- 3e5f32 addr:f78cc
133    Call Stack 5 -- 3f78c0 addr:f78ec
134    Call Stack 6 -- 3f5e24 addr:f78fc
135    ```
136
1373.  Based on the above call stack information, it can be determined that an exception occurs in the  **WadRecvCB**  function.
138
139    ![](figure/wadrecvcb-function.png)
140
1414.  Check and modify the code.
142
143## Viewing Execution Result<a name="section18115713118"></a>
144
145After the sample code is compiled, burnt, run, and debugged, the following information is displayed on the serial port interface:
146
147```
148ready to OS start
149FileSystem mount ok.
150wifi init success!
151[DEMO] Hello world.
152```
153
154## Follow-up Learning<a name="section9712145420182"></a>
155
156Congratulations! You have finished all steps! You are advised to go on learning how to develop  [WLAN-connected products](../guide/device-wifi.md).
157
158