- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一项任务,我必须为现有的 C++ 库编写 Python 绑定(bind)。由于 SWIG 不仅支持 Python,还支持 Java 和 Perl 等语言,因此我正在使用 SWIG。我是 SWIG 的新手,所以我有疑问。我希望我的 python 库在 Python 2.7 和 Python 3.x 下受支持。但我不知道如何做到这一点。所以如果有人可以建议我。任何帮助,将不胜感激。如果你愿意,你可以问我更多的细节。
到目前为止我尝试了什么。
这是我的代码文件。
/* example.c file */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int my_mod(int n, int m) {
return n % m;
}
int sieve(int number) {
int* arr = malloc(sizeof (int) * (number + 10));
int* prime = malloc(sizeof (int) * (number + 10));
/* printf("Size of arr: %lu", sizeof(arr));
printf("Size of int: %lu", sizeof(int)); */
memset(arr, 0, sizeof(int) * (number + 10));
int counter = 0;
prime[counter++] = 2;
arr[0] = arr[1] = 0;
for (int i = 3; i * i <= number; i += 2) {
if (!arr[i]) {
for (int j = i + i; j < number; j += i) {
arr[j] = 1;
}
}
}
for (int i = 3; i < number; i += 2)
if (!arr[i])
prime[counter++] = i;
// free(arr);
// free(prime);
return counter;
}
我的接口(interface)文件是
/* example.i */
%module example
%{
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
extern int my_mod(int n, int m);
extern int sieve(int number);
%}
extern int my_mod(int n, int m);
extern int sieve(int number);
我的编译步骤
swig -python example.i
gcc -fpic -c example.c example_wrap.c $(pkg-config --cflags --libs python3)
gcc -shared example.o example_wrap.o -o _example.so
在上面的编译格式中,模块在 python3 中工作正常,但在 python2 中失败,错误日志为
ImportError: ./_example.so: undefined symbol: PyUnicode_FromFormat
如果我使用下面的编译命令
swig -python example.i
gcc -fpic -c example.c example_wrap.c $(pkg-config --cflags --libs python2)
gcc -shared example.o example_wrap.o -o _example.so
比模块正在使用 python2 但是当我尝试在 python3 中导入时错误消息是
ImportError: /home/deepanshu/env/swig/env/src/deep/_example.so: undefined symbol: PyInstance_Type
我确定错误是因为 $(pkg-config --cflags --libs pythonX)
我在 X 的位置指定了版本,但我如何才能确保我的模块适用于两者Python 版本?
我尝试了 swig 的 -py3
标志,但我无法使模块适用于具有上述指定标志的两个 Python 版本。
最佳答案
C 扩展在 python3 和 python2 中的工作方式不同。此链接可以帮助您确定您需要在 C 级别执行哪些操作才能使库同时适用于 python 2 和 3(不使用 swig)。 http://python3porting.com/cextensions.html
使用 Swig,我要做的是利用 pip wheels 命名约定: https://packaging.python.org/tutorials/distributing-packages/#packaging-your-project
这样做,我将打包 python3 的库并使包遵循 wheel 命名标准(“https://www.python.org/dev/peps/pep-0425/#id1”“py3-none-any”),然后制作 Python2 的库并使用相同的策略打包它(“py2-无-任何”)。然后你可以将它上传到 Pypi,pip 会知道它需要为 python2 使用 py2,为 python3 使用 py3。
关于python - 使用 SWIG 为 python2 和 python3 创建模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43891207/
我正在制作一个 C++ 库的包装器,以便它可以从 Java 中使用,我正在用 Swig 做这个。 我面临的是我有一个类(class) SomeClass ,它有一些重载的方法( someMethod
我有许多要在 SWIG 中重命名的类。我的大部分类(class)看起来像这样some_class ,我想将其重命名为 SomeClass .这很简单: %replace("%(camelcase)s"
PyPy 有一些 compatibility limitations ,尤其是关于 CPython C API。 我用 QuickFix预编译的 SWIG 绑定(bind)附带的包,我正在考虑将它与
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
使用 SWIG 生成接口(interface)模块时,生成的 C/C++ 文件包含大量静态样板函数。因此,如果想通过在同一个应用程序中使用许多单独编译的小接口(interface)来模块化 SWIG
我正在应用 SWIG 手册中有关嵌套类的解决方法,该部分使用全局内部类。在这里,我将向您展示一个类似于手册中的版本,但为您尽可能地简化了。我还必须将内联定义 {} 添加到 method(),因为没有它
我有一个现有的库 (JPhysX),它是原生 C++ 库 (PhysX) 的 Java 包装器。 Java 库使用 SWIG 生成的类型,例如 com.jphysx.SWIGTYPE_p_NxStre
有没有办法动态向下转换 swig 对象的 swig 代理? 这样做的原因是为了模拟 C++ 向下转换,但纯粹来自 python。例如,典型的 C++ 用法是 MyBase* obj = new MyB
我在远程服务器上工作,所以我在本地安装了 swig,使用 -prefix=/home/user/directory。 我有一个来自同事的 makefile,其中包含以下命令: swig $(SWIG_
据我所知,在用于将 c++ 文件编译为 python 扩展模块的 .i 文件中,我们可以添加一些 python 代码,如下所示(来自 example for adding additional pyt
我的 Swig 文件 (.i) 中有以下代码: %extend vgSofa::handler::VertexShape { vgd::Shp createVSWithNode( so
我有一个用 swig 包装的类的 C++ 代码。我无法修改代码或包装。在 python 中,我使用 ctypes 拥有一个指向所述 C++ 类的实例的指针。如何围绕该指针创建一个 swig 包装器?
我开始掌握 SWIG 的窍门,SWIG 的最新版本 (v3.0) 似乎可以处理我开箱即用所需的一切,包括 C++11 功能,但我遇到了麻烦开始在我的导演类(class)中使用 shared_ptr。
我正在使用 javacode 类型映射来添加一些附加函数来代替 SWIG 生成的函数。我想删除 SWIG 为 unsigned char mac[6]; 生成的默认 getter 和 setter(p
我正在使用 SWIG 为我的 C 库生成 Python 语言绑定(bind)。我已经设法构建了绑定(bind)和导出的数据结构,但在使用该库时我不得不跳过一些障碍。 例如,C 头文件的数据类型和函数原
我最近在node-js应用程序中从jade模板引擎切换到了swig。在使用jade时我使用了命令 jade.render('/sample.jade',{obj:object});渲染模板并传递对象。
我在我的 python 代码中发现了瓶颈,尝试了 Psycho 等。然后决定编写一个 c/c++ 扩展来提高性能。 在 swig 的帮助下,您几乎不需要关心参数等。一切正常。 现在我的问题是:swig
由于 SWIG 无法解析 __attribute__((packed))在我想包装的一些 C 结构上,我通过放置一个 #define __attribute__(x) 在我的.i文件。 这什么时候会来
我有一个包含 C++ header 的 SWIG 文件。 痛饮文件: %module my_module %{ #include "my_c_file.h" %} %include "my_c_fil
我正在尝试学习如何使用 SWIG,并且想知道我是否正在执行一些不需要执行的额外步骤。我目前有文件 Dog.cpp、Dog.h 和 Dog.i。我正在尝试使用 SWIG 包装 Dog.cpp 以便在 P
我是一名优秀的程序员,十分优秀!