harmony(鸿蒙)Writing a Hello World Program
Writing a Hello World Program
The 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.
- Determine the directory structure.
Before compiling a service, you must create a directory (or a directory structure) in ./applications/sample/wifi-iot/app to store source code files.
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:
.
└── applications
└── sample
└── wifi-iot
└── app
└── my_first_app
│── hello_world.c
└── BUILD.gn
Write the service code.
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.)
#include <stdio.h>
#include "ohos_init.h"
#include "ohos_types.h"
void HelloWorld(void)
{
printf("[DEMO] Hello world.\n");
}
SYS_RUN(HelloWorld);
- Compile the BUILD.gn file for building services into a static library.
Create the BUILD.gn file in ./applications/sample/wifi-iot/app/my_first_app and configure the file as follows:
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.
static_library("myapp") {
sources = [
"hello_world.c"
]
include_dirs = [
"//utils/native/lite/include"
]
}
Specify the compilation result named libmyapp.a in static_library. You can fill in this part based on your need.
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.
Specify the path of .h file on which sources depends in include_dirs.
Add a component.
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: The configuration between ##start## and ##end## is new. (##start## and ##end## are only used to identify the location. After the configuration is complete, delete these lines.)
{
"components": [
{
"component": "camera_sample_communication",
"description": "Communication related samples.",
"optional": "true",
"dirs": [
"applications/sample/camera/communication"
],
"targets": [
"//applications/sample/camera/communication:sample"
],
"rom": "",
"ram": "",
"output": [],
"adapted_kernel": [ "liteos_a" ],
"features": [],
"deps": {
"components": [],
"third_party": []
}
},
##start##
{
"component": "hello_world_app",
"description": "hello world samples.",
"optional": "true",
"dirs": [
"applications/sample/wifi-iot/app/my_first_app"
],
"targets": [
"//applications/sample/wifi-iot/app/my_first_app:myapp"
],
"rom": "",
"ram": "",
"output": [],
"adapted_kernel": [ "liteos_m" ],
"features": [],
"deps": {
"components": [],
"third_party": []
}
},
##end##
{
"component": "camera_sample_app",
"description": "Camera related samples.",
"optional": "true",
"dirs": [
"applications/sample/camera/launcher",
"applications/sample/camera/cameraApp",
"applications/sample/camera/setting",
"applications/sample/camera/gallery",
"applications/sample/camera/media"
],
- Modify the board configuration file.
Modify the vendor/hisilicon/hispark_pegasus/config.json file and add an entry of the hello_world_app component. The following code snippet is the configuration of the applications subsystem, the configuration between ##start## and ##end## is new. (##start## and ##end## are only used to identify the location. After the configuration is complete, delete these lines.)
{
"subsystem": "applications",
"components": [
##start##
{ "component": "hello_world_app", "features":[] },
##end##
{ "component": "wifi_iot_sample_app", "features":[] }
]
},
你可能感兴趣的鸿蒙文章
harmony(鸿蒙)Overall Description of Compilation Form Factors
harmony(鸿蒙)Importing a Source Code Project
harmony(鸿蒙)Setting Up the Windows+Ubuntu Hybrid Development Environment
harmony(鸿蒙)Introduction to the Hi3516 Development Board
harmony(鸿蒙)Introduction to the Hi3861 Development Board
harmony(鸿蒙)Mini and Small System Overview
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