基于OpenHarmony开发板上测试Native C++应用开发

本文主要分享在软通动力扬帆系列“竞”openharmony开发板上测试native c++应用开发,实现ets调用native c++ 程序实现对给定的两个数进行加减乘除运算示例(ets)
1.新建openharmony native c++工程
选择file->new->create project -> openharmony -> native c++点击next
输入project name,选择sdk版本9
点击finish,如果native sdk 没有下载则会出现以下界面,点击configure now
下载native sdk
native sdk下载完成后点击finish 进入工程
2.源码修改
2.1 工程主要文件说明
工程初始化后目录结构如下图,主要文件为红色框内文件
主要文件文件说明如下:
├── cpp:c++代码区                  │   ├── types:                                          // 接口存放文件夹│   │   └── libentry              │   │       ├── index.d.ts                              // 接口文件│   │       └── package.json                            // 接口注册配置文件│   ├── cmakelist.txt                                   // cmake打包配置文件│   └── hello.cpp                                       // c++源代码└── ets                                                 // ets代码区    └── application    │   └── abilitystage.ts                             // hap包运行时类    ├── mainability    │   └── mainability.ts                              // ability,提供对ability生命周期、上下文环境等调用管理    └── pages        └── index.ets                                   // 主页面  
(左右移动查看全部内容)
2.2 cpp源码编写
自带的案例已经实现了加法运算的接口,本案例在此基础上加入减法乘法除法,entrysrcmaincpphello.cpp主要修改如下
参考“add”方法,实现sub、mul、div
static napi_value sub(napi_env env, napi_callback_info info){    size_t requireargc = 2;    size_t argc = 2;    napi_value args[2] = {nullptr};    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);    napi_valuetype valuetype0;    napi_typeof(env, args[0], &valuetype0);    napi_valuetype valuetype1;    napi_typeof(env, args[1], &valuetype1);    double value0;    napi_get_value_double(env, args[0], &value0);    double value1;    napi_get_value_double(env, args[1], &value1);    napi_value sum;    napi_create_double(env, value0 - value1, &sum);    return sum;}static napi_value mul(napi_env env, napi_callback_info info){    size_t requireargc = 2;    size_t argc = 2;    napi_value args[2] = {nullptr};    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);    napi_valuetype valuetype0;    napi_typeof(env, args[0], &valuetype0);    napi_valuetype valuetype1;    napi_typeof(env, args[1], &valuetype1);    double value0;    napi_get_value_double(env, args[0], &value0);    double value1;    napi_get_value_double(env, args[1], &value1);    napi_value sum;    napi_create_double(env, value0*value1, &sum);    return sum;}static napi_value div(napi_env env, napi_callback_info info){    size_t requireargc = 2;    size_t argc = 2;    napi_value args[2] = {nullptr};    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);    napi_valuetype valuetype0;    napi_typeof(env, args[0], &valuetype0);    napi_valuetype valuetype1;    napi_typeof(env, args[1], &valuetype1);    double value0;    napi_get_value_double(env, args[0], &value0);    double value1;    napi_get_value_double(env, args[1], &value1);    napi_value sum;    napi_create_double(env, value0/value1, &sum);    return sum;}  
(左右移动查看全部内容)
init中注册对外接口名为“sub”、“mul”、“div”
extern_c_startstatic napi_value init(napi_env env, napi_value exports){    napi_property_descriptor desc[] = {        { add, nullptr, add, nullptr, nullptr, nullptr, napi_default, nullptr },        { sub, nullptr, sub , nullptr, nullptr, nullptr, napi_default, nullptr },        { mul, nullptr, mul , nullptr, nullptr, nullptr, napi_default, nullptr },        { div, nullptr, div , nullptr, nullptr, nullptr, napi_default, nullptr },    };    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);    return exports;}extern_c_end  
(左右移动查看全部内容)
2.3 index.d.ts接口文档编写
src/main/cpp/types/libentry/index.d.ts添加以下接口:
export const sub: (a: number, b: number) => number;export const mul: (a: number, b: number) => number;export const div: (a: number, b: number) => number;  
(左右移动查看全部内容)
2.4 界面实现
src/main/ets/pages/index.ets中通过import testnapi from 'libentry.so'引入so包,当点击按钮时调用对应的方法
import testnapi from 'libentry.so'@entry@componentstruct index {  private textinputcontroller1: textinputcontroller = new textinputcontroller()  private textinputcontroller2: textinputcontroller = new textinputcontroller()  private tittle: string = '调用c标准库示例'  private message: string = '对给定的两个数进行加减乘除运算'  private tipsnum1: string = '请输入第一个数:'  private tipsnum2: string = '请输入第二个数:'  private tipsresult: string = '结果:'  private buttonadd: string = '加'  private buttonsub: string = '减'  private buttonmul: string = '乘'  private buttondiv: string = '除'  @state result: number = 0  @state num1: number = 0.0  @state num2: number = 0.0  build() {    row() {      column() {        row(){          text(this.tittle).height('100%').align(alignment.center).fontsize(40).fontweight(800)        }.height('10%').width('100%').justifycontent(flexalign.center)        row(){          text(this.message).height('100%').align(alignment.center).fontsize(24).fontweight(500)        }.height('15%').width('100%').justifycontent(flexalign.center)        row(){          text(this.tipsnum1).fontcolor(color.black).fontsize(24).width('30%').height('100%').margin({left:30})          textinput({ placeholder: '请输入第一个数字:', controller:this.textinputcontroller1}).type(inputtype.number)            .height('100%').width('60%').margin({left:10,right:30})            .onchange(value =>{this.num1 = parsefloat(value)})        }.height('5%').width('100%').justifycontent(flexalign.start)        row(){          text(this.tipsnum2).fontcolor(color.black).fontsize(24).width('30%').height('100%').margin({left:30})          textinput({ placeholder: '请输入第二个数字:', controller:this.textinputcontroller2}).type(inputtype.number)            .height('100%').width('60%').margin({left:10,right:30})            .onchange(value =>{this.num2 = parsefloat(value)})        }.height('5%').width('100%').margin({top:20})        row(){          text(this.tipsresult).fontcolor(color.black).fontsize(24).width('40%').height('100%').margin({left:30})          text(''+this.result).fontcolor(color.black).fontsize(30).width(60).height(200).width('60%').height('100%')        }.height('10%').width('100%').touchable(false)        row(){          button(this.buttonadd)            .fontsize(40)            .fontweight(fontweight.bold)            .margin({top:5})            .height(100)            .width(100)            .onclick(() => {              this.result = testnapi.add(this.num1,this.num2)            })          button(this.buttonsub)            .fontsize(40)            .fontweight(fontweight.bold)            .margin({top:5})            .height(100)            .width(100)            .onclick(() => {              this.result = testnapi.sub(this.num1,this.num2)            })          button(this.buttonmul)            .fontsize(40)            .fontweight(fontweight.bold)            .margin({top:5})            .height(100)            .width(100)            .onclick(() => {              this.result = testnapi.mul(this.num1,this.num2)            })          button(this.buttondiv)            .fontsize(40)            .fontweight(fontweight.bold)            .margin({top:5})            .height(100)            .width(100)            .onclick(() => {              this.result = testnapi.div(this.num1,this.num2)            })        }.height('30%').width('100%').justifycontent(flexalign.center)      }      .width('100%')    }    .height('100%')  }}  
(左右移动查看全部内容)
3.运行效果演示
签名后运行效果如下:
加法:
减法:
乘法:
除法:


路透社:特朗普加剧了汽车芯片短缺
带隙基准是什么_带隙基准电路的优点
一种估计小电流系统线路对地电容的新方法
CastAway手机保护壳发起众筹,为智能手机打造第二块屏幕
如何做大汽车产业?如何做强汽车产业?
基于OpenHarmony开发板上测试Native C++应用开发
2018走进人工智能时代V1.0第一届论坛开幕:人类与智能的可持续发展
靠选定增益保持偏压的交流反相放大电路
电联共建共享一张5G网 中国移动领跑5G
中国电信高同庆公布5G资费上的收取情况
一个全新的无监督不需要明确物体种类的实例分割算法
新型软体机器人可用磁场隔空操纵
看VR与环保有什么联系?
Oculus为什么要首先去掉物理调整机制呢?
李东生:未来三年TCL将再投资800亿在半导体显示技术和材料
Photonis 推出适用于激光雷达应用的 MCP-PMT
OLED显示中国奋起直追 如何实现弯道超车?
老程序员对新人的建议
激光雷达和毫米波雷达的优势
华米科技入股微纳感知 后者专注于MEMS传感器研发