swig char **作为指向char的指针* [英] swig char ** as a pointer to a char *

查看:92
本文介绍了swig char **作为指向char的指针*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用swig和char **作为指向变量char *的指针时遇到麻烦(不是作为char *的列表!!!).我找不到将指针包装到char *的方法.

I'm having trouble with swig and char ** as pointer to a variable char * (not as a list of char *!!!). I couldn't find out a way to wrap the pointer to a char *.

目标是将级联的结果写入指针所引用的char *中.

The aim is to write the result of the concatenation in the char * referenced by the pointer.

以下是我的代码:

文件pointers.cpp:

File pointers.cpp:

#include "pointers.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

void conc(char *str1, char *str2, char **res){
  char *aux = (char *)malloc(strlen(str1)+strlen(str2)+1);
  strcpy(aux,str1);
  strcat(aux,str2);
  strcpy(*res,aux);
  free(aux);
}

文件指针.h

void conc(char *str1, char *str2, char **res)

文件指针.i

%module pointers
%{
#define SWIG_FILE_WITH_INIT
#include "pointers.h"
%}

%include "typemaps.i"
%include "cpointer.i"
%include "cstring.i"

%pointer_functions(char *, charp);
extern void conc(char *str1, char *str2, char **res);

文件setup.py:

File setup.py:

from distutils.core import setup, Extension


pointers_module = Extension('_pointers',
                       sources=['pointers_wrap.cxx', 'pointers.cpp'],
                       )

setup (name = 'pointers',
   version = '0.1',
   author      = "SWIG Docs",
   description = """Simple swig example from docs""",
   ext_modules = [pointers_module],
   py_modules = ["pointers"],
   )

最后是python main:

Finally, the python main:

import pointers

result = new_charp()
pointers.conc("Hello ","World!", result);
print(result)
delete_charp(result)

所有这些都是使用终端命令编译的:

And all of them are compiled with terminal commands:

swig -c++ -python pointers.i
python setup.py build_ext --inplace 

但是编译器返回错误:

pointers_wrap.cxx: In function ‘char** copy_charp(char*)’:
pointers_wrap.cxx:3124:58: error: invalid static_cast from type ‘char*’ to            
type ‘const char*&’ return (new char *(static_cast< const char *& >(value)));
                                                                         ^
error: command 'gcc' failed with exit status 1

有帮助吗?

[问题更新]

按照@MarkTolonen的建议,我尝试通过以下方式更改pointers.i文件:

As suggested by @MarkTolonen I tried to change the pointers.i file in the following way:

新文件指针.i:

%module pointers

%{
#include "pointers.h"
%}

// This input typemap declares that char** requires no input parameter.
// Instead, the address of a local char* is used to call the function.
%typemap(in,numinputs=0) char** (char* tmp) %{
  $1 = &tmp;
%}

// After the function is called, the char** parameter contains a malloc'ed char* pointer.
// Construct a Python Unicode object (I'm using Python 3) and append it to
// any existing return value for the wrapper.
%typemap(argout) char** %{
  PyObject *obj = PyUnicode_FromString(*$1);
  $result = SWIG_Python_AppendOutput($result,obj);
%}

// The malloc'ed pointer is no longer needed, so make sure it is freed.
%typemap(freearg) char** %{
  free(*$1);
%}

// Now that the typemap exists, let swig wrap the header.
%include "pointers.h"

编译方式:

swig -c++ -python pointers.i
g++ --std=c++11 -fPIC -c pointers.cpp
g++ --std=c++11 -fPIC -c pointers_wrap.cxx -I/usr/local/include/python3.6m

然后我得到了错误:

