HarmonyOS鸿蒙Next中go编译出的so加载报错:Error message:Cannot read property add of undefined
HarmonyOS鸿蒙Next中go编译出的so加载报错:Error message:Cannot read property add of undefined 问题描述:参考如下链接,使用 ohos_golang_go 配合编译工具链,编译出来的 so 拿到鸿蒙设备,一加载就崩溃。求问是什么原因,如何处理
参考链接:https://gitcode.com/openharmony-sig/ohos_golang_go/wiki/Home.md
1 go源码
package main
import "C"
import (
"runtime"
)
func init() {
println("os:", runtime.GOOS, "IsOpenharmony:", runtime.IsOpenharmony)
}
//export add
func add(a, b C.int) int32 {
sum := int32(a) + int32(b)
return sum
}
func main() {
println("helloworld")
}
2 编译成 libhelloworld.so
2.1 build arm64-v8a
#!/bin/sh
export COMMANDLINE_TOOLS_PATH="/home/wzy/ohos-sdk/linux/commandline-tools-linux-x64-6.0.1.260/command-line-tools/sdk/default/openharmony/native"
export AR="$COMMANDLINE_TOOLS_PATH/llvm/bin/llvm-ar"
export CC="$COMMANDLINE_TOOLS_PATH/llvm/bin/aarch64-unknown-linux-ohos-clang"
export CXX="$COMMANDLINE_TOOLS_PATH/llvm/bin/aarch64-unknown-linux-ohos-clang++"
export LD="$COMMANDLINE_TOOLS_PATH/llvm/bin/lld"
#export CGO_CFLAGS="-I$COMMANDLINE_TOOLS_PATH/sysroot/usr/include -fPIC -D__MUSL__=1"
#export CGO_LDFLAGS="-Wl,-soname,libhelloworld.so -L$COMMANDLINE_TOOLS_PATH/sysroot/usr/lib/aarch64-linux-ohos -lhilog_ndk.z"
export LLVMCONFIG="$COMMANDLINE_TOOLS_PATH/llvm/bin/llvm-config"
export CGO_CFLAGS="-g -O2 `$LLVMCONFIG --cflags` -target aarch64-linux-ohos --sysroot=$COMMANDLINE_TOOLS_PATH/sysroot"
export CGO_LDFLAGS="-Wl,-soname,libhelloworld.so -target aarch64-linux-ohos -fuse-ld=lld"
export GOOS="openharmony"
export GOARCH="arm64"
export CGO_ENABLED="1"
export CGO_LDFLAGS_ALLOW="-Wl,-soname,.*"
echo "current dir: $PWD"
go version
go build -buildmode=c-shared -o ./openharmony/linuxbuild/arm64-v8a/libhelloworld.so ./helloworld.go
if [ $? -eq 0 ]; then
echo "Build for OpenHarmony linuxbuild arm64-v8a on linux success!"
else
echo "Build failed with error code $?." <&2
exit 1
fi
2.2 build x86_64
#!/bin/sh
export COMMANDLINE_TOOLS_PATH="/home/wzy/ohos-sdk/linux/commandline-tools-linux-x64-6.0.1.260/command-line-tools/sdk/default/openharmony/native"
export AR="$COMMANDLINE_TOOLS_PATH/llvm/bin/llvm-ar"
export CC="$COMMANDLINE_TOOLS_PATH/llvm/bin/x86_64-unknown-linux-ohos-clang"
export CXX="$COMMANDLINE_TOOLS_PATH/llvm/bin/x86_64-unknown-linux-ohos-clang++"
export LD="$COMMANDLINE_TOOLS_PATH/llvm/bin/lld"
export LLVMCONFIG="$COMMANDLINE_TOOLS_PATH/llvm/bin/llvm-config"
export CGO_CFLAGS="-g -O2 `$LLVMCONFIG --cflags` -target x86_64-linux-ohos --sysroot=$COMMANDLINE_TOOLS_PATH/sysroot"
export CGO_LDFLAGS="-Wl,-soname,libhelloworld.so -target x86_64-linux-ohos -fuse-ld=lld"
export GOOS="android"
export GOARCH="amd64"
export CGO_ENABLED="1"
export CGO_LDFLAGS_ALLOW="-Wl,-soname,.*"
echo "current dir: $PWD"
go version
go build -buildmode=c-shared -o ./openharmony/linuxbuild/x86_64/libhelloworld.so ./helloworld.go
if [ $? -eq 0 ]; then
echo "Build for OpenHarmony linuxbuild x86_64 on linux success!"
else
echo "Build failed with error code $?." <&2
exit 1
fi
3 DevEco Studio 加载
3.1 创建一个 NativeC++工程,复制 libhelloworld.so 过来,并在CMakeLists.txt 包含就崩溃。

