SlideShare ist ein Scribd-Unternehmen logo
1 von 22
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

Weitere ähnliche Inhalte

Was ist angesagt?

Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
Ranel Padon
 
Monitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntopMonitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntop
PyCon Italia
 

Was ist angesagt? (20)

A commercial open source project in Python
A commercial open source project in PythonA commercial open source project in Python
A commercial open source project in Python
 
Python on a chip
Python on a chipPython on a chip
Python on a chip
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.
 
Mixing C++ & Python II: Pybind11
Mixing C++ & Python II: Pybind11Mixing C++ & Python II: Pybind11
Mixing C++ & Python II: Pybind11
 
Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015
 
Python for Delphi Developers - Part 2
Python for Delphi Developers - Part 2Python for Delphi Developers - Part 2
Python for Delphi Developers - Part 2
 
Ry pyconjp2015 karaoke
Ry pyconjp2015 karaokeRy pyconjp2015 karaoke
Ry pyconjp2015 karaoke
 
Python lec1
Python lec1Python lec1
Python lec1
 
PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtime
 
The genesis of clusterlib - An open source library to tame your favourite sup...
The genesis of clusterlib - An open source library to tame your favourite sup...The genesis of clusterlib - An open source library to tame your favourite sup...
The genesis of clusterlib - An open source library to tame your favourite sup...
 
Tensorflow 2.0 and Coral Edge TPU
Tensorflow 2.0 and Coral Edge TPU Tensorflow 2.0 and Coral Edge TPU
Tensorflow 2.0 and Coral Edge TPU
 
Building custom kernels for IPython
Building custom kernels for IPythonBuilding custom kernels for IPython
Building custom kernels for IPython
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from Python
 
1901200100000 presentation short term mini project on python
1901200100000 presentation short term mini project on python1901200100000 presentation short term mini project on python
1901200100000 presentation short term mini project on python
 
Monitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntopMonitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntop
 
Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020
 
Python 101 for the .NET Developer
Python 101 for the .NET DeveloperPython 101 for the .NET Developer
Python 101 for the .NET Developer
 
Python for Delphi Developers - Part 1 Introduction
Python for Delphi Developers - Part 1 IntroductionPython for Delphi Developers - Part 1 Introduction
Python for Delphi Developers - Part 1 Introduction
 

Ähnlich wie SWIG Hello World

Ähnlich wie SWIG Hello World (20)

Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance Tooling
 
Buildout: creating and deploying repeatable applications in python
Buildout: creating and deploying repeatable applications in pythonBuildout: creating and deploying repeatable applications in python
Buildout: creating and deploying repeatable applications in python
 
Creating new Tizen profiles using the Yocto Project
Creating new Tizen profiles  using the Yocto ProjectCreating new Tizen profiles  using the Yocto Project
Creating new Tizen profiles using the Yocto Project
 
Optimizing Your CI Pipelines
Optimizing Your CI PipelinesOptimizing Your CI Pipelines
Optimizing Your CI Pipelines
 
Python on pi
Python on piPython on pi
Python on pi
 
Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023
 
Android Security Development - Part 2: Malicious Android App Dynamic Analyzi...
Android Security Development - Part 2: Malicious Android App Dynamic Analyzi...Android Security Development - Part 2: Malicious Android App Dynamic Analyzi...
Android Security Development - Part 2: Malicious Android App Dynamic Analyzi...
 
Getting started with open cv in raspberry pi
Getting started with open cv in raspberry piGetting started with open cv in raspberry pi
Getting started with open cv in raspberry pi
 
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
 
Digital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingDigital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meeting
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
 
september13.ppt
september13.pptseptember13.ppt
september13.ppt
 
OpenStack API's and WSGI
OpenStack API's and WSGIOpenStack API's and WSGI
OpenStack API's and WSGI
 
Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extension
 
Where's the source, Luke? : How to find and debug the code behind Plone
Where's the source, Luke? : How to find and debug the code behind PloneWhere's the source, Luke? : How to find and debug the code behind Plone
Where's the source, Luke? : How to find and debug the code behind Plone
 
Drone CI/CD Platform
Drone CI/CD PlatformDrone CI/CD Platform
Drone CI/CD Platform
 
“warpdrive”, making Python web application deployment magically easy.
“warpdrive”, making Python web application deployment magically easy.“warpdrive”, making Python web application deployment magically easy.
“warpdrive”, making Python web application deployment magically easy.
 
E D - Environmental Dependencies in Python
E D - Environmental Dependencies in PythonE D - Environmental Dependencies in Python
E D - Environmental Dependencies in Python
 
Visual Studio Release Management - New weltanschauung or natural evolution? @...
Visual Studio Release Management - New weltanschauung or natural evolution? @...Visual Studio Release Management - New weltanschauung or natural evolution? @...
Visual Studio Release Management - New weltanschauung or natural evolution? @...
 

Kürzlich hochgeladen

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Kürzlich hochgeladen (20)

Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 

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