2017-07-06 46 views
0

以下C++的OpenCL代码编译细跟克++ -c no_x.cpp:OpenCL的矢量类型:无法访问联合在一起分量x,y和z与C++ 11启用

// 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 [0],xy.s [1]等,但这是丑陋的(这当然是原因的OpenCL提供了.X, .y组件)。 C++ 11导致这种情况的原因是什么?我通常可以不用C++ 11编译OpenCL吗?

+0

xy.s [0]是最便携的方式,它很丑,但工作。 – DarkZeros

回答

1

在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),您将无法访问这些成员。

+0

嗯,我不知道我的opencl版本比你看到的版本更新还是更新,但是我的版本是由__CL_HAS_ANON_STRUCT__控制的,而且我在cl_platform.h中看到,这反过来取决于STRICT_ANSI,我猜-std = C++ 11定义它。 – RubeRad

+0

@ user2387508'-std = C++ 11'对其进行了定义,但您可以在g ++中简单使用参数'-U__STRICT_ANSI__'。 – Lovy

+0

哦,甜蜜!这是诀窍,谢谢! – RubeRad