3.2 CMakeLists.txt:
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0)
project(loadLibnet_httpUseCMake)
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(entry SHARED napi_init.cpp)
target_link_libraries(entry PUBLIC libc.so libc++.so libace_napi.z.so)
target_link_libraries(entry PUBLIC ${NATIVERENDER_ROOT_PATH}/../../../libs/${OHOS_ARCH}/libhelloworld.so)
3.3 pages/index.ets:
import { hilog } from '@kit.PerformanceAnalysisKit';
import testNapi from 'libentry.so';
const DOMAIN = 0x0000;
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.message = 'Welcome';
hilog.info(DOMAIN, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
})
}
.width('100%')
}
.height('100%')
}
}
3.4 指定 arm64-v8a 和 x86_64
entry/build-profile.json5:
{
"apiType": "stageMode",
"buildOption": {
"resOptions": {
"copyCodeResource": {
"enable": false
}
},
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt",
"arguments": "",
"cppFlags": "",
"abiFilters": ["arm64-v8a", "x86_64"]
}
},
"buildOptionSet": [
{
"debuggable" : true,
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": false,
"files": [
"./obfuscation-rules.txt"
]
}
}
},
"nativeLib": {
"debugSymbol": {
"strip": true,
"exclude": []
}
}
},
],
"targets": [
{
"name": "default"
},
{
"name": "ohosTest",
}
]
}
4 模拟器、真机都报错:jscrash happened in Mate 70 Pro

