用SWIG封装C/C++库

2024-06-17 11:06:08 发布

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

我有一个项目,我正在工作,需要使用黄玉签名捕捉垫。我们的产品目前使用Topaz提供的ActiveX控件来收集网站上的签名,但我们正在尝试使用Python支持的桌面应用程序。Python将允许我们在没有太多额外工作的情况下扩展到多个操作系统。在

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

我的SWIG目录结构是:

  • 西格斯威格
    • Includes(包含来自Topaz共享C库的所有头文件)
    • 在设置.py在
    • SigLib.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.号

^{pr2}$

签名。我

%module Signature
%include "Include\Signature.h"

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

我尝试了几种不同的方法,这似乎是迄今为止最好的。但是,当我执行设置.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:\users\kevin\downloads\sigpython\include\TabletParameters.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文档中找不到任何关于它的东西,但老实说,我真的不知道该找什么。在

编辑:

除了加载额外的邮件头外,还进行了Giorgos建议的更改。它现在似乎生成了包含所需的所有内容的整个wrap文件,但是链接到库时会出现错误。在

SigLib.i.号

%module SigLib
%{
    #include "Include\SigLib.h"
%}


%include <windows.i>
%include "Include\SigLib.h"
%include "Include\SigPoint.h"
%include "Include\SigStroke.h"
%include "Include\TabletParameters.h"
%include "Include\CircularBuffer.h"
%include "Include\SerialIoIF.h"
%include "Include\HidIoIF.h"
%include "Include\UsbIoIF.h"
%include "Include\SocketIoIF.h"
%include "Include\NewSocketIoIF.h"
%include "Include\SimIoIF.h"
%include "Include\ProcessSerialData.h"
%include "Include\CaptureSig.h"
%include "Include\TabletPoint.h"
%include "Include\PointBuffer.h"
%include "Include\TabletInterface.h"
%include "Include\TabletSampleList.h"
%include "Include\SigWindowType.h"
%include "Include\SigFile.h"
%include "Include\md5.h"
%include "Include\Utilities.h"
%include "Include\HotSpot.h"
%include "Include\HotSpotList.h"
%include "Include\BitmapCharacter.h"
%include "Include\CharacterMap.h"
%include "Include\TiffImage.h"
%include "Include\LCDGraphicBitmap.h"
%include "Include\LCDInterface.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文件,但我不知道如何从设置.py我有一个文件,如果这对Python有效。在


Tags: 文件pyincludeliberrortempcppsignature
1条回答
网友
1楼 · 发布于 2024-06-17 11:06:08

根据SWIG文件第3.4段:

A common problem when using SWIG on Windows are the Microsoft function calling conventions which are not in the C++ standard. SWIG parses ISO C/C++ so cannot deal with proprietary conventions such as __declspec(dllimport), __stdcall etc. There is a Windows interface file, windows.i, to deal with these calling conventions though. The file also contains typemaps for handling commonly used Windows specific types such as __int64, BOOL, DWORD etc. Include it like you would any other interface file, for example:

%include <windows.i>

__declspec(dllexport) ULONG __stdcall foo(DWORD, __int32);

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

相关问题 更多 >