Calling C++ function from Pyhon
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]
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