In function ‘PyObject* _wrap_conc(PyObject*, PyObject*):`
pointers_wrap.cxx:3618:1: error: jump to label ‘fail’ [-fpermissive]
fail:
pointers_wrap.cxx:1222:49: note:   from here
 #define SWIG_fail                  goto fail

pointers_wrap.cxx:2999:68: note: in expansion of macro ‘SWIG_fail’
 #define SWIG_exception_fail(code, msg) do { SWIG_Error(code, msg); SWIG_fail; } while(0)
pointers_wrap.cxx:3603:5: note: in expansion of macro ‘SWIG_exception_fail’
 SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "conc" "', argument " "2"" of type '" "char *""'");

pointers_wrap.cxx:3609:8: note:   crosses initialization of ‘_object* obj’
 auto obj = PyUnicode_FromString(*arg3);
pointers_wrap.cxx:3618:1: error: jump to label ‘fail’ [-fpermissive]
fail:

pointers_wrap.cxx:1222:49: note:   from here
 #define SWIG_fail                     goto fail
pointers_wrap.cxx:2999:68: note: in expansion of macro ‘SWIG_fail’
 #define SWIG_exception_fail(code, msg) do { SWIG_Error(code, msg); SWIG_fail; } while(0)
pointers_wrap.cxx:3598:5: note: in expansion of macro ‘SWIG_exception_fail’
 SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "conc" "', argument " "1"" of type '" "char *""'");

pointers_wrap.cxx:3609:8: note:   crosses initialization of ‘_object* obj’
 auto obj = PyUnicode_FromString(*arg3);

pointers_wrap.cxx:3618:1: error: jump to label ‘fail’ [-fpermissive]
fail:
pointers_wrap.cxx:1222:49: note:   from here
 #define SWIG_fail                   goto fail
pointers_wrap.cxx:3595:62: note: in expansion of macro ‘SWIG_fail’
 if (!PyArg_ParseTuple(args,(char *)"OO:conc",&obj0,&obj1)) SWIG_fail;
pointers_wrap.cxx:3609:8: note:   crosses initialization of ‘_object* obj’
 auto obj = PyUnicode_FromString(*arg3);

它在此Windows操作系统上有效,但在我的ubuntu上无效.谁能告诉我如何处理它.我真的不知道如何解决指针问题.

It works on this windows OS but not on my ubuntu. Can anyone tell how to deal with it. I really don't know how resolve my pointers problem.

推荐答案

从SWIG 3.0文档中,在§9.2.1 cpointer.i末尾附近:

From the SWIG 3.0 documentation, near the end of §9.2.1 cpointer.i:

注意:这些宏都不能用于安全地使用字符串(char *或char **).

Note: None of these macros can be used to safely work with strings (char * or char **).

因此,您将不得不求助于typemap.下面是一个示例:

So you will have to resort to typemaps. Below is an example:

pointers.cpp

我不得不稍微改变一下您的出处. char**参数应该返回分配的指针,而不是释放它:

I had to change your source slightly. A char** argument should return the allocated pointer, and not free it:

#include <string.h>
#include <stdlib.h>
#include "pointers.h"

void conc(char *str1, char *str2, char **res){
  *res = (char *)malloc(strlen(str1)+strlen(str2)+1);
  strcpy(*res,str1);
  strcat(*res,str2);
}

pointers.h

void conc(char *str1, char *str2, char **res);

pointers.i

此版本的swig文件声明了用于处理char**输出参数的类型映射.

This version of the swig file declares the typemaps to handle the char** output argument.

%module pointers

%{
#include "pointers.h"
%}

// This input typemap declares that char** requires no input parameter.
// Instead, the address of a local char* is used to call the function.
%typemap(in,numinputs=0) char** (char* tmp) %{
    $1 = &tmp;
%}

// After the function is called, the char** parameter contains a malloc'ed char* pointer.
// Construct a Python Unicode object (I'm using Python 3) and append it to
// any existing return value for the wrapper.
%typemap(argout) char** (PyObject* obj) %{
    obj = PyUnicode_FromString(*$1);
    $result = SWIG_Python_AppendOutput($result,obj);
%}

// The malloc'ed pointer is no longer needed, so make sure it is freed.
%typemap(freearg) char** %{
    free(*$1);
%}

// Now that the typemap exists, let swig wrap the header.
%include "pointers.h"

test.py

import pointers
result = pointers.conc('Hello ','World!');
print(result)

输出

Hello World!

Hello World!

这篇关于swig char **作为指向char的指针*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