关键字: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++"; } |