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

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

记录使用“lite_component.gni”模板编译hello程序
OH版本:4.0Release
内核版本:LiteOS-A
产品版本:qemu_small_system_demo

lite_component.gni模板

相信编译过Openharmony的应该对这个模板都不陌生,网上对该模板的示例使用也非常多。
该模板在使用时首先需要进行import

import("//build/lite/config/component/lite_component.gni")

实际上仔细阅读"//build/lite/config/component/lite_component.gni"这个文件内容,大概用法就已经都说得很明白了,至于一些细节,本人没有深究,毕竟看名字也知道,是一个“轻量-组件”模板对吧,相应内容这里不贴了,按照路径在源码里就可以找到。

hello程序代码

文件结构

hello_lite/
├── BUILD.gn
├── include
│   └── hello.h
├── src
│   └── hello.cpp
└── test
    └── test_hello.cpp

头文件

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

动态库源文件

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

主函数源文件

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

BUILD.gn

import("//build/lite/config/component/lite_component.gni")

lite_component("testhellolite")
{
    features = [":test_hello"]
}

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

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

lite_library("hellotest")
{
    sources = libsources
    if(ohos_kernel_type == "liteos_m"){
        target_type = "static_library"
    }
    else{
        target_type = "shared_library"
    }
    public_configs = [ ":testhelloliteinclude" ]
    #configs = [ ":testhelloliteinclude" ]
    #include_dirs = ["include"]
    output_name = "hellotestv1.0"
    output_dir = "${root_out_dir}/usr/hello"
}

exesources = [
    "test/test_hello.cpp"
]

executable("test_hello") {

  sources = exesources
  configs += [":testhelloliteinclude"]
  deps = [
    ":hellotest",
  ]
  output_name = "test_hello"
  output_dir = "${root_out_dir}/usr/hello"
}

为了编译的时候将这个程序编译进去,可以在qemu_small_system_demo目录下的BUILD.gn中新增这个部分

group("qemu_small_system_demo") {
  deps = [
    "apps",
    "hals/utils/sys_param:vendor.para",
    "init_configs",
    "//testhello/hello_lite:test_hello",
  ]
}

注意事项

  • lite_library内的public_configs

lite_library中如果想使用config(“xxxconfig”)配置来简化,那么关键字一定要使用public_configs,而不能用configs关键字。否则会出现如下错误:
cannot open crti.o: No such file or directory
unable to find library -lm
unable to find library -lc
cannot open crtn.o: No such file or directory
在这里插入图片描述
当然也可以不使用这种config,在lite_library内部所有需要的都写好也可以,这里config配置只不过在executable中可重复使用而已。

  • lite_library内的output_name

output_name非必须,如果设置该值,会以output_name的值生成库名称,本例中生成hellotestv1.0.so。
如果不写,则会以lite_library()括号内参数为库名称

  • lite_library内的output_dir

output_dir 非必须,如果设置该值,库文件在此路径下生成,本例中库文件将在out/arm_virt/qemu_small_system_demo/usr/hello中产生,否则默认路径为out/arm_virt/qemu_small_system_demo/usr/lib,这里的${root_out_dir}会被转义为out/arm_virt/qemu_small_system_demo/文件目录,不同build目标会有不同的产品路径

  • executable中的configs

executable中的configs关键字一定要用+=赋值,因为实际上executable有自己默认的configs模板,只用=会覆盖掉,系统会检查此处导致编译出错。

  • executable中的output_name
    output_name 非必须,如果设置该值,按照该值命名生成的可执行文件,本例中可执行文件为test_hello,否则按照executable()括号内的值命名

  • executable中的output_dir
    output_dir 非必须,如果设置该值,可执行文件在此路径下生成,本例中out/arm_virt/qemu_small_system_demo/usr/hello/bin,否则默认在out/arm_virt/qemu_small_system_demo/bin目录下生成。

特别提醒

如果使用了output_dir ,并且设置了一个特殊的文件目录,一定要在fs.yml中添加相应的文件系统配置,在编译过程中将程序及库文件打包至镜像内。否则系统镜像中看不到所编译的内容。请参考Openharmony fs.yml 配置文件

最后
在这里插入图片描述

  • 34
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值