How to compile and run HelloWorld on Openharmony

Openharmony's first application--Hello world

Everyone understands the C++ code of Hello World

// FirstApp.cpp
#include <iostream>
int main(int argc, char *argv[])
{
    std::cout << "Hello Openharmony!" << std::endl;
    return 0;
}

The question is, how to add FirstApp.cpp to Openharmony, compile it, and then run it? This process involves the subsystem of Openharmony, the compilation and construction subsystem . The overall complex process of compilation and construction is not listed. According to the principle of "code can run", the following process is operated step by step, and the reason why is explained in detail in the following chapters .

Introduction to development environment

Code version: Openharmon V3.1-Release

Development board: rk3568

1. First place the FirstApp.cpp file in the /base/account directory of Openharmon V3.1-Release.

2. Create the /base/account/BUILD.gn file with the following content

# HelloOpenHarmony为可执行程序名称
# ohos_executable为Openharmony编译可执行程序的模板
ohos_executable("HelloOpenHarmony") {
  sources = [
    "FirstApp.cpp",
  ]
}

3. Modify /base/account/bundle.json as follows

Add "//base/account/os_account:HelloOpenHarmony" in sub_component

"sub_component": [
  "//base/account/os_account/services:services_target",
  "//base/account/os_account/services/accountmgr/src/appaccount:app_account_service_core",
  "//base/account/os_account/frameworks/appaccount/native:app_account_innerkits",
  "//base/account/os_account/frameworks/osaccount/core:os_account_core",
  "//base/account/os_account/frameworks/common:common_target",
  "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits",
  "//base/account/os_account/interfaces/kits/napi/appaccount:appaccount",
  "//base/account/os_account/interfaces/kits/napi/distributedaccount:distributedaccount",
  "//base/account/os_account/interfaces/kits/napi/osaccount:osaccount",
  "//base/account/os_account/sa_profile:account_sa_profile",
  "//base/account/os_account/tools:os_account_tools",
  "//base/account/os_account:HelloOpenHarmony"
],

4.Compile

# 此为完整编译,也可以选择子组件进行单独编译,加上-T HelloOpenHarmony指定模块进行编译也可以生成HelloOpenHarmony的可执行程序
./build.sh --product-name rk3568
./build.sh --product-name rk3568 -T HelloOpenHarmony

5. Run

After the compilation is successful, the executable program of HelloOpenHarmony can be found in the /out/rk3568/common/common/ directory. At this time, push HelloOpenHarmony to the /system/bin directory of the development board, and modify its executable permission through chmod. The result is as follows:

# 通过hdc_std推送至开发板的过程较易,略过(如有疑问,欢迎留言)
# 只展示在hdc_std shell进入开发板之后的操作与结果
# chmod 777 /system/bin/HelloOpenHarmony
# ls -ali /system/bin/HelloOpenHarmony
2297 -rwxrwxrwx 1 root root 25300 2020-01-15 09:29 /system/bin/HelloOpenHarmony
# ./system/bin/HelloOpenHarmony
Hello Openharmony!

At this point, the first executable program running on Openharmony is completed.

A brief description of the Openharmony program compilation and build process

1.About bundle.json

One or more bundle.json forms a subsystem of Openharmony. In this example, HelloOpenHarmony is added as a component to the account subsystem (account) to participate in compilation.

2.About BUILD.gn

BUILD.gn can be seen as a component's makefile.

3. In summary, the HelloOpenHarmony compilation and construction process can now be understood as firstly when the ./build.sh --product-name rk3568 command is executed, the bundle.json of the subsystem will be found, and then the bundle.json will be based on the sub_component component list, Find the BUILD.gn of the component, and then the cross-compilation tool compiles the template (ohos_executable) defined by BUILD.gn.

4.Attention _

  • The above compilation and build process is based on versions before Openharmony V3.1. The process of versions after Openharmony V3.2 is slightly different.

  • The above is just a brief description, and the detailed compilation and construction process. If you want to know more, there will be a separate chapter to describe it.

follow-up more exciting

1. About the past and present life of service ability

2.Openharmony’s distributed soft bus

3. The difficulty of subsequent articles will increase. Everyone is welcome to leave a message and give feedback.

Guess you like

Origin blog.csdn.net/procedurecode/article/details/128734065