22
SWIG Hello world Calling C++ function from Pyhon 2014/12/20 Jacky Liu

SWIG Hello World

  • Upload
    e8xu

  • View
    209

  • Download
    5

Embed Size (px)

Citation preview

Page 1: SWIG Hello World

SWIG Hello worldCalling C++ function from Pyhon

2014/12/20

Jacky Liu

Page 2: SWIG Hello World

• This slide organize instructions from “Falldog的程式戰場 - [Python] Windows底下使用SWIG呼叫C/C++的function” (http://falldog7.blogspot.tw/2013/07/python-swig-c-function.html)

Page 3: SWIG Hello World

Environment Setting

• OS (Windows 7 also works)Windows 8.1

• CompilerVisual 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/

Page 4: SWIG Hello World

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)) # >>> 2print(Sample.Sqrt(2)) # >>> 4

C++

Python

Page 5: SWIG Hello World

New project

Page 6: SWIG Hello World

New projectName=Sample

Page 7: SWIG Hello World

New project

Page 8: SWIG Hello World

New project

1. DLL2. Empty project3. Finish

Page 9: SWIG Hello World

Project setting

• Project setting - Include Python header files

> Right click >

Page 10: SWIG Hello World

Project setting

• Properties | Configuration Properties | VC++ DirectoriesExecutable Directories: C:\swigwin-3.0.2

Page 11: SWIG Hello World

Project setting

• Project | Properties | Configuration Properties | C/C++Additional Include Directories: C:\Python33\include

Page 12: SWIG Hello World

Project setting

• Properties | Configuration Properties | LinkerAdditional Library Directories: C:\Python33\libs

Page 13: SWIG Hello World

Project setting

• Properties | Configuration Properties | Build Events | Pre-Build Eventswig.exe -c++ -python -o $(ProjectDir)\Sample_wrap.cxx $(ProjectDir)Sample.i

Page 14: SWIG Hello World

Add file

• Add Sample.h

#include "Python.h"

int AddOne(int n);int Sqrt(int n);PyObject* SqrtInPyObj(PyObject* obj);

Page 15: SWIG Hello World

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);

}

Page 16: SWIG Hello World

Add file

• Add Sample.i

%module Sample

%{#include "Sample.h" %}

%include "Sample.h"

Page 17: SWIG Hello World

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

Page 18: SWIG Hello World

Configure Linker output file name$(OutDir)\_$(ProjectName).pyd

Page 19: SWIG Hello World

Add preprocessor definitions__WIN32__

Page 20: SWIG Hello World

Generate .pyd file for python

• Build “Release” (F7) againGenerate • $(ProjectDir)/Release/_Sample.pyd

Page 21: SWIG Hello World

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) # >>> 2print Sample.Sqrt(2) # >>> 4print Sample.Sqrt(3) # >>> 9print Sample.SqrtInPyObj(2) # >>> 4print Sample.SqrtInPyObj(3) # >>> 9

Page 22: SWIG Hello World

Reference

• Falldog的程式戰場 - [Python] Windows底下使用SWIG呼叫C/C++的function