2011-05-11 40 views
5

嘿大家好, 我想SWIG一个多文件项目,我在VS2010(C++)中做了python。我设法链接了Python26.lib文件,并让SWIG为我的主要.cpp文件生成一个包装器.cpp文件。我已经设置了我的代码来构建一个扩展名为.pyd的.dll。如何在VS2010中SWIG?

这是.i文件我目前:

%module HiveGPS 

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


%include ou_thread.h 
%include HiveGPS.h 

而且我mangaed获得的.py和.pyc文件的文件。现在,根据我的理解,为了运行我的.py文件,我需要将它链接到我的.pyd文件,但是当我尝试使用VS2010使用上面列出的设置构建项目时,它抱怨了Thread类I “M使用:

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

所有这些错误(减去最后一个)的基本上说,String类以某种方式实现的,是相互矛盾与String类的Python的定义是什么?最后一个错误是抱怨Thread类,这可能是同样的问题吗?

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

如果不能这样做,我可以将我生成的.py文件链接到.dll文件吗?

对不起,很长的解释,但我有点失落,所以我决定解释一切。

+1

它可能是任何东西。 SWIG支持相当多,但需要一些结构的帮助。如果没有看到头文件或者如何编译生成的SWIG包装器,很难确定哪里出错。 – 2011-05-12 02:00:19

+0

如果使用的字符串是std :: string,请参见http://www.swig.org/Doc1.3/Library.html – Torleif 2011-05-18 21:16:22

回答

11

好吧,我使用痛饮和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文件添加到项目(创建空文件,而痛饮还没有产生了一个尚未)

(二)
一)在mylib中按下右键。我,选择属性。
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) 

改变C:\痛饮\痛饮。exe文件到您的路径痛饮

d)在输出领域:

$(InputName)_wrap.cxx 

Custom build window

(3)到这个项目属性:

一)C++标签 - >其他包含目录
add $(PYTHON_INCLUDE); ...

C)链接 - >输出文件
路径 - 你需要的 \ _mylib.pyd

d)链接 - >启用增量链接
集作为否(/ INCREMENTAL:NO)

e)连接器输入 - >附加依赖​​
加$(PYTHON_LIB); ...

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

g)常规选项卡。只要检查这些设置:
配置类型=动态库文件(.dll)
字符集=使用Unicode字符集
公共语言运行库支持=无公共语言运行库支持

它编译。

P.S.并且不要忘记在你的系统中设置%PYTHON_INCLUDE%和%PYTHON_LIB%变量。

+1

应该输出.cxx文件必须包含到项目中吗? – innochenti 2012-08-27 14:12:17

+0

是的。我更新了这篇文章。 – MajesticRa 2012-08-27 17:18:54

0

似乎在ou_thread.h中缺少一些#include。

更进一步,在“‘类’重新定义类型”可能表明你没有像

#ifndef __ou_thread_h__ 
#define __ou_thread_h__ 

// your stuff, classes, etc. 

#endif 

在ou_thread.h

这是一个猜测...

相关问题