douhoushou8385 2017-07-28 19:31
浏览 241
已采纳

AttributeError:dlsym(0x7fc4cfd563b0,add_all_items_to_map):未找到符号; 使用C从Python运行Go

I have the following go file:

//try_async.go
package main

import (
    "C"
    "fmt"
    "math/rand"
    "sync"
    "time"
)

var mutex sync.Mutex
var wg sync.WaitGroup

func random_sleep() {
    r := rand.Intn(3000)
    time.Sleep(time.Duration(r) * time.Millisecond)
}


func add_to_map(m map[string] string, word string) {
    defer wg.Done()
    added_word := word + " plus more letters"
    fmt.Println("Before sleep")
    random_sleep()
    mutex.Lock()
    defer mutex.Unlock()
    m[word] = added_word
    fmt.Println("Added word %v", word)
}

// export add_all_items_to_map
func add_all_items_to_map(words []string) map[string]string {
    words_map := make(map[string]string)
    for _, this_word := range words {
        wg.Add(1)
        go add_to_map(words_map, this_word)
    }
    wg.Wait()
    return words_map
}


func main() {
    // result := add_all_items_to_map([]string{"cat", "dog", "fish"})
    // fmt.Println(result)
}

I have the Python script:

from ctypes import cdll

"""
run

  go build -buildmode=c-shared -o try_async.so try_async.go

first
"""

lib = cdll.LoadLibrary('./try_async.so')

print("Loaded go lib")
result = lib.add_all_items_to_map(['cat', 'dog', 'fish'])
print(result)

The outcome is strange error that others said is when you don't build the shared object, but I did:

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ rm *.so

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ go build -buildmode=c-shared -o try_async.so try_async.go

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ python go-async-caller.py 
Loaded go lib
Traceback (most recent call last):
  File "go-async-caller.py", line 14, in <module>
    result = lib.add_all_items_to_map(['cat', 'dog', 'fish'])
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 375, in __getattr__
    func = self.__getitem__(name)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 380, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x7fc4cfd563b0, add_all_items_to_map): symbol not found

The following does work:

libadd.go-

//libadd.go
package main

import "C"

//export add
func add(left int, right int) int {
    return left + right
}

func main() {}

go-caller-example.py-

from ctypes import cdll

"""
run

  go build -buildmode=c-shared -o libadd.so libadd.go

first
"""

lib = cdll.LoadLibrary('./libadd.so')

print("Loaded go lib")
result = lib.add(2, 3)
print(result)

like this

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ go build -buildmode=c-shared -o libadd.so libadd.go

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ python go-caller-example.py 
Loaded go lib
5

http://savorywatt.com/2015/09/18/calling-go-code-from-python-code/

  • 写回答

1条回答 默认 最新

  • douzen3516 2017-07-28 20:46
    关注

    Directives in Go are kinda fragile, you need to get them exactly right or they will fail silently.

    In this case you have this:

    // export add_all_items_to_map
    

    But need this:

    //export add_all_items_to_map
    

    See the cgo docs for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化