Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未声明的SWIG包装器(首次在此函数中使用)_Python_C++_Wrapper_Swig - Fatal编程技术网

Python 未声明的SWIG包装器(首次在此函数中使用)

Python 未声明的SWIG包装器(首次在此函数中使用),python,c++,wrapper,swig,Python,C++,Wrapper,Swig,我试图为C++代码创建包装器,以便在Python项目中使用它。 代码取自(主要是mtree.h) 我使用swig通过以下方式生成接口: swig-python-module mtree mtree.h 然后,当我尝试gcc-c-fpic mtree\u wrap.c时,我得到以下错误: user@ubuntu:~/git/M-Tree/cpp2python(master)$ gcc -c -fpic mtree_wrap.c mtree_wrap.c: In function ‘Swig_var

我试图为C++代码创建包装器,以便在Python项目中使用它。 代码取自(主要是
mtree.h

我使用swig通过以下方式生成接口:
swig-python-module mtree mtree.h

然后,当我尝试
gcc-c-fpic mtree\u wrap.c
时,我得到以下错误:

user@ubuntu:~/git/M-Tree/cpp2python(master)$ gcc -c -fpic mtree_wrap.c
mtree_wrap.c: In function ‘Swig_var_mt_set’:
mtree_wrap.c:3029:7: error: ‘mt’ undeclared (first use in this function)
       mt = *((namespace *)(argp));
       ^~
mtree_wrap.c:3029:7: note: each undeclared identifier is reported only once for each function it appears in
mtree_wrap.c:3029:15: error: ‘namespace’ undeclared (first use in this function); did you mean ‘isspace’?
       mt = *((namespace *)(argp));
               ^~~~~~~~~
               isspace
mtree_wrap.c:3029:26: error: expected expression before ‘)’ token
       mt = *((namespace *)(argp));
                          ^
mtree_wrap.c: In function ‘Swig_var_mt_get’:
mtree_wrap.c:3041:47: error: ‘mt’ undeclared (first use in this function)
   pyobj = SWIG_NewPointerObj(SWIG_as_voidptr(&mt), SWIGTYPE_p_namespace,  0 );
                                               ^
mtree_wrap.c:1163:89: note: in definition of macro ‘SWIG_NewPointerObj’
 ointerObj(ptr, type, flags)            SWIG_Python_NewPointerObj(NULL, ptr, type, flags)
                                                                        ^~~
mtree_wrap.c:3041:30: note: in expansion of macro ‘SWIG_as_voidptr’
   pyobj = SWIG_NewPointerObj(SWIG_as_voidptr(&mt), SWIGTYPE_p_namespace,  0 );
                              ^~~~~~~~~~~~~~~
这是发生错误的
mtree.c

SWIGINTERN int Swig_var_mt_set(PyObject *_val) {
  {
    void *argp = 0;
    int res = SWIG_ConvertPtr(_val, &argp, SWIGTYPE_p_namespace,  0 );
    if (!SWIG_IsOK(res)) {
      SWIG_exception_fail(SWIG_ArgError(res), "in variable '""mt""' of type '""namespace""'");
    }
    if (!argp) {
      SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in variable '""mt""' of type '""namespace""'");
    } else {
      mt = *((namespace *)(argp));
    }
  }
  return 0;
fail:
  return 1;
}


SWIGINTERN PyObject *Swig_var_mt_get(void) {
  PyObject *pyobj = 0;

  pyobj = SWIG_NewPointerObj(SWIG_as_voidptr(&mt), SWIGTYPE_p_namespace,  0 );
  return pyobj;
}


首先,使用SIP的C++模式,用代码> -C++ +/Cuff>切换。但这对你没有任何好处。Swig无法为模板类创建包装。您需要创建适当的swg文件并声明应该在python中使用的模板规范。像这样:

%module strmtree;

//generate good wrapper for std::string -> python string
%include "stdstring.i"

%{
//include actualy header in wrapper
#include "mtree.h"
%}

//generate wrapper for class in this file,
//but since the class is template, it won't generate anything on it's own.
%include "mtree.h"

//declare template specialization for string and call it StrMTree.
//ideally you'll get StrMTree class in python
%template(StrMTree) mt::mtree<std::string>;
%模块strmtree;
//为std::string->python字符串生成好的包装器
%包括“stdstring.i”
%{
//在包装器中实际包含标题
#包括“mtree.h”
%}
//为此文件中的类生成包装,
//但由于该类是模板,它不会自己生成任何内容。
%包括“mtree.h”
//声明字符串的模板专门化,并将其称为StrMTree。
//理想情况下,您将获得python中的StrMTree类
%模板(StrMTree)mt::mtree;
不知道mtree模板参数通常应该使用什么类型。如果您想要python对象的mtree,那么它要复杂得多。。。您可能需要C++中的某种接口,MutRead与此接口的专业化,然后在Python中实现接口,