#跟着小白一起学鸿蒙# [二]第一个OpenHarmony程序 原创 精华

深开鸿
发布于 2022-8-26 16:18
浏览
4收藏

作者:王石,胡瑞涛

上节回顾

在#跟着小白一起学鸿蒙# [一]运行OpenHarmony章节我们学习了如何下载,编译,运行OpenHarmony,接下来我们来熟悉下OpenHarmony的编译框架和如果自己开发一个HelloWord程序。

简介

OpenHarmony的编译过程分析

graph LR
build.sh --> build.py --> hb --> gn
hb --> ninja
hb --> clang

#跟着小白一起学鸿蒙# [二]第一个OpenHarmony程序-鸿蒙开发者社区

名词解释

  • gn: generate ninja工具,在out目录下生成ninja编译文件*.ninja,文件位置在prebuilts/build-tools/linux-x86目录里

    $ ./gn --help
    Commands (type "gn help <command>" for more help):
      analyze: Analyze which targets are affected by a list of files.
      args: Display or configure arguments declared by the build.
      check: Check header dependencies.
      clean: Cleans the output directory.
      desc: Show lots of insightful information about a target or config.
      format: Format .gn files.
      gen: Generate ninja files.
      help: Does what you think.
      ls: List matching targets.
      meta: List target metadata collection results.
      path: Find paths between two targets.
      refs: Find stuff referencing a target or file.
    ……
    
  • ninja:构建工具,根据gn生成的*.ninja文件进行编译构建,文件位置在prebuilts/build-tools/linux-x86目录里

    ./ninja --help
    usage: ninja [options] [targets...]
    if targets are unspecified, builds the 'default' target (see manual).
    options:
      --version      print ninja version ("1.10.1")
      -v, --verbose  show all command lines while building
    
      -C DIR   change to DIR before doing anything else
      -f FILE  specify input build file [default=build.ninja]
    
      -j N     run N jobs in parallel (0 means infinity) [default=10 on this system]
      -k N     keep going until N jobs fail (0 means infinity) [default=1]
      -l N     do not start new jobs if the load average is greater than N
      -n       dry run (don't run commands but act like they succeeded)
    
      -d MODE  enable debugging (use '-d list' to list modes)
      -t TOOL  run a subtool (use '-t list' to list subtools)
        terminates toplevel options; further flags are passed to the tool
      -w FLAG  adjust warnings (use '-w list' to list warnings)
    
  • clang:编译器,同gcc兼容

1. 查看.gn并增加应用输出

路径为:oh32/third_party/libuv/BUILD.gn,找到如下位置增加

//静态库
ohos_static_library("uv_static") {
    deps = [ ":libuv_source" ]
        public_configs = [ ":libuv_config" ]
        subsystem_name = "thirdparty"
        part_name = "libuv"
}
//动态库
ohos_shared_library("uv") {
    deps = [ ":libuv_source" ]
        public_configs = [ ":libuv_config" ]
        subsystem_name = "thirdparty"
        part_name = "libuv"
        if (is_ohos) {
            output_extension = "so"
        }
    install_images = [
        "system",
        "updater",
    ]
}
//增加的新应用
ohos_executable("helloworld") {
  sources = [ "helloword.c",]
}
2. 增加helloworld.c

在oh32/third_party/libuv目录下增加helloworld.c

3. 增加helloworld编译生成的入口
  • 在test/xts/acts/graphic/BUILD.gn里增加deps,如下

    import("//build/ohos_var.gni")
    group("graphic") {
      testonly = true
      if (is_standard_system) {
        deps = [
          "webGL:webGL_hap_test",
          "windowStage:ActsWindowStageTest",
          "windowstandard:window_hap_test",
          "//third_party/libuv:helloworld", #引入helloworld程序编译
        ]
      } else {
        deps = [
          "appaccount:appaccount_hap",
          "osaccount:osaccount_hap",
        ]
      }
    }
    
    
  • 源码根目录执行:

    ./build.sh --product-name rk3568
    
  • 源码根目录执行:

    ./build.sh --product-name rk3568 --gn-args build_xts=true --build-target "acts" --gn-args is_standard_system=true
    
    
5. 编译完成后生成的第三方库验证程序helloworld使用流程
  • runtest库文件会输出helloworld,路径为:out/rk3568/common/common

    ~/oh32/out/rk3568/common/common$ ls
    libc  libimagePixelmap.so  libnativerender.so  libsqlite.z.so  libteststring.so  libusb_shared.z.so  helloworld
    
  • 使用方式如下:

    1. 拷贝helloworld到板子上
    hdc_std.exe file send helloworld /data/local/tmp
    //2. 登录板子,运行helloworld,如果第一次需要给权限
    pc端:hdc_std.exe shell
    板子:cd /data/local/tmp
    板子:chmod +x helloworld
    板子:./helloworld
    
  • 结果

    #./helloworld
    
    hello world!#
    

    更多原创内容请关注:深开鸿技术团队

    入门到精通、技巧到案例,系统化分享HarmonyOS开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2022-8-26 16:18:05修改
2
收藏 4
回复
举报
2条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

第二节来了,顺便贴一下第一节的链接:https://ost.51cto.com/posts/16203

回复
2022-8-26 16:35:39
带带小老弟
带带小老弟

万物皆从HelloWord开始学习

回复
2022-8-29 14:13:14
回复
    相关推荐