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

using namespacestd;

#ifdef MYDLL_EXPORTS#define MYDLL_API __declspec(dllexport)

#else

#define MYDLL_API __declspec(dllimport)

#endif

//This class is exported from the MyDll.dll

classMYDLL_API CMyDll {public:

CMyDll(void);virtual ~CMyDll() {}string SayHello(stringname);double add(double i, doublej);virtual stringlocation();

};

MyDll.cpp

#include "stdafx.h"#include"MyDll.h"CMyDll::CMyDll()

{return;

}string CMyDll::SayHello(stringname)

{return "Hello" + name + ". I'm from" + location() + ".";

}double CMyDll::add(double i, doublej)

{return i +j;

}stringCMyDll::location()

{return "C++";

}

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

%module(directors="1") MyPython

%{

#include "../MyDll/MyDll.h"

%}

%feature("director") CMyDll;

%include

%include

%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.

importMyPythonimportos

o=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")

运行结果如下:

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

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 . . .

c++和python有联系吗_Python和C++交互相关推荐

  1. 用python计算工资工资_python小编程------交互界面计算工资(五险一金不按比例缴纳)...

    # coding:utf-8 from Tkinter import * root = Tk() Label(root, text='应发总工资 :').grid(row=0, column=0) # ...

  2. vscode使用教程python-VSCode下好用的Python插件及配置_python

    这篇文章主要介绍了微软官方的Python插件,已经自带很多功能,下面是插件功能描述,其中部分内容我做了翻译,需要的朋友可以参考下 MS Python插件. 这是微软官方的Python插件,已经自带很多 ...

  3. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  4. python中定义数据结构_Python中的数据结构—简介

    python中定义数据结构 You have multiples algorithms, the steps of which require fetching the smallest value ...

  5. python 数据框缺失值_Python:处理数据框中的缺失值

    python 数据框缺失值 介绍 (Introduction) In the last article we went through on how to find the missing value ...

  6. python决策树 多分类_Python中的决策树分类:您需要了解的一切

    python决策树 多分类 什么是决策树? (What is Decision Tree?) A decision tree is a decision support tool that uses ...

  7. python基础知识测试题_Python中的单元测试—基础知识

    python基础知识测试题 Unit testing is the number one skill which separates people who just finished their de ...

  8. python技术晨讲_python系列教程14

    声明:在人工智能技术教学期间,不少学生向我提一些python相关的问题,所以为了让同学们掌握更多扩展知识更好的理解人工智能技术,我让助理负责分享这套python系列教程,希望能帮到大家! 好了,是开始 ...

  9. python开发小型数据库_python数据库编程小应用

    python DB api 数据库连接对象connection 数据库交互对象cursor 数据库异常类exceptions 流程: 开始 创建connection 获取cursor 执行查询.执行命 ...

最新文章

  1. 【原创】贴片电容的测量方法。。。这是我从自己QQ空间转过来的,本人实操!...
  2. linux6.4 安装oracle11g 出现错误ORA-01078和LRM-00109错误
  3. C语言 *p++/*(p)++/*(p++)/*p++ - C语言零基础入门教程
  4. java面试题 Arraylist 与 LinkedList比较
  5. 这8个MySQL经典错误
  6. rocketMq配置外网IP
  7. iOS 图片压缩策略
  8. Xilinx FPGA下载器(DLC9)的 [Labtoolstcl 44-494] 报错解决方法
  9. javascript实现繁体简体转换
  10. 如何用html制作移动app,html移动开发app
  11. sql注入-简要SQL注入
  12. 如何查看linux当前版本
  13. WAP上网 AT+CGDCONT=1,IP,cmwap 命令详解
  14. 什么是业务对象?业务逻辑是什么?
  15. Excel公式-提取字符串中首次出现的数值
  16. 【新人报道】新人报道,大家多多关照
  17. 【HTML】P2 HTML 文本标签、图片标签、超链接标签
  18. python画出来的老鼠那么萌
  19. 老鱼Python数据分析——篇十二:使用selenium+BeautifulSoup获取淘股吧数据
  20. 2、sipeed Maix Bit开发板使用小记(二)

热门文章

  1. HTML+CSS+JS实现 ❤️流星天体动画场景特效❤️
  2. 基于Java+jsp+servlet的养老院管理系统设计和实现《收藏版》
  3. 基于Java jsp+servlet超市订单管理平台设计和实现【建议收藏】
  4. Hexo部署出现错误err-Error-Spawn-failed解决方式
  5. 生活中的算法的实际举例_驾校学的技术,在实际生活中,你能运用自如吗?
  6. android物理健代码,Android 物理按键整理及实例代码
  7. Java通过反射访问成员变量
  8. 因子分析——python
  9. 【OpenCV 例程200篇】55. 可分离卷积核
  10. 360浏览器广告太多怎么办_360浏览器如何关闭广告自动推送