V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
myyou
V2EX  ›  Python

golang 编译为.so 库用 Python 调用后报“dlsym(0x7fe2a7d4ea30, background): symbol not found”的错误,请问是什么原因?

  •  
  •   myyou · 2018-08-10 15:05:30 +08:00 · 2508 次点击
    这是一个创建于 2100 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • golang 代码:
    package main
    
    import (
    	"C"
    	"github.com/360EntSecGroup-Skylar/excelize"
    	)
    
    func background(excelname, sheetname, image *C.char) *C.char {
    	excelname_go := C.GoString(excelname)
    	sheetname_go := C.GoString(sheetname)
    	image_go := C.GoString(image)
    
    	xlsx, err := excelize.OpenFile(excelname_go)
    	if err != nil {
    		panic(err)
    	}
        
    	xlsx.SetSheetBackground(sheetname_go, image_go)
    
    	err = xlsx.SaveAs(excelname_go)
    	if err != nil {
    		panic(err)
    	}
    
    	return C.CString("success")
    }
    
    func main()  {}
    
    import ctypes
    from ctypes import cdll
    
    def set_background():
        name = b'/Users/myyou/work/background.xlsx'
        background = cdll.LoadLibrary('/Users/myyou/work/excel.so').background
        background.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p]
        background.restype = ctypes.c_char_p
        background(name, b'Sheet1', b'/Users/myyou/work/ntim.jpg')
        return 'ok'
    
    • 调用后发生的错误:
    Traceback (most recent call last):
      File "/Users/myyou/work/pythonenv/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-3-24964e319282>", line 1, in <module>
        set_background()
      File "/Users/myyou/work/test.py", line 275, in 
        background = cdll.LoadLibrary('/Users/kamui/work/excel.so').background
      File "/Users/myyou/.pyenv/versions/3.6.2/lib/python3.6/ctypes/__init__.py", line 361, in __getattr__
        func = self.__getitem__(name)
      File "/Users/myyou/.pyenv/versions/3.6.2/lib/python3.6/ctypes/__init__.py", line 366, in __getitem__
        func = self._FuncPtr((name_or_ordinal, self))
    AttributeError: dlsym(0x7fe2a7d4ea30, background): symbol not found
    

    而且在编译成.so 库是并没有生成 excel.h 只有 excel.so

    2 条回复    2018-08-10 16:58:10 +08:00
    myyou
        1
    myyou  
    OP
       2018-08-10 15:58:42 +08:00
    - 自己找到原因了,需要价格注释

    ```
    import ctypes
    from ctypes import cdll

    //export background
    def set_background():
    name = b'/Users/myyou/work/background.xlsx'
    background = cdll.LoadLibrary('/Users/myyou/work/excel.so').background
    background.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p]
    background.restype = ctypes.c_char_p
    background(name, b'Sheet1', b'/Users/myyou/work/ntim.jpg')
    return 'ok'
    ```
    myyou
        2
    myyou  
    OP
       2018-08-10 16:58:10 +08:00
    @myyou 注释加错了,应该加载 golang 代码中

    ```
    package main

    import (
    "C"
    "github.com/360EntSecGroup-Skylar/excelize"
    )

    //export background
    func background(excelname, sheetname, image *C.char) *C.char {
    excelname_go := C.GoString(excelname)
    sheetname_go := C.GoString(sheetname)
    image_go := C.GoString(image)

    xlsx, err := excelize.OpenFile(excelname_go)
    if err != nil {
    panic(err)
    }

    xlsx.SetSheetBackground(sheetname_go, image_go)

    err = xlsx.SaveAs(excelname_go)
    if err != nil {
    panic(err)
    }

    return C.CString("success")
    }

    func main() {}

    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   975 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:56 · PVG 05:56 · LAX 14:56 · JFK 17:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.