undefined symbol: PyExc_ImportError when embedding Python in C

后端 未结 3 1002
慢半拍i
慢半拍i 2020-12-11 18:11

I\'m developing a C shared library that makes a call to a python script. When I run the application I get this error:

Traceback (most recent call last):
  Fi         


        
相关标签:
3条回答
  • 2020-12-11 18:53

    I use such workaround: explicit linking of plugins from lib-dynload directory (it's simply, then explicit dlopen in code). Example with datetime.so:

    cmake:

    SET ( CMAKE_SHARED_LINKER_FLAGS "/usr/lib/python2.7/lib-dynload/datetime.so" )
    

    or just add /usr/lib/python2.7/lib-dynload/datetime.so as linker parameter to gcc in command line:

    g++ -shared -o libfoo.so foo.o -lbar -lzab /usr/lib/python2.7/lib-dynload/datetime.so
    
    0 讨论(0)
  • 2020-12-11 19:04

    I've found the solution. Maybe can be useful for someone else. It's a bug of python as written here http://mail.python.org/pipermail/new-bugs-announce/2008-November/003322.html I've used the solution posted here http://www.cilogon.org/gsi-c-authz

    0 讨论(0)
  • 2020-12-11 19:06

    @user1515248 solution is a link-only solution which are discouraged. i am writing this answer to expand on the links he gave and provide a more fleshed out answer (that also backs up the link he gave).

    The link, https://mail.python.org/pipermail/new-bugs-announce/2008-November/003322.html, says:

    I have been given the following workaround: in mylib.c, before PyInitialize() I can call dlopen("libpython2.5.so", RTLD_LAZY | RTLD_GLOBAL);

    This works, but I believe that lib-dynload/*.so should depend on libpython2.5.so.1 so this hack should not be necessary.

    I am using Ubuntu 8.04 with Python version 2.5.2-2ubuntu4.1.

    All I had to do was add a single line of code:

    // new line of code
    void*const libpython_handle = dlopen("libpython2.6.so", RTLD_LAZY | RTLD_GLOBAL);
    
    PyInitialize();
    

    p.s. I am on CentOS-6.

    p.p.s. My PyInitialize() is wrapped in a class and so dlopen()/PyInitialize() is done in the constructor and dlclose()/PyFinalize() is done in the destructor.

    0 讨论(0)
提交回复
热议问题