繁体   English   中英

Python C API代码出现gcc错误-“ ISO C ++禁止在指针指向函数和指针指向对象之间进行强制转换”

[英]gcc error with Python C API code - “ISO C++ forbids casting between pointer-to-function and pointer-to-object”

以下代码片段不执行任何操作,但说明了问题。 它是从一些使用Numpy C API的Boost Python代码中提取的。 已使用Debian不稳定的gcc 4.7快照的反向端口进行了测试。

#include <boost/python/object.hpp>
#include <numpy/arrayobject.h>

int main(void)
{
  PyObject* obj=0;
  npy_int64 val;
  PyArray_ScalarAsCtype(obj, &val);
  return 0;
}

我正在这样编译。

g++-4.7 -o warn.o -c -isystem /usr/include/python2.6 -fdiagnostics-show-option -ftemplate-depth-100 -fno-strict-aliasing -ansi -pedantic -Wextra -Wall -Werror -Wno-unused-function -Wc++0x-compat -g -O3 -std=c++11 -I/usr/include/python2.6 warn.cc
warn.cc: In function 'int main()':
warn.cc:8:3: error: ISO C++ forbids casting between pointer-to-function and pointer-to-object [-Werror]
cc1plus: all warnings being treated as errors

问题是-pedanticPyArray_ScalarAsCtype代码行。 如果没有-pedantic则以下编译不会出错

g++-4.7 -o warn.o -c -isystem /usr/include/python2.6 -fdiagnostics-show-option -ftemplate-depth-100 -fno-strict-aliasing -ansi -Wextra -Wall -Werror -Wno-unused-function -Wc++0x-compat -g -O3 -std=c++11 -I/usr/include/python2.6 warn.cc
g++-4.7 -o warn warn.o -L/usr/lib/python2.6/config -lpython2.6 -lboost_python

注意:我添加了=0以禁止未初始化的警告。 就像我说的那样,代码没有任何作用。

我想抑制或删除警告并保留-pedantic标志。 根据我的阅读,这里没有这样的错误,但这属于标准中有争议的部分。 我不太了解这个问题,也不了​​解它与这行代码的关系。 新的gcc诊断程序允许人们有选择地抑制一段代码中的警告,但它们需要您知道触发该警告的特定标志,而我不知道。 没有-Werror标志,我得到

warn.cc:8:3: warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object [enabled by default]

在Standard C ++中,您不能在int*int(*)() 这很可能是实现中的幕后故事。 大多数平台都允许,但并非全部。

当然,任何仅在合法平台上执行的库都没有违法行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM