免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2078 | 回复: 0
打印 上一主题 下一主题

[C++] swig编译python-c++接口,gcc出错const char*不能转char* [复制链接]

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 10:16:532015元宵节徽章
日期:2015-03-06 15:53:22
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-12-29 12:59 |只看该作者 |倒序浏览
我定义了一个普通函数,.h/.cpp/.i文件,用于给python调用,如下:
$cat example.h
  1. #pragma once
  2. int fact(int n);
复制代码
cat example.cpp
  1. #include "example.h"
  2. int fact(int n) {
  3.   if (n < 0){ /* This should probably return an error, but this is simpler */
  4.     return 0;
  5.   }
  6.   if (n == 0) {
  7.     return 1;
  8.   }
  9.   else {
  10.      /* testing for overflow would be a good idea here */
  11.     return n * fact(n-1);
  12.   }
  13. }
复制代码
$cat example.i
  1. %module example

  2. %{
  3. #define SWIG_FILE_WITH_INIT
  4. #include "example.h"
  5. %}

  6. int fact(int n);
复制代码
python的DistUtil用到的文件如下
$cat setup.cpp.py
  1. """
  2. setup.py
  3. """

  4. from distutils.core import setup, Extension


  5. example_module = Extension('_example',
  6.                            sources=['example_wrap.cxx', 'example.cpp'],
  7.                            )

  8. setup (name = 'example',
  9.        version = '0.1',
  10.        author      = "SWIG Docs",
  11.        description = """Simple swig example from docs""",
  12.        ext_modules = [example_module],
  13.        py_modules = ["example"],
  14.        )
复制代码

然后用python的swig工具去编译
  1. $swig -c++ -python example.i
  2. $python setup.cpp.py build_ext --inplace
  3. running build_ext
  4. building '_example' extension
  5. 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
  6. cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
  7. In file included from example_wrap.cxx:2554:
  8. example.h:5:20: warning: no newline at end of file
  9. example_wrap.cxx: In function ‘int SWIG_Python_ConvertFunctionPtr(PyObject*, void**, swig_type_info*)’:
  10. example_wrap.cxx:2051: error: invalid conversion from ‘const char*’ to ‘char*’
  11. example_wrap.cxx: In function ‘void SWIG_Python_FixMethods(PyMethodDef*, swig_const_info*, swig_type_info**, swig_type_info**)’:
  12. example_wrap.cxx:3200: error: invalid conversion from ‘const char*’ to ‘char*’
  13. error: command 'gcc' failed with exit status 1
复制代码
上面这个编译错误似乎是来自于swig工具生成的代码。我想swig工具本身应该没有什么问题吧,是不是我的代码或者我执行的命令缺少了什么参数之类的?

该如何修改?

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP