swig编译python c++包装器,gcc生成错误:“const char*无法转换为char*”

2024-05-16 03:13:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试一个非常简单的代码片段,它展示了swig如何将c++模块转换成python可调用模块,它来自internet。我遵循了这些步骤,但似乎当gcc试图编译生成的代码时,它会提示错误。在

以下是我的步骤:

$cat example.h

#pragma once
int fact(int n);

猫示例.cpp在

^{pr2}$

$cat示例。i

%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
int fact(int n);

以及一个python DistUtil文件 $猫设置.cpp.py在

"""
setup.py
"""

from distutils.core import setup, Extension
example_module = Extension('_example',
                           sources=['example_wrap.cxx', 'example.cpp'],
                           )
setup (name = 'example',
       version = '0.1',
       author      = "SWIG Docs",
       description = """Simple swig example from docs""",
       ext_modules = [example_module],
       py_modules = ["example"],
       )

然后使用swig编译

$ swig -c++ -python example.i

$ python setup.cpp.py build_ext --inplace

running build_ext
building '_example' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python2.7 -c example_wrap.cxx -o build/temp.linux-x86_64-2.7/example_wrap.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
In file included from example_wrap.cxx:2554:
example.h:5:20: warning: no newline at end of file
example_wrap.cxx: In function ‘int SWIG_Python_ConvertFunctionPtr(PyObject*, void**, swig_type_info*)’:
example_wrap.cxx:2051: error: invalid conversion from ‘const char*’ to ‘char*’
example_wrap.cxx: In function ‘void SWIG_Python_FixMethods(PyMethodDef*, swig_const_info*, swig_type_info**, swig_type_info**)’:
example_wrap.cxx:3200: error: invalid conversion from ‘const char*’ to ‘char*’
error: command 'gcc' failed with exit status 1

正如您所看到的,我正在编译一个生成的文件,但是gcc提示错误。在

我认为swig在处理这样一个简单的情况时应该不会出错,只是想知道我的代码或命令行选项是否遗漏了什么?在

需要你的专业知识!在


Tags: 代码frompyinfoexamplesetupcxxcpp