gpt4 book ai didi

c++ - 如何在 VS2010 中痛饮?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:52 29 4
gpt4 key购买 nike

大家好, 我正在尝试将我在 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 类我'正在使用:

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

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

最佳答案

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

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


如何在VS2010下使用SWIG。

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

(1) 首先为SWIG接口(interface)创建C++项目。使用应创建 C++ DLL stub 的 Visual C++-> 类库项目。我把所有的 .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

Custom build window

(3) 转到此项目属性:

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

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

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

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

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

g) 常规 标签。只需检查是否已设置:
配置类型 = 动态库 (.dll)
字符集 = 使用 Unicode 字符集
公共(public)语言运行时支持 = 没有公共(public)语言运行时支持

它编译。<​​/p>

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

关于c++ - 如何在 VS2010 中痛饮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5969173/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com