关键字: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. python生成简单的验证码

    #coding=utf-8 from PIL import Image, ImageDraw, ImageFont, ImageFilter import random # 随机字母: def rnd ...

  2. CSS——选择器

    css选择器 css选择器可分为:标签(元素)选择器,ID选择器,类选择器,属性选择器,后代选择器,子代选择器,相邻兄弟选择器和兄弟选择器.... 标签选择器: //E{attr:value;attr ...

  3. Android_Menu_contextMenu

    xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  4. 自定义ListView适配器Adapter引用布局文件的情况下实现点击列表项时背景颜色为灰色

    listview控件设置适配器的时候,如果使用自定义的adapter,比如MyArrayAdapter extends ArrayAdapter<String> 如果listitem布局文 ...

  5. hackerrank Day15: Linked List

    #include <iostream> #include <cstddef> using namespace std; class Node { public: int dat ...

  6. 【高级JEE技术】JMX

    JMX即Java Manager Extentin(java 管理扩展)一种动态改变javabean属性值的技术,具体应用场景可以有很多.比如使用JMX作为线上应用的开关,在做一些新老系统改造的时候 ...

  7. Linux VIM python 自动补全插件:pydiction

    Pydiction 可以是我们使用Tab键自动补全Python代码在Vim,是一款非常不错的插件. Pydiction不需要安装,所有没有任何依赖包问题,Pydiction主要包含三个文件. pyth ...

  8. ACM——数的计算

    数的计算——(递归(超时)和非递归) 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:1050            测试通过:31 ...

  9. 转-----EasyCHM制作教程

    希望以后自己的笔记能够整理成 chm 格式的文档 制作过CHM帮助文件的同志们可能都遇到过以下两个问题: 1.制作好的CHM文件图像.公式不显示. 2.制作好的CHM文件在自己电脑上能显示,在别人电脑 ...

  10. wpf打印控件 实现分页打印控件功能

    因为 要实现打印 wpf  listbox控件  数据特别多 要打印在 几张纸上    找了几天 都没有找到相关的例子 现在 解决了 在这里和大家分享一下 public void print(Fram ...