如何让C++/wxWidgets代码在wxPython应用中可用?

0 投票
1 回答
689 浏览
提问于 2025-04-15 23:29

我有一个用C++写的代码库,它大量使用了wxWidgets这个库。现在我想把这个库包装一下(目前是用SWIG),让它可以从wxPython中调用,但我遇到了麻烦:

------ Build started: Project: MyLibLib, Configuration: Release_SWIG_OutputForBin Win32 ------
Performing Custom Build Step
In order to function correctly, please ensure the following environment variables are correctly set:
PYTHON_INCLUDE: C:\Python26\include
PYTHON_LIB: C:\Python26\libs\python26.lib
d:\MyProject\Software\MyLib\trunk\MyLib>C:\swigwin-2.0.0\swig.exe -python d:\MyProject\Software\MyLib\trunk\MyLibLib\MyLib.i 
d:\MyProject\Software\MyLib\trunk\MyLib>if errorlevel 1 goto VCReportError 
d:\MyProject\Software\MyLib\trunk\MyLib>goto VCEnd 
Compiling...
MyLib_wrap.c
C:\wxWidgets-2.8.10\include\wx/wxchar.h(886) : warning C4273: '_snprintf' : inconsistent dll linkage
        c:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(358) : see previous definition of '_snprintf'
C:\wxWidgets-2.8.10\include\wx/buffer.h(127) : error C2061: syntax error : identifier 'wxCharBuffer'
C:\wxWidgets-2.8.10\include\wx/buffer.h(127) : error C2059: syntax error : ';'
C:\wxWidgets-2.8.10\include\wx/buffer.h(127) : error C2449: found '{' at file scope (missing function header?)
C:\wxWidgets-2.8.10\include\wx/buffer.h(127) : error C2059: syntax error : '}'
C:\wxWidgets-2.8.10\include\wx/buffer.h(127) : error C2059: syntax error : ')'
C:\wxWidgets-2.8.10\include\wx/buffer.h(129) : error C2061: syntax error : identifier 'wxWritableCharBuffer'
C:\wxWidgets-2.8.10\include\wx/buffer.h(129) : error C2059: syntax error : ';'
C:\wxWidgets-2.8.10\include\wx/buffer.h(129) : error C2059: syntax error : ':'
C:\wxWidgets-2.8.10\include\wx/buffer.h(134) : error C2061: syntax error : identifier 'wxWCharBuffer'
C:\wxWidgets-2.8.10\include\wx/buffer.h(134) : error C2059: syntax error : ';'
C:\wxWidgets-2.8.10\include\wx/buffer.h(134) : error C2449: found '{' at file scope (missing function header?)
C:\wxWidgets-2.8.10\include\wx/buffer.h(134) : error C2059: syntax error : '}'
C:\wxWidgets-2.8.10\include\wx/buffer.h(134) : fatal error C1004: unexpected end-of-file found
Build log was saved at "file://d:\MyProject\Software\MyLib\trunk\MyLib\Release_SWIG_OutputForBin\BuildLog.htm"
MyLibLib - 13 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我应该怎么做才好呢?我花了一些时间在网上搜索类似的错误,但没有找到相关的信息,这让我觉得我可能走错方向了……?

[编辑] 是不是用dll和ctypes可以解决这个问题?

[编辑] 我试着把主要的wx头文件加上去,但似乎没有解决问题:

%{
#include <wx/wx.h>
%}

我是不是得一个一个手动添加wxWidgets的所有头文件?要知道它们的数量可真是非常非常多

1 个回答

0

看起来你的 Mylib.i 文件在生成的 C 文件中没有包含定义 wxCharBuffer 等的头文件(或者可能缺少一些 #define,导致这些包含没有正常工作,但这种可能性较小)。你有需要的

%{
 #include "wxwhatever.h"
%}

和其他相关内容在你的 .i 文件中吗?你能查看一下生成的 .c 文件,看看有哪些包含指令是存在的,或者缺失的(或者可能顺序不对)吗?

我认为你正在尝试的任务没有特别复杂的地方,那些编译器给出的错误信息只是告诉我“缺少包含文件或类似的问题”...

撰写回答