如何在VS2010中切换?

2024-06-01 05:50:28 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好, 我正在尝试将在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使用上面列出的设置构建我的项目时,它抱怨我正在使用的线程类:

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文件吗?

很抱歉解释太长了,但我对这个有点迷茫,所以我决定把一切都解释清楚。


Tags: typenotouerrorthreadusersintnote
2条回答

在ou线程中似乎缺少一些include

此外,“'class'类型重新定义”可以表示您没有

#ifndef __ou_thread_h__
#define __ou_thread_h__

// your stuff, classes, etc.

#endif

在你的线上

只是猜测。。。

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

在你的例子中,看起来你在链接方面有问题。除了检查库之外,不要忘记检查正在链接的32-64位库。尝试将所有作为x86目标链接为故障安全。不要使用任何CPU。


如何在VS2010下使用SWIG。

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

< P><强>(1)< /强> SWIG接口的第一个创建C++项目。使用VisualC++ + GT类库项目,创建一个C++ DLL存根。我把所有的文件都放在那里了。并将visual studio设置为高亮。i as.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

Custom build window

(3)转到此项目属性:

a)> > C++ > tab ><强> >附加目录><强> BR/> 添加$(PYTHON_INCLUDE)。。。

c)链接器->;输出文件
您需要的路径

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

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

f)<强> C/C++ +>强> -gt;<强>预编译头< /强>: 关闭预编译头,设置**不使用预编译头*并在之后删除stdafx文件

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

它编译。

另外,不要忘记在系统中设置%PYTHON_INCLUDE%和%PYTHON_LIB%变量。

相关问题 更多 >