VS2012 + SWIG Python

来源:互联网 发布:c语言有什么用? 编辑:程序博客网 时间:2024/06/01 09:15

平台:window 8, Visual Studio 2012, Python 2.7(x64)

1. 下载swig安装包

首先在www.swig.org下载swig安装包,把文档也下载下来以备日后使用。解压安装包到任意路径,路径中最好不要带空格。

2. 制作源文件和swig接口文件

我的文件如下

C/C++ code/* sample.h */#ifndef SAMPLE_H#define SAMPLE_Hint compute(int a, int b);#endif
/* sample.cpp */#include <iostream>#include "sample.h"using namespace std;int compute(int a, int b){    int temp = (a+b)*(a-b);    return temp;}
/* File : sample.i */%module sample%inline %{#include "sample.h"%}int compute(int a,int b);

3. 使用swig生成部分文件

'swig -c++ -python jsample.i'#这条语句可以用批处理程序,即新建一个.bat文件,写入这条语句。#要先把swig加入环境变量才能用swig的语句#如果是C语言的话就是'swig -python jsample.i'

执行完第三步后在当前目录下会生成两个文件sample_wrap.cxx和sample.py。
这里写图片描述

4. 配置Visual Studio 2012

4.1 新建控制台工程,选择空白项,输出为DLL

原文新建一个Win32 Console Application工程,在向导中点next,Application type选择DLL,在Additional options中选择Empty project。向Header Files中加入jsample.h,向Source File中加入jsample.cpp和jsample_wrap.cxx,向工程中加入jsample.i,这是可能会弹出一个对话框,我选的是'否'。然后再VS中project>Properties中点Configuration Manager,设置Configration为Release,Platform为Win32。  最后Build Solution,在Release文件夹中会生成jsample.dll,把它改名成_jsample.pyd。把jsample.py,_jsample.pyd和测试文件放到同一个文件夹中测试。  

4.2 配置VC++目录

在工程上右击,选择属性,平台默认是win32,如果用的python的64位的,就要改成x64,
由于PYTHON是64版本,所以将解决方案平台定义为‘ X64’。这里没改之前是’WIN32’,编译可以成功,但是链接时会出现错误:

  error LNK2019: 无法解析的外部符号 __imp__PyObject_GetAttrString,该符号在函数 _main 中被引用  error LNK2019: 无法解析的外部符号 __imp__Py_Initialize,该符号在函数 _main 中被引用  error LNK2019: 无法解析的外部符号 __imp__Py_Finalize,该符号在函数 _main 中被引用  error LNK2019: 无法解析的外部符号 __imp__PyEval_CallObjectWithKeywords,该符号在函数 _main 中被引用  error LNK2019: 无法解析的外部符号 __imp__PyImport_ImportModule,该符号在函数 _main 中被引用

修改平台后,编译链接正常。

下面在包含目录中引入python.h的路径(我的是E:\Anaconda\include),
库目录中引入python27.lib和python27_d.lib的路径(我的是E:\Anaconda\libs),
如果没有python27_d.lib,就把python27.lib复制一份改个名字。
但是不知道python27和python27_d有什么区别?????????
这里写图片描述

原文:好像也可以配置成环境变量PYTHON_INCLUDE和PYTHON_LIB,也最好把文件拷到不到空格的路径下,不过我没有试。我说一下在VS中的配置方法:tools>options>Projects and Solutions>VC++ Directories中,在show directories for 'Include files'中加入如'D:\Program Files\python26\include';在show dirctories for 'Library files'中加入如'D:\Program_runing_data\libs\VSlibs\Python\libs\python26.lib'。这是我从python的安转路径中拷到不带空格的路径中的,如果带空格的话,就得在工程中也加入python26.lib,

4.3 可能问题

如果在vs 200x 下 debug 模式出现链接问题

extmodule.obj : error LNK2019: unresolved external symbol __imp___Py_Dealloc referenced in function _PySwigObject_formatextmodule.obj : error LNK2019: unresolved external symbol __imp___Py_NegativeRefcount referenced in function _PySwigObject_formatextmodule.obj : error LNK2001: unresolved external symbol __imp___Py_RefTotalextmodule.obj : error LNK2019: unresolved external symbol __imp___PyObject_DebugFree referenced in function _PySwigObject_deallocextmodule.obj : error LNK2019: unresolved external symbol __imp___PyObject_DebugMalloc referenced in function _PySwigObject_Newextmodule.obj : error LNK2019: unresolved external symbol __imp__Py_InitModule4TraceRefs referenced in function _init_extmodule

主要是因为 Py_DEBUG/Py_TRACE_REFS 引起, 修改 Python\include 下的 pyconfig.h, object.h 两个文件就行了 …

1. 修改 pyconfig.h 修改 #ifdef _DEBUG # define Py_DEBUG #endif #ifdef _DEBUG //# define Py_DEBUG #endif 修改 # ifdef _DEBUG # pragma comment(lib,"python24_d.lib") # else # pragma comment(lib,"python24.lib") # endif /* _DEBUG */ # ifdef _DEBUG # pragma comment(lib,"python24.lib") # else # pragma comment(lib,"python24.lib") # endif /* _DEBUG */ 2. object.h 修改 #if defined(Py_DEBUG) && !defined(Py_TRACE_REFS) #define Py_TRACE_REFS #endif #if defined(Py_DEBUG) && !defined(Py_TRACE_REFS) // #define Py_TRACE_REFS #endif

4.4 生成pyd文件

把4.2生成的dll文件改后缀名为pyd,但是前面要加一个横杠“_”,如_sample.pyd,
把_sample.pyd 和sample.py 都放入python的库文件夹中(我的是E:\Anaconda\Lib)
然后就可以:

import sampleprint('this is the test running.\n')aa = sample.compute(5,78)print(aa)raw_input("press the enter key to exit.")

大功告成!

5.参考内容

参考内容:

  • [swig]import error:dynamic module does not define init function

  • (Swig to python) import error:dynamic module does not define init function

  • 谁能出个详细的visual studio 使用swig的教程
  • 在VS2012中C++嵌入python
  • 在 C++ 中使用 Python script
0 0
原创粉丝点击