- 论坛徽章:
- 2
|
我定义了一个普通函数,.h/.cpp/.i文件,用于给python调用,如下:
$cat example.h
- #pragma once
- int fact(int n);
复制代码 cat example.cpp
- #include "example.h"
- int fact(int n) {
- if (n < 0){ /* This should probably return an error, but this is simpler */
- return 0;
- }
- if (n == 0) {
- return 1;
- }
- else {
- /* testing for overflow would be a good idea here */
- return n * fact(n-1);
- }
- }
复制代码 $cat example.i
- %module example
- %{
- #define SWIG_FILE_WITH_INIT
- #include "example.h"
- %}
- int fact(int n);
复制代码 python的DistUtil用到的文件如下
$cat setup.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"],
- )
复制代码
然后用python的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
复制代码 上面这个编译错误似乎是来自于swig工具生成的代码。我想swig工具本身应该没有什么问题吧,是不是我的代码或者我执行的命令缺少了什么参数之类的?
该如何修改?
|
|