Skip to main content
This guide demonstrates how to integrate a C/C++ library compiled externally (e.g., with CLion or CMake) into an arkffi project and call its functions via dlopen.

Overview

  1. Create a C/C++ library project
  2. Cross-compile for HarmonyOS arm64-v8a using the NDK
  3. Place the compiled .so into entry/libs/arm64-v8a/
  4. Load and call it from ArkTS via dlopen

1. Create the External Library

Source library.cpp:
extern "C" prevents C++ name mangling, ensuring dlsym("hello") can find the symbol. Without it, the symbol would be mangled to _Z5hellov and dlopen would fail.
CMakeLists.txt:

2. Cross-Compile for HarmonyOS

3. Place the Output

If your project also supports x86_64 emulator, place the corresponding .so under entry/libs/x86_64/ as well.

4. Configure abiFilters

Ensure library/build-profile.json5 contains the target architecture in abiFilters:

5. Call from ArkTS

Important Notes

  • Functions returning const char* must use returns: FFIType.int64 (to get the pointer), not FFIType.CString.
  • Use CString to read the actual string content from the pointer.
  • Ensure the library uses extern "C" to avoid name mangling.

Troubleshooting