用SWIG封装C/C++库



我正在进行一个项目,该项目需要使用Topaz Signature Capture pad。我们的产品目前使用Topaz提供的ActiveX控件在网站上收集签名,但我们正试图转向由Python支持的桌面应用程序。Python将允许我们在没有太多额外工作的情况下扩展到多个操作系统。

Topaz提供"C"库(http://www.topazsystems.com/sigpluslibrary.html)用于与他们的设备接口。我相信我已经确定它实际上是C++(使用类和其他C++构造)。我曾尝试使用SWIG和Cython为Python创建一个包装器,但运气并不好。

我的SWIG目录结构是:

  • SigSwig
    • Includes(包含Topaz Shared C Library中的所有标头)
    • setup.py
    • SigLib.i
    • 签名.i
    • 等等

设置.py

# setup.py
import distutils
from distutils.core import setup, Extension
module = Extension(
                        "_SigLib", 
                        ["SigLib.i"],
                        swig_opts=['-c++'],
                        language='c++',
                        library_dirs=['c:Python27'],
                        libraries=["SigLib", "hid", "LibJpeg", "libtiff", "WINTAB32", "WNTAB32X", "zlib"],
                        extra_options=["SigLib.lib", "hid.lib", "LibJpeg.lib", "libtiff.lib", "WINTAB32.LIB", "WNTAB32X.LIB", "zlib.lib"]
                    )
setup(
      name = "Signature Capture",
      version = "1.0",
      ext_modules = [module]
)

SigLib.i

%module SigLib
%{
#define SIGAPI 
#include "IncludeSigLib.h"
%}
%include TabletParameters.i
%include TabletInterface.i
%include LCDInterface.i
%include Signature.i

签名.i

%module Signature
%include "IncludeSignature.h"

我很难理解SWIG的例子,但据我所知,这应该没问题。SigLib.i加载在主标头(SigLib.h)中,其中包括SIGAPI和所有主包含文件的声明。它不包括Signature.h(或其他接口文件中的任何其他头文件),因为它不跟随所有头文件。然后加载其他接口文件。Signature.i和所有其他接口文件只包含对要解析的头的引用。

我尝试了几种不同的方法,这似乎是迄今为止最好的。然而,当我执行setup.py时,我会得到以下错误:

SigLib_wrap.cpp(3417) : error C2065: 'temp' : undeclared identifier
SigLib_wrap.cpp(3418) : error C2065: 'temp' : undeclared identifier
SigLib_wrap.cpp(3418) : error C2059: syntax error : '*'
SigLib_wrap.cpp(3419) : error C2513: 'TabletParameters' : no variable declared before '='
SigLib_wrap.cpp(3419) : error C2065: 'temp' : undeclared identifier
SigLib_wrap.cpp(3420) : error C2065: 'temp' : undeclared identifier
SigLib_wrap.cpp(3420) : error C2541: 'delete' : cannot delete objects that are not pointers
SigLib_wrap.cpp(3432) : error C2275: 'TabletParameters' : illegal use of this type as an expression c:userskevindownloadssigpythonincludeTabletParameters.h(18) : see declaration of 'TabletParameters'

查看生成的代码,大多数错误都包含SIGAPI,如下所示:

SIGAPI * temp;

SIGAPI看起来是一个#define,出现在SigLib.h文件中,用于类定义,如:

class SIGAPI Signature

SigLib.h定义:

#ifdef  SIGLIBDLL
#define SIGAPI  __declspec( dllexport ) 
#else
#define SIGAPI
#endif

我试了几个小时在谷歌上搜索,试图找到解决这个问题的方法,但我运气不好。我在SWIG文档中找不到任何关于它的内容,但老实说,我真的不确定该找什么。

编辑:

做了乔尔戈斯建议的改变,除了加载额外的标题。它现在似乎生成了包含所有所需内容的整个包装文件,但在链接到库时出错。

SigLib.i

%module SigLib
%{
    #include "IncludeSigLib.h"
%}

%include <windows.i>
%include "IncludeSigLib.h"
%include "IncludeSigPoint.h"
%include "IncludeSigStroke.h"
%include "IncludeTabletParameters.h"
%include "IncludeCircularBuffer.h"
%include "IncludeSerialIoIF.h"
%include "IncludeHidIoIF.h"
%include "IncludeUsbIoIF.h"
%include "IncludeSocketIoIF.h"
%include "IncludeNewSocketIoIF.h"
%include "IncludeSimIoIF.h"
%include "IncludeProcessSerialData.h"
%include "IncludeCaptureSig.h"
%include "IncludeTabletPoint.h"
%include "IncludePointBuffer.h"
%include "IncludeTabletInterface.h"
%include "IncludeTabletSampleList.h"
%include "IncludeSigWindowType.h"
%include "IncludeSigFile.h"
%include "Includemd5.h"
%include "IncludeUtilities.h"
%include "IncludeHotSpot.h"
%include "IncludeHotSpotList.h"
%include "IncludeBitmapCharacter.h"
%include "IncludeCharacterMap.h"
%include "IncludeTiffImage.h"
%include "IncludeLCDGraphicBitmap.h"
%include "IncludeLCDInterface.h"

我这样做了,并改变了我加载所有内容的方式。现在它似乎正在编译,但我收到了很多链接错误,比如:

SigLib_wrap.obj : error LNK2019: unresolved external symbol "public: int __thiscall TabletInterface::PointsInPointBuffer(void)" (?PointsInPointBuffer@TabletInterface@@QAEHXZ) referenced in function __wrap_TabletInterface_PointsInPointBuffer
SigLib_wrap.obj : error LNK2019: unresolved external symbol "public: void __thiscall TabletInterface::InitProcessInputData(void)" (?InitProcessInputData@TabletInterface@@QAEXXZ) referenced in function __wrap_TabletInterface_InitProcessInputData
SigLib_wrap.obj : error LNK2019: unresolved external symbol "public: void __thiscall TabletInterface::CloseProcessInputData(void)" (?CloseProcessInputData@TabletInterface@@QAEXXZ) referenced in function __wrap_TabletInterface_CloseProcessInputData

我需要链接SigLib.lib和其他一些lib文件,但我不确定如何从我的setup.py文件中做到这一点,也不确定这是否适用于Python。

来自SWIG文件第3.4段:

在Windows上使用SWIG时的一个常见问题是Microsoft函数调用C++标准中没有的约定。SWIG解析ISOC/C++因此无法处理诸如__declspec(dllimport)、__stdcall等。不过,有一个Windows接口文件Windows.i来处理这些调用约定。这个文件还包含用于处理常用Windows的类型映射特定类型,如__int64、BOOL、DWORD等。像您一样包括它任何其他接口文件,例如:

%include <windows.i>
__declspec(dllexport) ULONG __stdcall foo(DWORD, __int32);

在任何特定于windows的声明之前添加%include <windows.i>可能会解决您的问题。

相关内容

  • 没有找到相关文章

最新更新