以下C++ OpenCL代码可以在g++ -c no_x.cpp中很好地编译:
// no_x.cpp
#include <CL/cl.h>
void func() {
cl_double2 xy;
xy.x = 1.0;
xy.y = 2.0;
}但在启用C++-11的情况下,相同的文件会出现错误:
$ g++ -std=c++11 -c no_x.cpp
nox.cpp: In function ‘void func()’:
nox.cpp:7:7: error: ‘union cl_double2’ has no member named ‘x’
xy.x = 1.0;
^
nox.cpp:8:7: error: ‘union cl_double2’ has no member named ‘y’
xy.y = 2.0;
^我可以用xy.s、xy.s1等解决这个问题,但这很丑陋(这肯定是OpenCL提供.x、.y组件的原因)。导致这种情况的C++11是怎么回事?我通常可以不用C++11编译OpenCL吗?
发布于 2017-07-06 15:33:08
在OpenCL标头(cl_platform.h,包含在cl.h中)中,cl_double2的定义方式如下:
typedef union
{
cl_double CL_ALIGNED(16) s[2];
#if defined( __GNUC__) && ! defined( __STRICT_ANSI__ )
__extension__ struct{ cl_double x, y; };
__extension__ struct{ cl_double s0, s1; };
__extension__ struct{ cl_double lo, hi; };
#endif
#if defined( __CL_DOUBLE2__)
__cl_double2 v2;
#endif
}cl_double2;因此,如果您的编译器没有使用GNU预处理器,或者如果定义了__STRICT_ANSI__ (g++ may define it),那么您将无法访问这些成员。
https://stackoverflow.com/questions/44942225
复制相似问题