【问题标题】:Doubts on how to do a c wrapper for python using SWIG怀疑如何使用 SWIG 为 python 做一个 c 包装器
【发布时间】:2016-12-25 19:43:11
【问题描述】:

上下文

我有一个用 c 实现的函数,我想在 python 环境中进行测试。经过一番阅读,我决定使用 SWIG 工具。 A 刚刚做了一个简单的例子,展示了here,但我仍然怀疑,所以我决定来这里。 c项目有以下文件

  • ransac2Dline.h
  • ransac2Dline.c
  • svd.h
  • svd.c

我想使用的功能是

int ransac_2Dline(float **data, int n, int maxT, float threshold,
                    float *bestModel, int *bestInliers, int verbose);

在头文件 ransac2Dline.h 中声明,并带有其他辅助功能。标题内容是

#ifndef RANSAC2DLINE_H
#define RANSAC2DLINE_H

#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "svd.hpp"

int ransac_2Dline(float **data, int n, int maxT, float threshold,
                    float *bestModel, int *bestInliers, int verbose);

int randomSelect(float **sel, int nsel, float **data, int *ndata);

int fitModel_line(float *point, float *l, float threshold);

void estimateModel_line(float *l, float **P, int n);

void twoPointsLine(float *l, float **P);

#endif /* RANSAC2DLINE_H */

问题

i) 我想知道我应该如何准确地定义 SWIG 模块 .i。由于我想要一个仅用于 ransac_2Dline 功能的接口,我猜想 SWIG 模块需要处理它。我的意思是这样的

/* File: ransac.i */
%module ransac

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

int ransac_2Dline(float **data, int n, int maxT, float threshold,
                      float *bestModel, int *bestInliers, int verbose);

。这是正确的吗?还是我应该做一些不同的事情?

ii) ransac_2Dline 函数计算的最多导入数据,它没有返回(返回的 int 值只是函数成功完成的指标任务),但通过 *bestModel 点可在外部范围内使用。 SWIG 可以创建一个接口来在 python 环境中模拟这种方法吗?

iii)这种方法是一个好的解决方案还是我应该使用 ctypes 或其他任何方法?

更多信息

我正在使用 Ubuntu 14.04.5 LTS,使用 g++ 编译器和 python vesion 2.7.6。

我将非常感谢任何帮助或提示(这个主题对我来说有点难,因为我是一名机械工程师,有一个愚蠢的想法与机器人一起工作。:))。

【问题讨论】:

    标签: python c swig python-c-api


    【解决方案1】:

    使用gcc(而不是g++)。 也可以使用swig -python ransac_2Dline.i(但没有-c++ 选项)

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      相关资源
      最近更新 更多