Python C API 代码的 gcc 错误 - “ISO C++禁止在函数指针和对象指针之间进行转换”
以下代码片段不执行任何操作,但说明了问题。它是从一些使用 Numpy C API 的 Boost Python 代码中提取的。这是通过从 Debian不稳定的 gcc 4.7快照向后移植到squeeze进行测试的。
#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
问题在于 -pedantic
和 PyArray_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]
The following code fragment does nothing, but illustrates the problem. It was extracted from some Boost Python code, which uses the Numpy C API. This was tested with the backport of a gcc 4.7 snapshot from Debian unstable to squeeze.
#include <boost/python/object.hpp>
#include <numpy/arrayobject.h>
int main(void)
{
PyObject* obj=0;
npy_int64 val;
PyArray_ScalarAsCtype(obj, &val);
return 0;
}
I'm compiling like this.
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
The problem is the -pedantic
and the PyArray_ScalarAsCtype
line of code. Without -pedantic
the following compiles without error
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
Note: I added the =0
to suppress an uninitialized warning. Like I said, the code doesn't do anything.
I'd like to either suppress or remove the warning and keep the -pedantic
flag. From what I've read, there is no error as such here, but this falls within some disputed section of the standard. I don't really understand the issue, or how it pertains to this line of code. The new gcc diagnostics allow one to selectively suppress warnings in a section of code, but they require you to know what specific flag is triggering the warning, and I don't know. Without the -Werror
flag I get
warn.cc:8:3: warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object [enabled by default]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在标准 C++ 中,您无法在
int*
和int(*)()
之间进行转换。很可能,这就是您的实施过程中发生的情况。大多数平台允许,但并非全部。当然,任何仅在合法平台上执行的库都没有什么非法之处。
In Standard C++, you cannot convert between, say, an
int*
andint(*)()
. Likely, this is what's happening under the hood in your implementation. Most platforms allow it, but not all.Of course, there is nothing illegal about any library only executing on platforms where it is legal.