Python和C++交互

摘要:
关键词:Python 2.7,VS2010,swigOS:Win8.1,带更新。MyApp依赖MyDll,MyPython依赖MyDl。%模块MyPython%{#include“../MyDll/MyDll.h”%}%featureCMyDll;%include%include %包含“../MyDll/MyDll.h”9。在MyPython的属性设置中设置CustomBuildTool。i、 修改MyApp的项目属性,添加python头文件文件夹和lib文件文件夹。

关键字:Python 2.7,VS 2010,swig

OS:Win8.1 with update。

1.下载swig:http://www.swig.org/download.html

2.将swig的路径添加到环境变量Path,例如set path=C:swigwin-3.0.2。

3.用VS创建一个win32 console application名为MyApp并生成解决方案,编译生成MyApp.exe。

Python和C++交互第1张

4.在MyApp解决方案里面新建一个win32 dll带导出符号项目名为MyDll,编译生成MyDll.dll。

Python和C++交互第2张

5.在MyApp解决方案里面新建一个win32 dll空项目名为MyPython。

Python和C++交互第3张

6.修改项目依赖关系。MyApp依赖于MyDll,MyPython依赖于MyDll。

7.修改CMyDll类的实现如下:

MyDll.h

#pragma once

#include <string>
using namespace std;


#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

// This class is exported from the MyDll.dll
class MYDLL_API CMyDll {
public:
    CMyDll(void);
    virtual ~CMyDll() {}

    string SayHello(string name);
    double add(double i, double j);
    virtual string location();
};

MyDll.cpp

#include "stdafx.h"
#include "MyDll.h"


CMyDll::CMyDll()
{
    return;
}

string CMyDll::SayHello(string name)
{
    return "Hello " + name + ". I'm from " + location() + ".";
}

double CMyDll::add(double i, double j)
{
    return i + j;
}

string CMyDll::location()
{
     return "C++";
}

8.在MyPython项目里添加接口文件MyPython.i。

%module(directors="1") MyPython

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

%feature("director") CMyDll;

%include <windows.i>
%include <std_string.i>
%include "../MyDll/MyDll.h"

9.在MyPython.i的属性设置里面设置Custom Build Tool。

Python和C++交互第4张

10.编译MyPython.i生成MyPython.py和MyPython_wrap.cxx,把MyPython_wrap.cxx添加到工程MyPython,并设置工程如下,Build工程MyPython生成_MyPython.pyd.

Python和C++交互第5张

Python和C++交互第6张

Python和C++交互第7张

Python和C++交互第8张

11.添加TestMyPython.py如下,在_MyPython.pyd的文件夹目录下运行TestMyPython.py.

import MyPython
import os

o = MyPython.CMyDll()
print(o.SayHello("World"))

class MyPyDll(MyPython.CMyDll):
    def __init__(self):
        MyPython.CMyDll.__init__(self)
    def location(self):
        return "Python"

o1 = MyPyDll();
print(o1.SayHello("World"))
os.system("pause")

运行结果如下:

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

12.Run python in MyApp。

修改MyApp的工程属性,添加python头文件的文件夹,和lib文件的文件夹。

修改MyApp.cpp的代码如下:

#include "stdafx.h"

#include <Python.h>

int _tmain(int argc, _TCHAR* argv[])
{
    Py_Initialize();

    PyObject * pModule = PyImport_ImportModule("TestMyPython");

    Py_Finalize();
    return 0;
}

编译运行MyApp.exe,结果如下:

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

源代码下载:https://github.com/ldlchina/CppPythonSwig/tree/839e3e50993d209c83c4b5c587369c98a8f05d5a

免责声明:文章转载自《Python和C++交互》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇width:100%和width:inheritRequestHeaders添加自定义参数下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python 并发总结,多线程,多进程,异步IO

1 测量函数运行时间 importtime defprofile(func): def wrapper(*args, **kwargs): importtime start =time.time() func(*args, **kwargs) end =time.time()...

python 反射

import timesss##反射:根据字符串的形式去对象(模块)中操作其成员(查找/获取/删除/添加)#根据用户输入内容,导入模块(字符串形式导入模块) inp = input("请输入模块名:") res = __import__(inp) uu = res.md5("123456") print(uu) #字符串方式去模块中查找函数,并执行# g...

python编码及requests乱码问题

1.字符编码简介 1.1. ASCIIASCII(American Standard Code for Information Interchange),是一种单字节的编码。计算机世界里一开始只有英文,而单字节可以表示256个不同的字符,可以表示所有的英文字符和许多的控制符号。不过ASCII只用到了其中的一半(x80以下),这也是MBCS得以实现的基础。...

windows7 上安装python3.8步骤

    今天给小白们写一个在windows7 上安装python3.8的过程。 1、先到https://www.python.org/downloads/官网下载最新版的python, 不要到别的下载网站去下载,里面可能有些广告和插件,且不一定是最新版的,软件还是官网靠谱。 根据你自己的操作系统选择对应点击进去,别搞错了哦,这里点击上图的windows...

利用python将ip转换为10进制

def int2ip(num): data = [] for i in range(4): num ,extra = divmod(num, 256) data.insert(0, str(extra)) return ".".join(data) def ip2int(astr): ipnum = astr.split(".") num = 0 for...

Python ORM框架之 Peewee入门

之前在学Django时,发现它的模型层非常好用,把对数据库的操作映射成对类、对象的操作,避免了我们直接写在Web项目中SQL语句,当时想,如果这个模型层可以独立出来使用就好了,那我们平台操作数据库也可以这么玩了,我不喜欢写SQL语句。 后来才知道,原来这个叫ORM(Object Relational Mapping,对象关系映射),在Python下面有很多...