2017-10-20 214 views
0

我將swig和char **指向變量char *的指針(不是char *列表)。我找不到將指針包裝到char *的方法。swig char **作爲指向char *的指針*

其目的是在指針引用的char *中寫入連接的結果。

以下是我的代碼:

文件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); 
} 

文件pointers.h

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

文件pointers.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); 

文件設置.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"], 
    ) 

最後,主要蟒蛇:

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 

import pointers 

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

而且所有的人都用終端命令編譯任何幫助?

[更新的問題]

正如@MarkTolonen建議我試圖改變pointers.i文件以下列方式:

新文件pointers.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上。任何人都可以告訴如何處理它。我真的不知道如何解決我的指針問題。

+0

其中pointers_wrap.cxx來自哪裏? >單個文件中的3k行使我頭暈,即使沒有看到文件 – user463035818

+0

是的,它創建運行文件setup.py。這是通過swig自動創建的文件來構建包裝文件。它在創建swig模塊時需要。 – lorsp000

回答

0

從痛飲3.0文檔,附近的§9.2.1 cpointer.i結束:

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

所以,你將不得不訴諸typemaps。下面是一個例子:

pointers.cpp

我不得不稍微改變你的來源。一個char**參數應該返回分配的指針,而不是釋放它:

#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); 
} 

指針。^ h

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

pointers.i

這個版本痛飲文件的聲明typemaps處理char**輸出參數。

%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!

+0

謝謝!但是編譯你的代碼,我得到了一些錯誤,所有提到'跳到標籤'失敗'在函數'PyObject * _wrap_conc(PyObject *,PyObject *)''和一個註釋:'跨越'_object * obj''的初始化'auto obj ='。你知道爲什麼嗎? – lorsp000

+0

@ lorsp000你的C++編譯器是舊的。將auto改爲PyUnicodeFromString的返回類型。我認爲這是PyObject *。 –

+0

我試圖改變返回類型,但仍然不起作用。另一方面,我有一個g ++ 6.3.0版本,對於PyUnicodeFromString來說太舊了嗎? – lorsp000