【Openharmony】【4.0R】hello程序之“ohos.gni”模板使用方法

【Openharmony】【4.0R】hello程序之“ohos.gni”模板使用方法

lite_component.gni虽然好用,但是毕竟是个“轻量-组件”模板。
这里记录下较为常用的“ohos.gni”模板使用方法。
OH版本:4.0Release
内核版本:LiteOS-A
产品版本:qemu_small_system_demo

ohos.gni模板

相比lite_component.gni模板,ohos.gni模板要复杂多了,关于该模板教程也很多。
首先依然是要先import才能使用模板方法

import("//build/ohos.gni")

在Openharmony看ohos.gni会发现,这个模板实际上是一堆模板通过import结合起来的,内容还是较多的,但是我们要用的实际在import(“//build/templates/cxx/cxx.gni”)这里。

//ohos.gni
import("//build/config/sanitizers/sanitizers.gni")
import("//build/ohos/ndk/ndk.gni")
import("//build/ohos/notice/notice.gni")
import("//build/ohos/sa_profile/sa_profile.gni")
import("//build/ohos_var.gni")
import("//build/toolchain/toolchain.gni")

# import cxx base templates
import("//build/templates/cxx/cxx.gni")
if (support_jsapi) {
  import("//build/ohos/ace/ace.gni")
  import("//build/ohos/app/app.gni")
}

import("//build/templates/common/ohos_templates.gni")

# import prebuilt templates
import("//build/templates/cxx/prebuilt.gni")
if (build_cross_platform_version) {
  import("//build_plugins/templates/java/rules.gni")
} else {
  import("//build/templates/bpf/ohos_bpf.gni")
  import("//build/templates/rust/ohos_cargo_crate.gni")
  import("//build/templates/rust/rust_bindgen.gni")
  import("//build/templates/rust/rust_cxx.gni")
  import("//build/templates/rust/rust_template.gni")
}

import("//build/templates/idl/ohos_idl.gni")

//build/templates/cxx/cxx.gni

在cxx.gni里面,才真正定义了我们要使用的模板,有兴趣的可以仔细阅读cxx.gni内容

template(“ohos_executable”)

可以看到ohos_executable内部使用的是executable,在lite_component.gni那个例子中用的也是executable生成可执行文件。如果熟悉gn,应该知道executable是gn原本就支持的功能,这里ohos_executable就是对executable进行了封装。

template("ohos_executable") {
...
  executable("${target_name}") {
  ...
  }
}
template(“ohos_shared_library”)

同上,ohos_shared_library对shared_library进行了封装。

template("ohos_shared_library") {
...
  shared_library("${target_name}") {
  ...
  }
}
template(“ohos_static_library”)
template("ohos_static_library") {
...
  static_library(target_name) {
  ...
  }
}
template(“ohos_source_set”)
template("ohos_source_set") {
...
  source_set(target_name) {
  ...
  }
}

实际上使用ohos_executable和ohos_shared_library就够了。

准备工作

使用ohos.gni模板需要提供两部分信息:子系统,部件。为了和原有子系统、部件区分,针对hello程序可以新建单独的子系统和部件。

子系统

在"build/subsystem_config.json"中新加一个“hellotestsystem”子系统

   "hellotestsystem":{
    "path":"testhello",
    "name":"hellotestsystem"
  },

部件

在项目目录中新建bundle.json
文件结构:

testhello/
└── hello_ohos
    ├── BUILD.gn
    ├── bundle.json
    ├── include
    │   └── hello.h
    ├── src
    │   └── hello.cpp
    └── test
        └── test_hello.cpp

bundle.json内容如下,设置名为hellotestpart的部件
重点看“component”:的内容,
name 部件名
subsystem 部件所属子系统
deps 部件依赖
sub_componrnt 部件中组件编译入口

{
    "name": "@ohos/hellotestpart",
    "description": "Hello example.",
    "version": "4.0",
    "license": "Apache License 2.0",
    "publishAs": "code-segment",
    "segment": {
        "destPath": "testhello/hello_ohos"
    },
    "dirs": {},
    "scripts": {},
    "component": {
        "name": "hellotestpart",
        "subsystem": "hellotestsystem",
        "syscap": [],
        "features": [],
        "adapted_system_type": [ "mini", "small", "standard" ],
        "rom": "10KB",
        "ram": "10KB",
        "deps": {
            "components": [],
            "third_party": []
        },
        "build": {
            "sub_component": [
                "//testhello/hello_ohos:test_hello"
            ],
            "inner_kits": [],
            "test": []
        }
    }
}

添加编译选项

在"vendor/ohemu/qemu_small_system_demo/config.json"中将子系统和部件添加到编译选项里

      {
        "subsystem": "hellotestsystem",
        "components": [
          { "component": "hellotestpart", "features":[] }
        ]
      },

头文件

//hello.h
#include <iostream>
void helloTest();

动态库源文件

//hello.cpp
#include "hello.h"
void helloTest()
{
    std::cout << "Hello, lyxqg, this is ohos!" << std::endl;
    return;
}

主函数源文件

//test_hello.cpp
#include "hello.h"
int main()
{
    helloTest();
    return 0;
}

BUILD.gn

import("//build/ohos.gni")

libsources = [
    "src/hello.cpp",
]

config("testhelloliteinclude")
{
    include_dirs = [
        "include"
    ]
    cflags = [ "-Wall" ]
    cflags_cc = cflags
    ldflags = [ "-Wl,-rpath-link=$ohos_root_path/$root_out_dir" ]
}

ohos_shared_library("hellotest")
{
    sources = libsources
    configs = [ ":testhelloliteinclude" ]
    output_name = "hellotestv2.0"
    part_name = "hellotestpart"
    subsystem_name = "hellotestsystem"
}

exesources = [
    "test/test_hello.cpp"
]

ohos_executable("test_hello") {
  sources = exesources
  configs = [":testhelloliteinclude"]
  deps = [
    ":hellotest",
  ]
  output_name = "test_hello"
  part_name = "hellotestpart"
  subsystem_name = "hellotestsystem"
}

注意事项

1.ohos.gni模板内禁止修改output_dirs,所以不要在BUILD.gn中设置output_dirs选项。
2.使用ohos.gni模板时可以使用deps去依赖lite_component模板中的库,测试发现lite_component里也不要写output_dirs,不然编译虽然通过,但是进入系统执行命令发现找不到库文件。

最后
在这里插入图片描述

  • 37
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值