【问题标题】:Swig C++ with multiple includes to Python moduleSwig C++ 包含多个 Python 模块
【发布时间】:2020-11-25 17:18:46
【问题描述】:

我需要将一些 C++ 代码导出到 Python 模块。我有以下代码:

#include <iostream>
#include <string>
#include <vector>

#include "1.h"
#include "2.h"
#include "3.h"
#include "4.h"
#include "5.h"
#include "6.h"

using namespace EXMPL;

int main(int argc, char *argv[])
{
  ...
  return 0;
}

由于我有很多自定义内容,因此处理此问题的首选方法是什么?我是否必须为每个包含制作 .i 接口文件,然后将其添加到主接口文件中?

我应该怎么做?提前致谢!

【问题讨论】:

    标签: python c++ swig


    【解决方案1】:

    最简单的方法是为此源代码创建一个头文件,然后在其中执行所有包含。所以 file.h 包含:

    #include <iostream>
    #include <string>
    #include <vector>
    
    #include "1.h"
    #include "2.h"
    #include "3.h"
    #include "4.h"
    #include "5.h"
    #include "6.h"
    

    然后:

    /* file.i */
     %module file
     %{
    #include "file.h"
    %}
    
    %include "file.h"
    

    第一步当然不是必须的。您也可以将所有包含添加到一个接口文件中

    /* file.i */
     %module file
     %{
    #include "1.h"
    #include "2.h"
    #include "3.h"
    #include "4.h"
    #include "5.h"
    #include "6.h"
    %}
    
    %include "1.h"
    %include "2.h"
    %include "3.h"
    %include "4.h"
    %include "5.h"
    %include "6.h"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多