如何在VS2010 SWIG? [英] How to SWIG in VS2010?

查看:490
本文介绍了如何在VS2010 SWIG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey Everybody,
我试图SWIG一个多文件项目,我在VS2010(c ++)到python。我设法链接Python26.lib文件,并有SWIG为我的主要.cpp文件生成包装器.cpp文件。我设置我的代码来构建一个扩展名为.pyd的.dll。



这是目前的.i档案:

 模块HiveGPS 

%{
#includeou_thread.h
#includeHiveGPS.h
%}


%include ou_thread.h
%include HiveGPS.h

以获取.py和.pyc文件。现在,从我的理解,为了运行我的.py文件,我需要链接到我的.pyd文件,但是当我试图使用VS2010构建我的项目与上面列出的设置,它抱怨一个Thread类I 'm using:

  1> ------ Build started:Project:HiveGPS,Configuration:Release Win32 --- --- 
1> Build started 5/11/2011 1:41:30 PM。
1> InitializeBuildStatus:
1>触摸Release\HiveGPS.unsuccessfulbuild。
1> ClCompile:
1> HiveGPS_wrap.cpp
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(57):错误C2146:语法错误:在标识符'm_strName'之前缺少'
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(57):错误C4430:缺少类型说明符 - int。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(57):错误C4430:缺少类型说明符。注意:C ++不支持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):错误C4430:缺少类型说明符 - int。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(64):错误C4430:缺少类型说明符。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(64):warning C4183:'getName':missing return类型;假设为返回'int'的成员函数
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(98):错误C2146:语法错误: ;'before identifier'm_strName'
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(98):error C4430:缺少类型说明符 - int。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(98):错误C4430:缺少类型说明符 - int 。注意:C ++不支持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):错误C4430:缺少类型说明符 - int。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(104):错误C4430:缺少类型说明符。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(104):warning C4183:'getName':missing return类型;假设为返回'int'的成员函数
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(115):错误C2146:语法错误: ;'before identifier'msg'
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(115):错误C4430:缺少类型说明符 - int。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(115):错误C4430:缺少类型说明符。注意:C ++不支持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):错误C4430:缺少类型说明符 - int。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(118):错误C4430:缺少类型说明符。注意:C ++不支持default-int
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(118):warning C4183:'getMessage':missing return类型;假设为返回'int'的成员函数
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(51):错误C2011:'openutils :: Thread ':'class'type redefinition
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(51):参见'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):请参见声明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):参见'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):错误C2079:'HiveGPS :: readWriteMutex'使用未定义类'openutils :: Mutex'
1> HiveGPS_wrap.cpp(3086):错误C2027:使用未定义类型'openutils :: Thread'
1> c:\users\ * \desktop\hivegps\hivegps\ou_thread.h(51):参见'openutils :: Thread'的声明
1> HiveGPS_wrap.cpp(3086):fatal error C1903:无法从先前的错误中恢复;停止编译
1>
1>构建失败。
1>
1>时间经过00:00:02.10
========== Build:0成功,1失败,0最新,0跳过====== ====

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



所有的说:
有人可以告诉我我在做什么错误或更好的,指向我如何做VS2010的教程(SWIG网站是2008年)。



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



对不起,长的解释,但我有点失去了这一个,所以我决定解释一切。

解决方案

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



在你的case,它看起来更像你有链接的问题。除了检查库,不要忘记检查正在链接的32-64位库。尝试链接所有作为x86目标作为故障安全。不要使用 AnyCpu






如何在VS2010下使用SWIG。



允许定义您要获取mylib.py,因此您使用mylib.i创建了一些SWIG界面文件作为main文件。我假设你已经有一个项目的解决方案与您的C + +类。



(1)首先创建SWIG界面的C ++项目。使用Visual C ++ - >类库项目应该创建一个C ++ DLL存根。我把所有的.i文件。并设置视觉工作室,突出.i为.h - 它是方便。



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

(2)

a)在mylib.i上按下右键,选择属性。

b)set ItemType为自定义构建工具。

在自定义构建步骤窗口中:

