我使用API10中napi调用第三方库,但是在CMakeList.txt中添加target_link_libraries以后会导致程序运行崩溃
CMakeList.txt如下:
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(WinmageScan)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
#if(DEFINED PACKAGE_FIND_FILE)
# include(${PACKAGE_FIND_FILE})
#endif()
include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include)
add_library(scan SHARED napi_init.cpp)
target_link_libraries(scan PUBLIC libace_napi.z.so)
target_link_libraries(scan PUBLIC ${NATIVERENDER_ROOT_PATH}/../../../libs/arm64-v8a/libmy.z.so)
崩溃后日志信息如下:
Error message:Cannot read property add of undefined
SourceCode:
hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
^
Stacktrace:
at anonymous (scan/src/main/ets/pages/Index.ets:16:75)
求助:HarmonyOS如何正确使用第三方库呢
Native侧可通过dlopen动态加载so库。
假设这里我们需要调用liba.so中的add函数。
1、首先需要将liba.so文件放到libs/arm64-v8a/路径下。
2、然后需要我们在ArkTS侧传递so库路径信息到Native侧。
3、然后在Native侧通过dlopen函数动态加载so库。
参考链接:Native侧如何动态加载SO库