ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
SWIG Hello world
Calling C++ function from Pyhon
2014/12/20
Jacky Liu
? This slide organize instructions from ¡°FalldogµÄ³Ì
ʽ‘ðˆö - [Python] Windowsµ×Ï ʹÓÃSWIGºô½Ð
C/C++µÄfunction¡± (http://falldog7.blogspot.tw/2013/07/python-
swig-c-function.html)
Environment Setting
? OS (Windows 7 also works)
Windows 8.1
? Compiler
Visual Studio 2013 Update4
? Python (Other version may be incompatible)
python-3.3.5.msi (x86) [1]
? SWIG (Unzip and copy swigwin-3.0.2 folder to C:)
swigwin-3.0.2 [2]
[1] http://www.python.org/ftp/python/3.3.5/python-3.3.5.msi
[2] http://sourceforge.net/projects/swig/files/swigwin/swigwin-3.0.2/
SWIG Hello world
? Our goal is to call a simple C++ function from
python.
int AddOne(int n) { return n + 1; }
int Sqrt(int n) { return n*n; }
print(Sample.AddOne(1)) # >>> 2
print(Sample.Sqrt(2)) # >>> 4
C++
Python
New project
New project
Name=Sample
New project
New project
1. DLL
2. Empty project
3. Finish
Project setting
? Project setting - Include Python header files
> Right click >
Project setting
? Properties | Configuration Properties | VC++ Directories
Executable Directories: C:swigwin-3.0.2
Project setting
? Project | Properties | Configuration Properties | C/C++
Additional Include Directories: C:Python33include
Project setting
? Properties | Configuration Properties | Linker
Additional Library Directories: C:Python33libs
Project setting
? Properties | Configuration Properties | Build Events | Pre-Build Event
swig.exe -c++ -python -o $(ProjectDir)Sample_wrap.cxx $(ProjectDir)Sample.i
Add file
? Add Sample.h
#include "Python.h"
int AddOne(int n);
int Sqrt(int n);
PyObject* SqrtInPyObj(PyObject* obj);
Add file
? Add Sample.cpp
#include "Sample.h"
int AddOne(int n)
{
return n + 1;
}
int Sqrt(int n)
{
return n*n;
}
PyObject* SqrtInPyObj(PyObject* obj)
{
int n = PyLong_AsLong(obj);
return Py_BuildValue("i", n*n);
}
Add file
? Add Sample.i
%module Sample
%{
#include "Sample.h"
%}
%include "Sample.h"
Generate files
? Build ¡°Release¡± (F7)
Generate
? $(ProjectDir)/Release/Sample.dll
? $(ProjectDir)/Sample/Sample.py
? $(ProjectDir)/Sample/Sample_wrap.cxx
? Add Sample_wrap.cxx back to project
Configure Linker output file name
$(OutDir)_$(ProjectName).pyd
Add preprocessor definitions
__WIN32__
Generate .pyd file for python
? Build ¡°Release¡± (F7) again
Generate
? $(ProjectDir)/Release/_Sample.pyd
Try it in Python
? Copy
? $(ProjectDir)/Release/_Sample.pyd
? $(ProjectDir)/Sample/Sample.py
? To the same folder with your script
? Test.py
import Sample
print Sample.AddOne(1) # >>> 2
print Sample.Sqrt(2) # >>> 4
print Sample.Sqrt(3) # >>> 9
print Sample.SqrtInPyObj(2) # >>> 4
print Sample.SqrtInPyObj(3) # >>> 9
Reference
? FalldogµÄ³Ìʽ‘ðˆö - [Python] Windowsµ×Ï ʹÓÃ
SWIGºô½ÐC/C++µÄfunction

More Related Content

SWIG Hello World

  • 1. SWIG Hello world Calling C++ function from Pyhon 2014/12/20 Jacky Liu
  • 2. ? This slide organize instructions from ¡°FalldogµÄ³Ì ʽ‘ðˆö - [Python] Windowsµ×Ï ʹÓÃSWIGºô½Ð C/C++µÄfunction¡± (http://falldog7.blogspot.tw/2013/07/python- swig-c-function.html)
  • 3. Environment Setting ? OS (Windows 7 also works) Windows 8.1 ? Compiler Visual Studio 2013 Update4 ? Python (Other version may be incompatible) python-3.3.5.msi (x86) [1] ? SWIG (Unzip and copy swigwin-3.0.2 folder to C:) swigwin-3.0.2 [2] [1] http://www.python.org/ftp/python/3.3.5/python-3.3.5.msi [2] http://sourceforge.net/projects/swig/files/swigwin/swigwin-3.0.2/
  • 4. SWIG Hello world ? Our goal is to call a simple C++ function from python. int AddOne(int n) { return n + 1; } int Sqrt(int n) { return n*n; } print(Sample.AddOne(1)) # >>> 2 print(Sample.Sqrt(2)) # >>> 4 C++ Python
  • 8. New project 1. DLL 2. Empty project 3. Finish
  • 9. Project setting ? Project setting - Include Python header files > Right click >
  • 10. Project setting ? Properties | Configuration Properties | VC++ Directories Executable Directories: C:swigwin-3.0.2
  • 11. Project setting ? Project | Properties | Configuration Properties | C/C++ Additional Include Directories: C:Python33include
  • 12. Project setting ? Properties | Configuration Properties | Linker Additional Library Directories: C:Python33libs
  • 13. Project setting ? Properties | Configuration Properties | Build Events | Pre-Build Event swig.exe -c++ -python -o $(ProjectDir)Sample_wrap.cxx $(ProjectDir)Sample.i
  • 14. Add file ? Add Sample.h #include "Python.h" int AddOne(int n); int Sqrt(int n); PyObject* SqrtInPyObj(PyObject* obj);
  • 15. Add file ? Add Sample.cpp #include "Sample.h" int AddOne(int n) { return n + 1; } int Sqrt(int n) { return n*n; } PyObject* SqrtInPyObj(PyObject* obj) { int n = PyLong_AsLong(obj); return Py_BuildValue("i", n*n); }
  • 16. Add file ? Add Sample.i %module Sample %{ #include "Sample.h" %} %include "Sample.h"
  • 17. Generate files ? Build ¡°Release¡± (F7) Generate ? $(ProjectDir)/Release/Sample.dll ? $(ProjectDir)/Sample/Sample.py ? $(ProjectDir)/Sample/Sample_wrap.cxx ? Add Sample_wrap.cxx back to project
  • 18. Configure Linker output file name $(OutDir)_$(ProjectName).pyd
  • 20. Generate .pyd file for python ? Build ¡°Release¡± (F7) again Generate ? $(ProjectDir)/Release/_Sample.pyd
  • 21. Try it in Python ? Copy ? $(ProjectDir)/Release/_Sample.pyd ? $(ProjectDir)/Sample/Sample.py ? To the same folder with your script ? Test.py import Sample print Sample.AddOne(1) # >>> 2 print Sample.Sqrt(2) # >>> 4 print Sample.Sqrt(3) # >>> 9 print Sample.SqrtInPyObj(2) # >>> 4 print Sample.SqrtInPyObj(3) # >>> 9
  • 22. Reference ? FalldogµÄ³Ìʽ‘ðˆö - [Python] Windowsµ×Ï ʹÓà SWIGºô½ÐC/C++µÄfunction