c)命令行字段应类似于:

  echo为了正确运行,请确保以下环境变量是
正确设置:
echo PYTHON_INCLUDE:%PYTHON_INCLUDE%
echo PYTHON_LIB:% PYTHON_LIB%
echo on
C:\swig\swig.exe -c ++ -python%(FullPath)

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



d)在输出字段中:

  $(InputName)_wrap.cxx 


b $ b



(3)转到此项目属性:



a) C ++标签 - >

add $(PYTHON_INCLUDE); ...



c)链接器 - > 输出文件

路径-You-Needed \_mylib.pyd



d)链接器 - > 启用增量链接

设为No(/ INCREMENTAL:NO)



e)链接器 - > 输入 - > 其他依赖关系

add $(PYTHON_LIB); ...



f) C ++ - > 预编译头
关闭预编译头,设置* 不使用预编译头,并在 $ b后删除stdafx文件
$ b

g)常规标签。只需检查这些设置:

配置类型=动态库(.dll)

字符集=使用Unicode字符集

公共语言运行时支持=否公共语言运行时支持



它编译。<​​/ p>

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


Hey Everybody, I'm trying to SWIG a multi file project that I made in VS2010 (c++) to python. I've managed to link the Python26.lib file, and have SWIG generating a wrapper .cpp file for my main .cpp file. I've set my code to build a .dll with the extension .pyd.

This is the .i file I have currently:

%module HiveGPS

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


%include ou_thread.h
%include HiveGPS.h

And I've mangaed to get a .py and .pyc file. Now, from my understanding in order to run my .py file, I need to link it to my .pyd file, but when I try to use VS2010 to build my project with the settings I listed above, it complains about a Thread class I'm using:

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

All of these errors (minus the last one) are basically saying that the String class is implemented somehow, is it conflicting with Python's definition of the String class? The last error is complaining about the Thread class, could that be the same problem?

All of that said: Can somebody tell me what I'm doing wrong, or better yet, point me to a tutorial of how to do this for VS2010 (the SWIG site is for 2008).

And if that can't be done, can I link the .py file I have generated to the .dll file somehow?

Sorry for the long explanation but I'm kinda lost on this one so I decided to explain everything.

解决方案

Ok, I'm using swig and VS2010 with no problems...

In your case, it looks more like you have problems with linking. Beyond the check of libraries, dont forget to check 32-64 bit libraries you are linking. Try to link all as x86 target as fail-safe. And don't use AnyCpu.


How to use SWIG under VS2010.

lets define you want to obtain mylib.py, so you created some SWIG interface files with mylib.i as "main" file. I assume that you already have a solution with project with your C++ classes there.

(1) First create C++ project for SWIG interface. Use Visual C++->Class library project which should create a C++ DLL stub. I put all .i files there. And set visual studio to highlight .i as .h - it is handy.

(1.1) Add mylib_wrap.cxx file to the project (create empty file while swig hasn't generated one yet)

(2)
a) Press right button over the mylib.i, choose properties.
b) set ItemType as "Custom build tool".
In custom build step window:
c) Command line field should be something like:

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) 

change C:\swig\swig.exe to your path to SWIG

d) In Outputs field:

$(InputName)_wrap.cxx

(3) Go to this project properties:

a) C++ tab -> Additional Include Directories
add $(PYTHON_INCLUDE); ...

c) Linker -> Output File
Path-You-Needed\_mylib.pyd

d) Linker -> Enable Incremental Linking
set as No (/INCREMENTAL:NO)

e) Linker -> Input -> Additional Dependencies
add $(PYTHON_LIB);...

f) C/C++ -> Precompiled Headers: Switch off precompiled headers, set *Not Using Precompiled Headers and remove stdafx files after

g) General tab. Just check that these are set:
Configuration type = Dynamic Library (.dll)
Character set = Use Unicode Character Set
Common Language Runtime Support = No Common Language Runtime Support

It compiles.

P.S. And dont forget to set %PYTHON_INCLUDE% and %PYTHON_LIB% variables in your system too.

这篇关于如何在VS2010 SWIG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