Tips:c++与python结合的好处就不写了,网上一搜好多,现在主要是记录下自己刚开始学习的简单例子,使用swig封装c\c++至python可调用
自己使用了两种方法,两种都已测试成功,现在将两种方法都说明一下。
方法一:
配置环境:win7 64+vs2010+swig2.0.9+python3.3
swig下载下来后直接解压到d:\就可以了
How to use SWIG under VS2010.
lets define you want to obtain mylib.py, so you created some SWIG interface files with mylib.i as "main" file. I assume that you already have a solution with project with your C++ classes there.
(1) First create C++ project for SWIG interface. Use Visual C++->Class library project which should create a C++ DLL stub. I put all .i files there. And set visual studio to highlight .i as .h - it is handy.
(1.1) Add mylib_wrap.cxx file to the project (create empty file while swig hasn't generated one yet)
(2)
a) Press right button over the mylib.i, choose properties.
b) set ItemType as "Custom build tool".
In custom build step window:
c) Command line field should be something like:
echo In order to function correctly, please ensure the following environment variables are
correctly set:
echo PYTHON_INCLUDE: %PYTHON_INCLUDE%
echo PYTHON_LIB: %PYTHON_LIB%
echo on
C:\swig\swig.exe -c++ -python %(FullPath)
change C:\swig\swig.exe to your path to SWIG
d) In Outputs field:
$(InputName)_wrap.cxx

(3) Go to this project properties:
a) C++ tab -> Additional Include Directories
add $(PYTHON_INCLUDE); ...
c) Linker -> Output File
Path-You-Needed\_mylib.pyd
d) Linker -> Enable Incremental Linking
set as No (/INCREMENTAL:NO)
e) Linker -> Input -> Additional Dependencies
add $(PYTHON_LIB);...
f) C/C++ -> Precompiled Headers:Switch off precompiled headers, set *Not Using Precompiled Headers and remove stdafx files after
g) General tab. Just check that these are set:
Configuration type = Dynamic Library (.dll)
Character set = Use Unicode Character Set
Common Language Runtime Support = No Common Language Runtime Support
It compiles.
这个跟上一篇转载的文章差不多。
方法二:就是官方文档里介绍的方法 Using distutils
Suppose that you defined a SWIG module such as the following:
/* File: example.i */
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
int fact(int n);
The #define SWIG_FILE_WITH_INIT line inserts a macro that specifies that the resulting C file should be built as a python extension, inserting the moduleinit code. This .i file wraps the following simple C file:
/* File: example.c */
#include "example.h"
int fact(int n) {
if (n < 0){ /* This should probably return an error, but this is simpler */
return 0;
}
if (n == 0) {
return 1;
}
else {
/* testing for overflow would be a good idea here */
return n * fact(n-1);
}
}
With the header file:
/* File: example.h */ int fact(int n);
To build a Python module, run SWIG using the -python option:
swig -python example.i
(打开命令提示符,进入到D:\swigwin-2.0.9文件夹,然后就可以直接运行这条命令了,对了记得要把example.i文件放到这个文件夹里)
运行命令后,会出现example_wrap.c 、example.py两个文件。
Here is a sample setup.py file for the above example:
#!/usr/bin/env python
"""
setup.py file for SWIG example
"""
from distutils.core import setup, Extension
example_module = Extension('_example',
sources=['example_wrap.c', 'example.c'],
)
setup (name = 'example',
version = '0.1',
author = "SWIG Docs",
description = """Simple swig example from docs""",
ext_modules = [example_module],
py_modules = ["example"],
)
然后将example.c、example.h、example.i、example.py、example_wrap.c、setup.py,放到一个目录中,在命令提示附中进入这个目录,运行
python setup.py build_ext --inplace
然后就会出现_example.pyd文件。
将_example.pyd、example.py放到python安装目录中的D:\Python33\DLLs中
在python的IDLE中就可以import example 了
参考:
1:官方帮助http://www.swig.org/Doc2.0/SWIGDocumentation.html#Python
2:http://blog.youkuaiyun.com/hugewave/article/details/7705392
3:http://blog.youkuaiyun.com/luotuo818/article/details/7335344
本文介绍两种使用SWIG将C++代码封装为Python模块的方法。一种是在VS2010环境下配置并构建SWIG接口文件,另一种则是通过distutils进行设置并构建。文章提供了详细的步骤指导。
714

被折叠的 条评论
为什么被折叠?



