关键字: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。

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

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

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。

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

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

编译运行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++交互的更多相关文章

  1. Python和Excel交互

    Python和Excel交互 使用的python包为XlsxWriter 下载的链接 https://pypi.python.org/pypi/XlsxWriter 初级的例子: def write_ ...

  2. 【转】Python之系统交互(subprocess)

    [转]Python之系统交互(subprocess) 本节内容 os与commands模块 subprocess模块 subprocess.Popen类 总结 我们几乎可以在任何操作系统上通过命令行指 ...

  3. Python与Mysql交互

    #转载请联系 在写内容之前,先放一张图,bling- 这张图算是比较详细的表达出了web开发都需要什么.用户访问网页,就是访问服务器的网页文件.这些网页文件由前端工程师编写的.服务器通常用nginx/ ...

  4. Python实现用户交互,显示省市县三级联动的选择

    题目:Python实现用户交互,显示省市县三级联动的选择 定义的字典为: dic = { "江西": { "萍乡": ["安源", &quo ...

  5. 二十、Python与Mysql交互

    先安装一个python与MySQL交互的包:MySQL-python $ gunzip MySQL-python-1.2.2.tar.gz $ tar -xvf MySQL-python-1.2.2. ...

  6. python与C++交互

    python和C++能进行有效的交互,c++调用Python的一些小用法 写了一个python脚本导入发生异常,可能是编码问题(如存在中文),Python默认的是ASCII可加上:#!/usr/bin ...

  7. Python之系统交互(subprocess)

    本节内容 os与commands模块 subprocess模块 subprocess.Popen类 总结 我们几乎可以在任何操作系统上通过命令行指令与操作系统进行交互,比如Linux平台下的shell ...

  8. Python的用户交互程序及格式化输出

    1.  用户输入 在Python 3 中,用户输入用input()函数即可实现用户交互程序. 例如,我们根据程序提示输入用户名和密码,并且打印输入的信息. 2. 字符串格式化输出 例如,我们根据程序提 ...

  9. Python与RabbitMQ交互

    RabbitMQ 消息队列 成熟的中间件RabbitMQ.ZeroMQ.ActiveMQ等等 RabbitMQ使用erlang语言开发,使用RabbitMQ前要安装erlang语言 RabbitMQ允 ...

  10. python与mysql交互中的各种坑

    开始学python 交互MySQLdb,踩了很多坑 第一个 %d format: a number is required, not str 参照以下博客: https://blog.csdn.net ...

随机推荐

  1. Linux内核学习笔记2

    http://www.cnblogs.com/bastard/category/412387.html

  2. eclipse启动不了报错java was started but returned exit code=13

    http://zhidao.baidu.com/question/1367307106041927459.html http://zhidao.baidu.com/question/570567914 ...

  3. debian下图形化管理工具的安装

    MySQL GUI Tools是一套图形化桌面应用工具套装,可以用来管理MySQL服务器.该套装工具包含三个工具:MySQL QueryBrowser.MySQL Administrator和MySQ ...

  4. Helpers\Assets

    Helpers\Assets The assets helper is for loading CSS and JS files rather than writing out the full sc ...

  5. sql时间段算法

    需求:领导要求写时间段算法,格式如下 :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: 一小时制 :-: :-: :-: ...

  6. linux 命令学习日记 20160621

    1.cat /proc/meminfo 观察Linux系统上虚拟内存的当前状态 2.ipcs -m ipcs命令专门用来查看系统上的当前共享内存页面 3.ls /dev -al sda* ttyS* ...

  7. 有关于Algorithm的基础介绍

    Niklaus Wirth:Algorithm + Data Structures = Programs 这句话呢,觉得很正确,算法和程序是不同的概念,算法的思想呢有递推,枚举,分治,贪婪,试探法,模 ...

  8. Burosuite抓包Sqlmap学习Sql注入

    在sqlmap中加入--proyxy参数: --proxy "http://127.0.0.1:8080" 如下图所示: 回车以后sqlmap会自动抓到数据包: 我们选择向前(fo ...

  9. JAXB - The JAXB Context

    As we have seen, an object of the class JAXBContext must be constructed as a starting point for othe ...

  10. Drbd 安装配置

    一.Drbd介绍 Distributed Replicated Block Device(DRBD)是基于块设备在不同的高可用服务器之间同步和镜像数据的软件,通过它可以实现在网络中两台服务器这间基于块 ...