本文介绍了内嵌在应用程序间的Python preTER无法加载本机模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态链接到 libpython.a 应用程序(2.7)。从应用程序的跨preTER中我尝试导入时间模块( time.so ),这失败:

I have an application that statically links to libpython.a (2.7). From within the application's interpreter I try importing time module (time.so), which fails with:

ImportError: ./time.so: undefined symbol: PyExc_IOError

因此​​,该模块有未解决的符号:

So, this module has unresolved symbols:

nm -D time.so | grep PyExc_IOError
         U PyExc_IOError

我想通这个符号是通过链接器连接的应用程序时丢弃。好吧,我现在联 libpython 所有符号:

... -Wl,-whole-archive -lpython -Wl,-no-whole-archive ...

符号是现在:

$ nm app | grep PyExc_IOError
8638348 D PyExc_IOError
08638ca0 d _PyExc_IOError

但我仍然得到同样的导入错误。问题出在哪里?

But I still get the same import error. Where is the problem?

推荐答案

除了确保所有libpython都包含在你的二进制文件,你还需要确保在库中的符号被暴露在加载共享对象。当你连接libpython(静态)到主二进制文件,这意味着你需要在 - 出口动态链接参数(因此轮候册, - -export动态 -Xlinker --export动态作为gcc的参数。)当装载有一个libpython共享对象(比如,当你嵌入libpython到你的应用程序插件),这意味着你必须确保共享对象装有 RTLD_GLOBAL 标志的dlopen()

Besides making sure all of libpython is included in your binary, you also need to make sure the symbols in the library are exposed to shared objects being loaded. When you're linking libpython (statically) into your main binary this means you need the --export-dynamic linker argument (so -Wl,--export-dynamic or -Xlinker --export-dynamic as the gcc argument.) When loading a shared object with libpython (say, when you embed libpython into a plugin for your app) this means you have to make sure the shared object is loaded with the RTLD_GLOBAL flag to dlopen().

这篇关于内嵌在应用程序间的Python preTER无法加载本机模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 12:41