将 SWIG 与将 std::string 作为参数的方法一起使用

Posted

技术标签:

【中文标题】将 SWIG 与将 std::string 作为参数的方法一起使用【英文标题】:Using SWIG with methods that take std::string as a parameter 【发布时间】:2012-02-03 01:20:58 【问题描述】:

我使用 SWIG 来包装我的 c++ 类。有些方法有一个const std::string& 作为参数。 SWIG 创建了一个名为 SWIGTYPE_p_std__string 的类型,但是在 c# 中调用该方法时,您不能只为此传递一个普通字符串。下面的示例只是 SWIG 包附带的修改示例。:

public void setName(SWIGTYPE_p_std__string name) 

    examplePINVOKE.Shape_setName(swigCPtr, SWIGTYPE_p_std__string.getCPtr(name));
    if (examplePINVOKE.SWIGPendingException.Pending) throw examplePINVOKE.SWIGPendingException.Retrieve();

在我的接口文件中,我只有:

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

%
#include "example.h"
%

/* Let's just grab the original header file here */
%include "example.h"

而被 C++ 包装的方法是:

void Shape::setName(const std::string& name)

    mName = name;

我必须在接口文件中放入某种类型的映射吗?如果是这样,我该怎么做?

【问题讨论】:

【参考方案1】:

当我发现你的问题时,我正试图自己解决这个问题。

你需要包含

%include "std_string.i" 

在您的 .i 文件中。见:

STL/C++ library

了解更多详情。

【讨论】:

以上是关于将 SWIG 与将 std::string 作为参数的方法一起使用的主要内容,如果未能解决你的问题,请参考以下文章