Python and C + + interaction

Source: Internet
Author: User

Keyword: Python 2.7,vs 2010,swig

os:win8.1 with update.

1. Download swig:http://www.swig.org/download.html

2. Add the path of the swig to the environment variable path, such as set path=c:\swigwin-3.0.2.

3. Use VS to create a Win32 console application named MyApp and build the solution to compile the build MyApp.exe.

4. Create a new Win32 DLL in the MyApp solution with the export symbol project named Mydll, compile to generate MyDll.dll.

5. Create a new Win32 DLL in the MyApp solution. Empty project named Mypython.

6. Modify the project dependencies. MyApp relies on Mydll,mypython to rely on Mydll.

7. The implementation of the Cmydll class is modified as follows:

MyDll.h

#pragmaOnce#include<string>using namespacestd; #ifdef mydll_exports#defineMydll_api __declspec (dllexport)#else#defineMydll_api __declspec (dllimport)#endif//This class is exported from the MyDll.dllclassMydll_api Cmydll { Public: Cmydll (void); Virtual~Cmydll () {}stringSayHello (stringname); DoubleAddDoubleIDoublej); Virtual stringLocation ();};

MyDll.cpp

#include"stdafx.h"#include"MyDll.h"Cmydll::cmydll () {return;}stringCmydll::sayhello (stringname) {    return "Hello"+ name +". I ' M from"+ location () +".";}DoubleCmydll::add (DoubleIDoublej) {    returni +J;}stringcmydll::location () {return "C + +";}

8. Add the interface file Mypython.i in the Mypython project.

%module (directors= "1") Mypython

%{
#include ". /mydll/mydll.h "
%}

%feature ("director") Cmydll;

%include <windows.i>
%include <stl.i>
%include ". /mydll/mydll.h "

9. Set the custom Build Tool in the Mypython.i property settings.

10. Compile mypython.i generate mypython.py and mypython_wrap.cxx, add mypython_wrap.cxx to Project Mypython, and set the project as follows, build project Mypython Generate _ Mypython.pyd.

11. Add testmypython.py below and run testmypython.py in the _mypython.pyd folder directory.

ImportMypythonImportOso=Mypython.cmydll ()Print(O.sayhello (" World"))classMypydll (mypython.cmydll):def __init__(self): Mypython.cmydll.__init__(self)defLocation (self):return "Python"O1=Mypydll ();Print(O1. SayHello (" World")) Os.system ("Pause")

The results of the operation are as follows:

Hello World. I ' m from C + +.
Hello World. I ' m from Python.
Press any key to continue ...

12.Run python in MyApp.

Modify the project properties of MyApp, add the Python header file folder, and the Lib file folder.

The code to modify MyApp.cpp is as follows:

" stdafx.h "  <Python.h>int _tmain (int argc, _tchar* argv[]) {    py_initialize ();     * Pmodule = Pyimport_importmodule ("testmypython");    Py_finalize ();     return 0 ;}

Compile run MyApp.exe with the following results:

Hello Ldlchina. I ' m from C + +.
Hello World. I ' m from Python.
Press any key to continue ...

Source code Download: https://github.com/ldlchina/CppPythonSwig/tree/839e3e50993d209c83c4b5c587369c98a8f05d5a

Python and C + + interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.