Read the original article:How To Integrate Native C++ Codes Into Arkts
How To Integrate Native C++ Codes Into Arkts
In this article, we will learn how to add native c++ module to HarmonyOS Next
Introduction
This article introduces how to use DevEco Studio to create a native C++ app, which employs the Native C++ template to implement the call to the C standard library through the NAPI.
What is The Native Development Kit (NDK)
The Native Development Kit (NDK) is a toolset that allows you to use C and C++ code with HarmonyOS. As a subset of HarmonyOS SDK, it provides native APIs, compile scripts, and compile toolchain to help you implement key application features using C or C++. The NDK covers only some basic underlying capabilities of HarmonyOS, such as the C runtime libc, graphics library, window system, multimedia, compression library, and Node-API that bridges ArkTS/JS and C code. It does not provide complete capabilities of ArkTS/JS APIs.
You can use Node-API in the NDK to access, create, and operate JS objects, and use native dynamic libraries in JS objects.
Creating an NDK Project
- If no project is open, select Create Project on the welcome page.
- If a project is already open, choose File > New > Create Project on the menu bar.
- On the Choose Your Ability Template page, select Native C++ as the template, and click Next.
Press enter or click to view image in full size
- On the project configuration page, configure basic project information and click Finish. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.
- The main code of your application is in the cpp folder under entry/src/main of the project directory. For details about the files in the cpp folder.
- Also You can right click on the entry folder and click new -> C/C++ (Napi)
How To Implement In Arkts
- You should add your function in here like this
- You should add your function in libentry -> index.ts
- The last part is you should call your function in arkts
Conclusion
Throughout this article, we have covered the intricacies of adding native C++ modules to Harmonyos applications. We have walked through the process step by step, from how to integrate with NDK (Native Development Kit) in Harmonyos projects to how to make your C++ code talk to the Arkts layer. In the Harmonyos development ecosystem, you can use the significant advantages offered by native C++ modules in terms of performance optimization, reuse of existing codebases, and hardware interaction.







Top comments (0)