harmony(鸿蒙)Writing a Hello World Program

  • 2022-08-09
  • 浏览 (568)

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.

  1. 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
  1. 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);
  1. 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"
         ],
  1. 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(鸿蒙)Getting Started

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

harmony(鸿蒙)Obtaining Source Code

harmony(鸿蒙)Building

harmony(鸿蒙)Burning

0  赞