本文主要分享在軟通動(dòng)力揚(yáng)帆系列“競”OpenHarmony開發(fā)板上測試Native C++應(yīng)用開發(fā),實(shí)現(xiàn)eTS調(diào)用Native C++ 程序?qū)崿F(xiàn)對給定的兩個(gè)數(shù)進(jìn)行加減乘除運(yùn)算示例(eTS)
1.新建OpenHarmony Native C++工程
選擇File->New->Create Project -> OpenHarmony -> Native C++點(diǎn)擊Next
輸入Project name,選擇SDK版本9
點(diǎn)擊Finish,如果Native SDK 沒有下載則會(huì)出現(xiàn)以下界面,點(diǎn)擊Configure Now
下載Native SDK
Native SDK下載完成后點(diǎn)擊Finish 進(jìn)入工程
2.源碼修改
2.1 工程主要文件說明
工程初始化后目錄結(jié)構(gòu)如下圖,主要文件為紅色框內(nèi)文件
主要文件文件說明如下:
├── cpp:C++代碼區(qū) │ ├── types: // 接口存放文件夾 │ │ └── libentry │ │ ├── index.d.ts // 接口文件 │ │ └── package.json // 接口注冊配置文件 │ ├── CmakeList.txt // Cmake打包配置文件 │ └── hello.cpp // C++源代碼 └── ets // ets代碼區(qū) └── Application │ └── AbilityStage.ts // Hap包運(yùn)行時(shí)類 ├── MainAbility │ └── MainAbility.ts // Ability,提供對Ability生命周期、上下文環(huán)境等調(diào)用管理 └── pages └── index.ets // 主頁面
(左右移動(dòng)查看全部內(nèi)容)
2.2 cpp源碼編寫
自帶的案例已經(jīng)實(shí)現(xiàn)了加法運(yùn)算的接口,本案例在此基礎(chǔ)上加入減法乘法除法,entrysrcmaincpphello.cpp主要修改如下
參考“Add”方法,實(shí)現(xiàn)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; }
(左右移動(dòng)查看全部內(nèi)容)
Init中注冊對外接口名為“sub”、“mul”、“div”
EXTERN_C_START static 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
(左右移動(dòng)查看全部內(nèi)容)
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;
(左右移動(dòng)查看全部內(nèi)容)
2.4 界面實(shí)現(xiàn)
src/main/ets/pages/index.ets中通過import testNapi from 'libentry.so'引入SO包,當(dāng)點(diǎn)擊按鈕時(shí)調(diào)用對應(yīng)的方法
import testNapi from 'libentry.so' @Entry @Component struct Index { private textInputController1: TextInputController = new TextInputController() private textInputController2: TextInputController = new TextInputController() private tittle: string = '調(diào)用C標(biāo)準(zhǔn)庫示例' private message: string = '對給定的兩個(gè)數(shù)進(jìn)行加減乘除運(yùn)算' private tipsNum1: string = '請輸入第一個(gè)數(shù):' private tipsNum2: string = '請輸入第二個(gè)數(shù):' private tipsResult: string = '結(jié)果:' 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: '請輸入第一個(gè)數(shù)字:', 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: '請輸入第二個(gè)數(shù)字:', 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%') } }
(左右移動(dòng)查看全部內(nèi)容)
3.運(yùn)行效果演示
簽名后運(yùn)行效果如下:
加法:
減法:
乘法:
除法:
-
接口
+關(guān)注
關(guān)注
33文章
8596瀏覽量
151147 -
C++
+關(guān)注
關(guān)注
22文章
2108瀏覽量
73646 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
5050瀏覽量
97456 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3722瀏覽量
16313
原文標(biāo)題:揚(yáng)帆系列“競”O(jiān)penHarmony開發(fā)板實(shí)現(xiàn)對給定的兩個(gè)數(shù)進(jìn)行加減乘除運(yùn)算
文章出處:【微信號:HarmonyOS_Community,微信公眾號:電子發(fā)燒友開源社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
【軟通動(dòng)力鴻湖萬聯(lián)揚(yáng)帆系列“競”開發(fā)板試用體驗(yàn)】Native C++應(yīng)用Demo示例(eTS)
![【軟通動(dòng)力鴻湖萬聯(lián)揚(yáng)帆系列“競”<b class='flag-5'>開發(fā)板</b>試用體驗(yàn)】<b class='flag-5'>Native</b> <b class='flag-5'>C++</b>應(yīng)用Demo示例(eTS)](https://file.elecfans.com/web2/M00/6E/AE/pYYBAGM9q9uAbKFGAADXbIa2T74617.png)
鴻蒙OS開發(fā)實(shí)例:【Native C++】
![鴻蒙OS<b class='flag-5'>開發(fā)</b>實(shí)例:【<b class='flag-5'>Native</b> <b class='flag-5'>C++</b>】](https://file1.elecfans.com/web2/M00/C8/31/wKgZomYZMTCAaDv3AAY5x13C324319.jpg)
【軟通動(dòng)力鴻湖萬聯(lián)揚(yáng)帆系列“競”開發(fā)板試用體驗(yàn)】試用測評報(bào)告五 –開源鴻蒙C/C++軟件開發(fā)
【軟通動(dòng)力鴻湖萬聯(lián)揚(yáng)帆系列“競”開發(fā)板試用體驗(yàn)】Native C++應(yīng)用Demo示例(eTS)
如何使用DevEco Studio創(chuàng)建Native C++應(yīng)用
openharmony開發(fā)版 openharmony開發(fā)板
OpenHarmony3.0上編譯C控制Hi3516開發(fā)板的LED閃爍
![<b class='flag-5'>OpenHarmony</b>3.0<b class='flag-5'>上</b>編譯<b class='flag-5'>C</b>控制Hi3516<b class='flag-5'>開發(fā)板</b>的LED閃爍](https://file.elecfans.com/web2/M00/16/43/poYBAGFSdHqAdrEfAAAG9H2ShCU055.png)
如何移植OpenHarmony 3.0 到星空派開發(fā)板上
![如何移植<b class='flag-5'>OpenHarmony</b> 3.0 到星空派<b class='flag-5'>開發(fā)板</b><b class='flag-5'>上</b>](https://file.elecfans.com/web2/M00/18/66/poYBAGFuHOuARdS-AAA7dQczIPo252.png)
如何把OpenHarmony燒錄進(jìn)博流BL-HWC-G1開發(fā)板上
![如何把<b class='flag-5'>OpenHarmony</b>燒錄進(jìn)博流BL-HWC-G1<b class='flag-5'>開發(fā)板</b><b class='flag-5'>上</b>](https://file.elecfans.com/web2/M00/1B/C3/poYBAGGIhPyADw-bAAAaAchmgeo856.png)
怎么樣把OpenHarmony燒錄到開發(fā)板上
![怎么樣把<b class='flag-5'>OpenHarmony</b>燒錄到<b class='flag-5'>開發(fā)板</b><b class='flag-5'>上</b>](https://file.elecfans.com/web2/M00/1C/45/poYBAGGLHqSAfUrqAAAaAchmgeo588.png)
如何使用DevEco Studio創(chuàng)建Native C++應(yīng)用
開發(fā)板如何適配OpenHarmony 3.2
OpenHarmony C++公共基礎(chǔ)類庫應(yīng)用案例:HelloWorld
![<b class='flag-5'>OpenHarmony</b> <b class='flag-5'>C++</b>公共基礎(chǔ)類庫應(yīng)用案例:HelloWorld](https://file.elecfans.com/web2/M00/26/21/pYYBAGG5jjSALfrEAAAwAa9Oig8799.png)
評論