Device info:emulator
Build info:emulator 6.0.0.48(SP1DEVC00E47R4P11)
Fingerprint:1c7adc95b7f9b2ebb6eb410c37902307a6e76cd10f9ec6fd647cb08230d008ae
Timestamp:2025-12-29 13:53:21.736
Module name:com.example.loadlibnet_httpusecmake
Version:1.0.0
VersionCode:1000000
PreInstalled:No
Foreground:Yes
Pid:24401
Uid:20020056
Process Memory(kB): 151511(Rss)
Device Memory(kB): Total 4025360, Free 1988120, Available 2640624
Page switch history:
13:53:15.555 :enters foreground
Reason:TypeError
Error name:TypeError
Error message:Cannot read property add of undefined
Stacktrace:
at anonymous entry (entry/src/main/ets/pages/Index.ets:19:84)
HiLog:
12-29 13:53:14.522 24401 24534 I C01321/JsRuntime: [js_runtime.cpp:200]HdcRegister msg, fd 37, option ark:24401[@Debugger](/user/Debugger), isStartWithDebug 1, isDebugApp 1
12-29 13:53:14.522 24401 24534 I C01321/JsRuntime: [js_runtime.cpp:208]unlocked= 0, oemmode=
12-29 13:53:14.522 24401 24534 I C03f00/ArkCompiler: StopDebug start, componentName = <private>, tid = <private>
12-29 13:53:14.522 24401 24534 I C03f00/ArkCompiler: [ecmascript] JSNApi::StartDebuggerForSocketPair, tid = 24401, socketfd = 37
12-29 13:53:14.522 24401 24534 I C03f00/ArkCompiler: StartDebugForSocketpair, tid = <private>, socketfd = <private>
12-29 13:53:14.522 24401 24534 E C03f00/ArkCompiler: [debugger] JS debugger was initialized
12-29 13:53:14.523 24401 24955 I C03f00/ArkCompiler: HandleClient
12-29 13:53:14.523 24401 24955 I C03f00/ArkCompiler: WsServer RunServer fport ark: 37
12-29 13:53:14.939 24401 24955 I C03f00/ArkCompiler: New client connected
12-29 13:53:14.951 24401 24955 I C03f00/ArkCompiler: WsServer OnMessage: {"id":203,"method":"Runtime.enable","params":{}}
12-29 13:53:14.951 24401 24401 I C03f00/ArkCompiler: [debugger] ProtocolHandler::SendResponse: success
12-29 13:53:14.951 24401 24401 I C03f00/ArkCompiler: WsServer SendReply: {"id":203,"result":{"protocols":[]}}
12-29 13:53:14.955 24401 24955 I C03f00/ArkCompiler: WsServer OnMessage: {"id":204,"method":"Debugger.enable","params":{"options":["enableLaunchAccelerate"],"maxScriptsCacheSize":1.0E7}}
12-29 13:53:14.955 24401 24401 I C03f00/ArkCompiler: [debugger] Debugger feature enableLaunchAccelerate is enabled
12-29 13:53:14.955 24401 24401 I C03f00/ArkCompiler: [debugger] ProtocolHandler::SendResponse: success
12-29 13:53:14.955 24401 24401 I C03f00/ArkCompiler: WsServer SendReply: {"id":204,"result":{"debuggerId":"0","protocols":["removeBreakpointsByUrl","setMixedDebugEnabled","replyNativeCalling","getPossibleAndSetBreakpointByUrl","dropFrame","setNativeRange","resetSingleStepper","callFunctionOn","smartStepInto","saveAllPossibleBreakpoints","setSymbolicBreakpoints","removeSymbolicBreakpoints"]}}
12-29 13:53:14.960 24401 24955 I C03f00/ArkCompiler: WsServer OnMessage: {"id":205,"method":"Debugger.saveAllPossibleBreakpoints","params":{"locations":{"entry|entry|1.0.0|src/main/ets/pages/Index.ts":[{"lineNumber":56,"columnNumber":16}]}}}
12-29 13:53:14.960 24401 24401 I C03f00/ArkCompiler: [debugger] ProtocolHandler::SendResponse: success
12-29 13:53:14.960 24401 24401 I C03f00/ArkCompiler: WsServer SendReply: {"id":205,"result":{}}
...
...
...
12-29 13:53:19.746 24401 24401 I C03f00/ArkCompiler: WsServer SendReply: {"id":210,"result":{"result":[{"name":"__proto__","value":{"type":"object","className":"Object","unserializableValue":"ViewPU[@11b8a9a5](/user/11b8a9a5)","description":"ViewPU[@11b8a9a5](/user/11b8a9a5)","objectId":"23"},"writable":true,"configurable":true,"enumerable":false,"isOwn":true}]}}
12-29 13:53:21.735 24401 24955 I C03f00/ArkCompiler: WsServer OnMessage: {"id":212,"method":"Debugger.stepOver","params":{}}
12-29 13:53:21.735 24401 24401 I C03f00/ArkCompiler: WsServer SendReply: {"method":"Debugger.resumed","params":{}}
12-29 13:53:21.735 24401 24401 I C03f00/ArkCompiler: [debugger] ProtocolHandler::SendResponse: success
12-29 13:53:21.735 24401 24401 I C03f00/ArkCompiler: WsServer SendReply: {"id":212,"result":{}}
12-29 13:53:21.735 24401 24401 W C03f00/ArkCompiler: [default] [LoadNativeModuleFailed:4989] Load native module failed, so is [@normalized](/user/normalized):Y&&&libentry.so&
12-29 13:53:21.735 24401 24401 W C03f00/ArkCompiler: [default] [Call:3892] occur exception need return
12-29 13:53:21.735 24401 24401 E C03f00/ArkCompiler: [ecmascript] Pending exception before ExecutePendingJob called, in line:5868, exception details as follows:
12-29 13:53:21.735 24401 24401 E C03f00/ArkCompiler: TypeError: Cannot read property add of undefined
12-29 13:53:21.735 24401 24401 E C03f00/ArkCompiler: at anonymous entry (entry/src/main/ets/pages/Index.ets:19:84)
12-29 13:53:21.735 24401 24401 W C03900/Ace: [(100000:100000:scope)] after call jsFunction hasError, empty: 0, caught: 1
12-29 13:53:21.737 24401 24401 I C01356/Recovery: [app_recovery.cpp:255]scheduleRecoverApp begin
12-29 13:53:21.737 24401 24401 E C01317/AppKit: [main_thread.cpp:2013]
12-29 13:53:21.737 24401 24401 E C01317/AppKit: com.example.loadlibnet_httpusecmake is about to exit due to RuntimeError
12-29 13:53:21.737 24401 24401 E C01317/AppKit: Error type:TypeError
12-29 13:53:21.737 24401 24401 E C01317/AppKit: Error name:TypeError
12-29 13:53:21.737 24401 24401 E C01317/AppKit: Error message:Cannot read property add of undefined
12-29 13:53:21.737 24401 24401 E C01317/AppKit: Stacktrace:
12-29 13:53:21.737 24401 24401 E C01317/AppKit: at anonymous entry (entry/src/main/ets/pages/Index.ets:19:84)
12-29 13:53:21.737 24401 24401 W C01317/AppKit: [main_thread.cpp:2024]hisysevent write result=0, send event [FRAMEWORK,PROCESS_KILL], pid=24401, processName=com.example.loadlibnet_更多关于HarmonyOS鸿蒙Next中go编译出的so加载报错:Error message:Cannot read property add of undefined的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS鸿蒙Next中go编译出的so加载报错:Error message:Cannot read property add of undefined的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
从报错信息看,libentry.so加载失败了,然后,CMake里不需要显式依赖libc和libc++,可以把依赖和libs目录下相关的so去掉试下,还有一点就是你的so库可以尝试下参考使用下面的方式链接:
target_link_libraries(entry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/libs/${OHOS_ARCH}/libhelloworld.so)
#将三方库的头文件加入工程中
target_include_directories(entry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
如果还是不行还可以咨询一下Go ohos社区怎么处理这个问题。
在HarmonyOS Next中,Go编译的SO库加载报错“Cannot read property add of undefined”,通常是由于Go的CGO与鸿蒙NDK的FFI(Foreign Function Interface)机制不兼容导致。鸿蒙Next的Native API采用ArkTS/ETS语言调用,而Go CGO生成的SO库依赖传统的Linux动态链接器,与鸿蒙的运行时环境存在差异。当前鸿蒙Next的NDK主要支持C/C++原生库,Go语言尚未被官方正式支持,因此编译的SO库无法直接加载。
根据你提供的日志和代码,问题核心在于 testNapi 对象为 undefined,导致无法访问其 add 属性。错误 Cannot read property add of undefined 直接来源于你的 ETS 代码 testNapi.add(2, 3)。
根本原因是你的 Go 编译的 libhelloworld.so 没有被 NAPI 系统正确封装和导出,导致 libentry.so 加载失败,进而使得 import testNapi from 'libentry.so' 语句导入了一个 undefined 值。
分析关键日志行:
12-29 13:53:21.735 24401 24401 W C03f00/ArkCompiler: [default] [LoadNativeModuleFailed:4989] Load native module failed, so is @normalized:Y&&&libentry.so&
这表明 libentry.so(你的 NAPI 封装库)加载失败。失败原因通常是其依赖的 libhelloworld.so(Go 库)未能正确链接或初始化。
问题根源与解决方案:
你的 Go 代码编译生成了一个纯 C ABI 的动态库(libhelloworld.so),但 HarmonyOS NAPI 框架要求通过特定的 NAPI 接口与 ArkTS 进行交互。你缺少了关键的 NAPI 封装层。
正确步骤应该是:
-
Go 编译产出:你的
go build -buildmode=c-shared命令产生libhelloworld.so和libhelloworld.h(头文件)。这个 so 库提供了 C 函数add。 -
编写 NAPI 封装:你需要创建一个 C/C++ 的 NAPI 模块(例如
entry),在这个模块中:#include "libhelloworld.h"或直接声明extern函数。- 实现一个 NAPI 方法(例如
Add),在这个方法内部调用 Go 库的add函数。 - 使用
napi_property_descriptor等 NAPI API 将你的Add方法暴露给 ArkTS。
-
修改 CMakeLists.txt:正确链接 Go 库和 NAPI 库。
# 添加头文件路径 include_directories(${NATIVERENDER_ROOT_PATH}/path/to/go/headers) # 添加你的 Go 库路径 add_library(helloworld SHARED IMPORTED) set_target_properties(helloworld PROPERTIES IMPORTED_LOCATION ${NATIVERENDER_ROOT_PATH}/../../../libs/${OHOS_ARCH}/libhelloworld.so) # 创建你的 NAPI 封装库 add_library(entry SHARED napi_init.cpp) # 在 napi_init.cpp 中实现上述封装 # 链接依赖 target_link_libraries(entry PUBLIC libc.so libc++.so libace_napi.z.so helloworld) -
ETS 导入:你的 ETS 代码
import testNapi from 'libentry.so'将导入这个 NAPI 封装库,testNapi对象才会包含你暴露的Add方法。
结论:
你当前流程只完成了第一步(生成Go的C库),缺失了第二步(创建NAPI桥接层)。libentry.so 加载失败是因为它试图链接一个不完整的模块。你需要按照 HarmonyOS NAPI 开发规范,创建一个 C/C++ 层来桥接 Go 库的 C 函数和 ArkTS 运行时。

