如何在VS2010中SWIG?

发布于 2024-11-06 08:46:31 字数 5348 浏览 3 评论 0原文

嘿大家, 我正在尝试将我在 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

并且我已经获得了 .py 和 .pyc 文件。现在,根据我的理解,为了运行我的 .py 文件,我需要将其链接到我的 .pyd 文件,但是当我尝试使用 VS2010 使用上面列出的设置构建我的项目时,它会抱怨 Thread 类我'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 ==========

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

所有这些都表明: 有人可以告诉我我做错了什么,或者更好的是,给我指点如何在 VS2010 上执行此操作的教程(SWIG 网站适用于 2008 年)。

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

抱歉这么长的解释,但我有点迷失在这个问题上,所以我决定解释一切。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

峩卟喜欢 2024-11-13 08:46:31

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

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


如何在VS2010下使用SWIG。

让我们定义您想要获取mylib.py,因此您创建了一些SWIG接口文件,其中mylib.i作为“主”文件。我假设您已经有了一个带有 C++ 类的项目解决方案。

(1) 首先为 SWIG 接口创建 C++ 项目。使用 Visual C++-> 类库项目,该项目应创建 C++ DLL 存根。我把所有 .i 文件放在那里。并将 Visual Studio 设置为突出显示 .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) 

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

d) 在输出字段中:

$(InputName)_wrap.cxx

自定义构建窗口

(3) 转到此项目属性:

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

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

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

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

f) C/C++ -> 预编译标头
关闭预编译头,设置 **不使用预编译头* 并在

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

它可以编译。

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

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

Custom build window

(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.

轻许诺言 2024-11-13 08:46:31

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

没有类似的内容,

#ifndef __ou_thread_h__
#define __ou_thread_h__

// your stuff, classes, etc.

#endif

此外,“'class'类型重新定义”可以表明您的 ou_thread.h 中

这是一个猜测......

It seems that some #include is missing in the ou_thread.h.

Further more, the "'class' type redefinition" can indicate that you don't have something like

#ifndef __ou_thread_h__
#define __ou_thread_h__

// your stuff, classes, etc.

#endif

in your ou_thread.h

It's a guessing...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文