[英]Swig wrapping C++ to python is using default Python rather than Anaconda python in Mac OS X
I am trying to wrap c++ functions into python using swig. 我正在尝试使用swig将c ++函数包装到python中。 I am using following commands
我正在使用以下命令
swig -c++ -python helloworld.i
g++ -O2 -fPIC -c hello.cpp
g++ -O2 -fPIC -c helloworld_wrap.cxx -I//anaconda/include/python2.7
g++ -lpython -dynamclib hello.o helloworld_wrap.o -o _helloworld.so
with hello.cpp
being initial file with functions and helloworld.i
being file with wrapper. hello.cpp
是具有功能的初始文件, helloworld.i
是具有包装器的文件。 These commands creates the library helloworld
but I can only import it through default python in /usr/bin/python
这些命令创建了
helloworld
库,但是我只能通过/usr/bin/python
默认python导入它
If I try to import it through python
installed through anaconda it gives following error: 如果我尝试通过通过
python
安装的python
导入它,则会出现以下错误:
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
Can you tell me how can I wrap the codes with python
from anaconda? 你能告诉我如何用anaconda的
python
包装代码吗?
Found a solution : Python.h not found using swig and Anaconda Python 找到了一个解决方案: 使用swig和Anaconda Python找不到Python.h
In above question, the top answer gives an explanation of using disutils
and a set up file in python to build the library. 在上述问题中,最高答案给出了使用
disutils
和python中的设置文件来构建库的说明。 This works wonders :) 这行奇妙的:)
The next problem I am having for wrapping simple class: 包装简单类时遇到的下一个问题:
my class code from [example] ( http://web.mit.edu/svn/src/swig-1.3.25/Examples/python/class/index.html ) 我来自[example]的班级代码( http://web.mit.edu/svn/src/swig-1.3.25/Examples/python/class/index.html )
/* File : example.h */
class Shape {
public:
Shape() {
nshapes++;
}
virtual ~Shape() {
nshapes--;
};
double x, y;
void move(double dx, double dy);
virtual double area() = 0;
virtual double perimeter() = 0;
static int nshapes;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
virtual double area();
virtual double perimeter();
};
class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { };
virtual double area();
virtual double perimeter();
};
My setup.py
file : 我的
setup.py
文件:
#setup.py file:
from setuptools import setup, Extension
setup(name='example',
version='0.1',
ext_modules=[Extension('_example', ['example.h', 'example.i'],
swig_opts=['-c++'],
)],
)
Code I am using to wrap : 我用来包装的代码:
python setup.py build_ext --inplace
Error message: 错误信息:
running build_ext
building '_example' extension
swigging example.i to example_wrap.cpp
swig -python -c++ -o example_wrap.cpp example.i
error: unknown file type '.h' (from 'example.h')
Can you suggest what is wrong here. 你能在这里提出什么问题吗? I suppose it is not recognizing '.h' file but as it is header file, I thought it could be kept as it is.
我想它不能识别“ .h”文件,但由于它是头文件,我认为可以将其保留为原样。 Also if my
setup.py
file is correct or not? 另外,我的
setup.py
文件是否正确? I am just trying to follow example for simple wrapping, there is no simple tutorial online apparently. 我只是尝试遵循简单包装的示例,显然在线上没有简单的教程。
I can also ask this question on other different question but thought just continuing here for now. 我也可以在其他不同的问题上问这个问题,但我想只是在这里继续。
Answer by Warren Weckesser in similar question . Warren Weckesser在类似问题中的回答 。 I used the
setup.py
file as suggested in the answer and added the path to the library to sys.path
and it work wonders :) 我按照答案中的建议使用了
setup.py
文件,并将该库的路径添加到sys.path
,它的工作原理很神奇:)
Use the option -I/Users/myuser/anaconda/include/python2.7
in the gcc
command. 在
gcc
命令中使用选项-I/Users/myuser/anaconda/include/python2.7
。 (That's assuming you are using python 2.7. Change the name to match the version of python that you are using.) You can use the command python-config --cflags
to get the full set of recommended compilation flags: (假设您使用的是python 2.7。更改名称以使其与所使用的python版本匹配。)您可以使用命令
python-config --cflags
获取建议的全套编译标志:
$ python-config --cflags
-I/Users/myuser/anaconda/include/python2.7 -I/Users/myuser/anaconda/include/python2.7 -fno-strict-aliasing -I/Users/myuser/anaconda/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
However, to build the extension module, I recommend using a simple setup script, such as the following setup.py
, and let distutils
figure out all the compiling and linking options for you. 但是,要构建扩展模块,我建议使用简单的安装脚本,例如以下
setup.py
,并让distutils
找出适合您的所有编译和链接选项。
# setup.py
from distutils.core import setup, Extension
example_module = Extension('_example', sources=['example_wrap.c', 'example.c'])
setup(name='example', ext_modules=[example_module], py_modules=["example"])
Then you can run: 然后,您可以运行:
$ swig -python example.i
$ python setup.py build_ext --inplace
(Take a look at the compiler commands that are echoed to the terminal when setup.py
is run.) (看看运行
setup.py
时回显到终端的编译器命令。)
distutils
knows about SWIG, so instead of including example_wrap.c
in the list of source files, you can include example.i
, and swig
will be run automatically by the setup script: distutils
知道SWIG,因此可以在源文件列表中包括example.i
而不是将example_wrap.c
包括在内,并且swig
将由安装脚本自动运行:
# setup.py
from distutils.core import setup, Extension
example_module = Extension('_example', sources=['example.c', 'example.i'])
setup(name='example', ext_modules=[example_module], py_modules=["example"])
With the above version of setup.py
, you can build the extension module with the single command 使用上述版本的
setup.py
,您可以使用单个命令来构建扩展模块。
$ python setup.py build_ext --inplace
Once you've built the extension module, you should be able to use it in python: 构建扩展模块后,您应该可以在python中使用它:
>>> import example
>>> example.fact(5)
120
If you'd rather not use the script setup.py
, here's a set of commands that worked for me: 如果您不想使用脚本
setup.py
,那么这是一组对我有用的命令:
$ swig -python example.i
$ gcc -c -I/Users/myuser/anaconda/include/python2.7 example.c example_wrap.c
$ gcc -bundle -undefined dynamic_lookup -L/Users/myuser/anaconda/lib example.o example_wrap.o -o _example.so
Note: I'm using Mac OS X 10.9.4: 注意:我使用的是Mac OS X 10.9.4:
$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.