You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Go共享库遇undefined symbol错误,求排查解决方法

问题解决:Go导出C函数被Python调用时提示未定义符号

问题原因

你定义的test函数返回值使用了Go原生的int类型,而要导出给C调用的函数必须使用C包提供的类型(如C.int),否则Go编译器无法生成符合C规范的导出符号,导致ctypes无法找到test符号。

修改后的main.go代码

package main

import "C"

// export test
func test() C.int {
    return C.int(1)
}

func main() {}

操作步骤

  1. 用上面修改后的代码替换原main.go
  2. 重新执行编译命令:
    go build -o lib.so -buildmode=c-shared main.go
    
  3. 再次运行Python脚本,即可正常获取返回值1。

验证符号是否正确导出(可选)

可以用nm命令检查库中的符号:

nm -D lib.so | grep test

如果输出类似0000000000001120 T test,说明test符号已正确导出。

内容的提问来源于stack exchange,提问作者Vlakius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 20:57:38