如何在VS2010中进行SWIG?

Bea*_*Tie 5 c++ python swig visual-studio-2010

大家好,我正在尝试将我在VS2010(c ++)中创建的多文件项目转换为python.我已设法链接Python26.lib文件,并让SWIG为我的主.cpp文件生成包装.cpp文件.我已经设置了我的代码来构建扩展名为.pyd的.dll.

这是我目前的.i文件:

%module HiveGPS

%{
#include "ou_thread.h"
#include "HiveGPS.h"
%}


%include ou_thread.h
%include HiveGPS.h
Run Code Online (Sandbox Code Playgroud)

我已经漫画来获取.py和.pyc文件.现在,根据我的理解,为了运行我的.py文件,我需要将它链接到我的.pyd文件,但是当我尝试使用VS2010使用上面列出的设置构建我的项目时,它会抱怨Thread类我使用:

1>------ Build started: Project: HiveGPS, Configuration: Release Win32 ------
1>Build started 5/11/2011 1:41:30 PM.
1>InitializeBuildStatus:
1>  Touching "Release\HiveGPS.unsuccessfulbuild".
1>ClCompile:
1>  HiveGPS_wrap.cpp
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(57): error C2146: syntax error : missing ';' before identifier 'm_strName'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(64): error C2146: syntax error : missing ';' before identifier 'getName'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(64): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(64): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(64): warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(98): error C2146: syntax error : missing ';' before identifier 'm_strName'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(98): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(98): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(104): error C2146: syntax error : missing ';' before identifier 'getName'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(104): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(104): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(104): warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(115): error C2146: syntax error : missing ';' before identifier 'msg'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(115): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(115): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(118): error C2146: syntax error : missing ';' before identifier 'getMessage'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(118): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(118): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(118): warning C4183: 'getMessage': missing return type; assumed to be a member function returning 'int'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(51): error C2011: 'openutils::Thread' : 'class' type redefinition
1>          c:\users\*\desktop\hivegps\hivegps\ou_thread.h(51) : see declaration of 'openutils::Thread'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(93): error C2011: 'openutils::Mutex' : 'class' type redefinition
1>          c:\users\*\desktop\hivegps\hivegps\ou_thread.h(93) : see declaration of 'openutils::Mutex'
1>c:\users\*\desktop\hivegps\hivegps\ou_thread.h(113): error C2011: 'openutils::ThreadException' : 'class' type redefinition
1>          c:\users\*\desktop\hivegps\hivegps\ou_thread.h(113) : see declaration of 'openutils::ThreadException'
1>c:\users\*\desktop\hivegps\hivegps\HiveGPS.h(29): error C2504: 'openutils::Thread' : base class undefined
1>c:\users\*\desktop\hivegps\hivegps\HiveGPS.h(53): error C2079: 'HiveGPS::readWriteMutex' uses undefined class 'openutils::Mutex'
1>HiveGPS_wrap.cpp(3086): error C2027: use of undefined type 'openutils::Thread'
1>          c:\users\*\desktop\hivegps\hivegps\ou_thread.h(51) : see declaration of 'openutils::Thread'
1>HiveGPS_wrap.cpp(3086): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.10
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

所有这些错误(减去最后一个)基本上都是说String类是以某种方式实现的,它是否与Python对String类的定义相冲突?最后一个错误是抱怨Thread类,可能是同样的问题?

所有这些都说:有人可以告诉我我做错了什么,或者更好的是,请指点我的VS2010如何做到这一点的教程(SWIG网站是2008年).

如果无法做到这一点,我可以将生成的.py文件以某种方式链接到.dll文件吗?

很抱歉很长的解释,但我有点迷失在这一个,所以我决定解释一切.

Maj*_*cRa 11

好的,我正在使用swig和VS2010没有问题......

在您的情况下,看起来更像是链接问题.除了检查库之外,别忘了检查要链接的32-64位库.尝试将所有x86目标链接为故障安全.并且不要使用AnyCpu.


如何在VS2010下使用SWIG.

让我们定义你想要获取mylib.py,所以你用mylib.i创建了一些SWIG接口文件作为"主"文件.我假设你已经有了一个带有C++类项目的解决方案.

(1)首先为SWIG接口创建C++项目.使用Visual C++ - >类库项目,它应该创建一个C++ DLL存根.我把所有.i文件放在那里.并设置视觉工作室以突出.i为.h - 它很方便.

(1.1)将mylib_wrap.cxx文件添加到项目中(创建空文件,而swig尚未生成一个)

(2)
a)在mylib.i上按右键,选择属性.
b)将ItemType设置为"自定义构建工具".
在自定义生成步骤窗口中:
c)命令行字段应类似于:

echo In order to function correctly, please ensure the following environment variables are
correctly set: 
echo PYTHON_INCLUDE: %PYTHON_INCLUDE% 
echo PYTHON_LIB: %PYTHON_LIB% 
echo on 
C:\swig\swig.exe -c++ -python %(FullPath) 
Run Code Online (Sandbox Code Playgroud)

将C:\ swig\swig.exe更改为SWIG的路径

d)在输出字段中:

$(InputName)_wrap.cxx
Run Code Online (Sandbox Code Playgroud)

自定义构建窗口

(3)转到这个项目属性:

a)C++选项卡 - > 其他包含目录
添加$(PYTHON_INCLUDE); ...

c)链接器 - > 输出文件
路径 - 您需要的\_mylib.pyd

d)链接器 - > 启用增量链接
设置为否(/ INCREMENTAL:NO)

e)链接器 - > 输入 - > 附加依赖
项添加$(PYTHON_LIB); ...

f)C/C++ - > 预编译标题:关闭预编译标题,设置**不使用预编译标题*并删除stdafx文件后

g) 常规选项卡.只需检查是否已设置:
配置类型=动态库(.dll)
字符集=使用Unicode字符集
公共语言运行时支持=无公共语言运行时支持

它汇编.

PS并且不要忘记在系统中设置%PYTHON_INCLUDE%和%PYTHON_LIB%变量.