swig

python distutils not include the SWIG generated module

拥有回忆 提交于 2020-01-23 04:21:12
问题 I am using distutils to create an rpm from my project. I have this directory tree: project/ my_module/ data/file.dat my_module1.py my_module2.py src/ header1.h header2.h ext_module1.cpp ext_module2.cpp swig_module.i setup.py MANIFEST.in MANIFEST my setup.py : from distutils.core import setup, Extension module1 = Extension('my_module._module', sources=['src/ext_module1.cpp', 'src/ext_module2.cpp', 'src/swig_module.i'], swig_opts=['-c++', '-py3'], include_dirs=[...], runtime_library_dirs=[...],

将c、c++代码包装成python代码

久未见 提交于 2020-01-22 17:26:41
如果要在Python中包装现有的C或C ++功能,有很多选择,这里只记录最值得推荐的方式,SWIG被Subversion, wxPython, Xapian等项目使用。值得一提的是,Google也使用SWIG。 用SWIG包装Python代码 SWIG本质上是一种宏语言,可以植入C代码,并且可以为你选择的语言(python)生成包装器代码。 SWIG包装“ hello”程序需要三件事。 第一步,生成文件 swig -python -c++ -o _swigdemo_module.cc swigdemo.i python setup.py build_ext --inplace 首先,运行SWIG生成C代码扩展; 然后,setup.py build 以实际构建它。 第二步,构建SWIG包装文件“ swigdemo.i” 在这种情况下,它可能非常简单: % module swigdemo % { # include <stdlib.h> # include "hello.h" % } % include "hello.h" %module指定要从该包装文件生成的模块的名称; %{%}之间的代码按原样放置在C输出文件中;在这种情况下,它仅包含两个头文件。 最后一行%include只是说“针对此头文件中的声明构建接口”。 第三步,构建setup.py文件 from distutils .

从Python调用C / C ++?

断了今生、忘了曾经 提交于 2020-01-18 07:32:50
构造与 C 或C ++库的 Python 绑定的最快方法是什么? (如果这很重要,我正在使用Windows。) #1楼 最快的方法是使用 SWIG 。 来自SWIG 教程的示例 : /* File : example.c */ int fact(int n) { if (n <= 1) return 1; else return n*fact(n-1); } 接口文件: /* example.i */ %module example %{ /* Put header files here or function declarations like below */ extern int fact(int n); %} extern int fact(int n); 在Unix上构建Python模块: swig -python example.i gcc -fPIC -c example.c example_wrap.c -I/usr/local/include/python2.7 gcc -shared example.o example_wrap.o -o _example.so 用法: >>> import example >>> example.fact(5) 120 请注意,您必须具有python-dev。 同样在某些系统中,python头文件会根据您的安装方式位于/usr

“No module named '_<module>'” when importing a SWIG module with embedded Python

你离开我真会死。 提交于 2020-01-15 10:40:06
问题 I'm trying to use SWIG with embedded Python 3.5.2. The following is built as a Windows console app. It fails initializing the Python side SWIG module "arpy.py" when it tries to import the C++ side SWIG module "_arpy". My (probably incorrect) understanding it that the C++ side "_arpy" module should already be loaded by the SWIG module init function called from main() but this doesn't seem to be the case. arpy.i: %module arpy %{ #include "arpy.h" %} %include <windows.i> int test(); arpy.h:

C# SWIG vector<string> to string[]

﹥>﹥吖頭↗ 提交于 2020-01-15 04:28:15
问题 Given C++ methods like std::vector< std::string > getFoo(); void setFoo( const std::vector< std::string > &foo); How can I get SWIG to expose it like this to C# string[] getFoo(); setFoo(string[] foo); I realize that lots of people have asked about vector<string> and SWIG, but I haven't seen anything addressing the conversion to/from string[] . Everyone mentions using the SWIG templates, like this %template(StringVector) vector<string>; But that just creates a datatype that callers will have

combining ctypes and swig

一曲冷凌霜 提交于 2020-01-15 02:55:10
问题 I have been using SWIG for a long time - generally I like it. But doing callback functions seems (much) easier using ctypes. How can I combine the two "ways" of interacting with a C dll ? The first step would be to know, how to get a ctypes object to the dll after the dll is already loaded via the import of the corresponding SWIG module. Thanks, Sebastian. 回答1: If x.pyd is the swig compiled extension module, then you can load the dll via ctypes in this way: import x from ctypes import PyDLL

Automake, generated source files and VPATH builds

强颜欢笑 提交于 2020-01-14 08:55:13
问题 I'm doing VPATH builds with automake. I'm now also using generated source, with SWIG. I've got rules in Makefile.am like: dist_noinst_DATA = whatever.swig whatever.cpp: whatever.swig swig -c++ -php $^ Then the file gets used later: myprogram_SOURCES = ... whatever.cpp It works fine when $builddir == $srcdir . But when doing VPATH builds (e.g. mkdir build; cd build; ../configure; make ), I get error messages about missing whatever.cpp . Should generated source files go to $builddir or $srcdir

SWIG : Unable to access constructor with double pointer

我的未来我决定 提交于 2020-01-14 03:30:28
问题 I am new to SWIG. I have created a python module to use c++ classes. My cpp header code is GradedComplex.h : class GradedComplex { public: typedef std::complex<double> dcomplex; typedef Item<dcomplex> item_type; typedef ItemComparator<dcomplex> comparator; typedef std::set<item_type, comparator> grade_type; private: int n_; std::vector<grade_type *> grade_; std::vector<double> thre_; public: GradedComplex(int n, double *thre); ~GradedComplex(); void push(item_type item); void avg(double *buf)

Convert a C++ callback to Java

帅比萌擦擦* 提交于 2020-01-14 03:18:09
问题 I have the following code in C++ (Cocos2d) : typedef void (CCObject::*SEL_CallFunc)(); CCCallFunc * CCCallFunc::actionWithTarget(CCObject* pSelectorTarget, SEL_CallFunc selector) { CCCallFunc *pRet = new CCCallFunc(); if (pRet && pRet->initWithTarget(pSelectorTarget)) { pRet->m_pCallFunc = selector; pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); return NULL; } When converting with swig to java I get the following : public static CCCallFunc actionWithTarget(CCObject pSelectorTarget,

How can I wrap a non-Traits model for use with Python Traits?

喜你入骨 提交于 2020-01-13 19:19:48
问题 I would like to wrap a non-Traits model class for use with Python Traits. My goal is to write a Traits-based UI to manipulate an "external" model class. The external model class has been generated by SWIG and so I cannot add enthought.traits.api.HasTraits as an ancestor (I think, though I may be wrong). My current best attempt is from enthought.traits.api import HasStrictTraits, Property, Instance class ExternalModel(): foo = 'foo' class TraitsModel(HasStrictTraits): _e = Instance