繁体   English   中英

swig 无法将导出的 c++ 文件包装到 python 中

[英]swig cannot wrap exported c++ file into python

我正在使用swig将 c++ 包装到 python 中。

我的PackA.h是:

#ifndef PACKA_HEADER_H
#define PACKA_HEADER_H

#include <string>
class  PackA
{
public:
    PackA();

};

#endif

我的PackA.i是:

%module PackA

%{
#define SWIG_FILE_WITH_INIT
#include "PackA.h"
%}
%include "PackA.h"

它成功生成了python模块。

但是,如果我通过以下方式将PackA声明为 dll:


// #include "PackAGlobal.h"

#ifndef PACKA_EXPORTES_H
#define PACKA_EXPORTES_H

#if (WIN32)
    #ifdef PackA_EXPORTS
        #define PackA_EXPORT __declspec(dllexport)
    #else
        #define PackA_EXPORT __declspec(dllimport)
    #endif
#else
    #define PackA_EXPORT
#endif


#endif

// PackA.h
#ifndef PACKA_HEADER_H
#define PACKA_HEADER_H

#include "PackAGlobal.h"
#include <string>
class  PackA
{
public:
    PackA();

};

#endif

VS 报告错误:

c4430 missing type specifier - int assumed. note c++ does not support default-int
c2071 abstract declarator illegal storage class
c2513: PackA:  no variable declared before =

如何将此导出类包装到 python 中?

更新 - - - - - - - - - - - - - - - - - - - - - - - - - ----------

感谢@Mark Tolonen 的建议。

我将我的PackA.i修改为:

%module PackA

%{
#define SWIG_FILE_WITH_INIT
#include "PackA.h"
%}
%include <windows.i>
%include "PackA.h"

但是,该错误仍然存​​在。

我已经将示例代码上传到了github: https ://github.com/zhang-qiang-github/SwigTest

终于找到了解决办法,我已经把正确的代码上传到了github: https ://github.com/zhang-qiang-github/SwigTest。 希望它可以帮助某人。

解决方案是:

  1. PackA.i中添加%include "PackAGlobal.h"
  2. PackAWrapping PackA_EXPORTS
SET_TARGET_PROPERTIES(PackAWrapping
    PROPERTIES
    COMPILE_DEFINITIONS PackA_EXPORTS
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM