Can not find cl::Error class in OpenCL library

I saw in some code that in OpenCL library, there is a class named cl::Error by which the error and type of error in OpenCL code can be catched. But when I added in my code, like this

#include <CL/cl.hpp>
#include <fstream>
#include <iostream>
#include <cassert>
#include <exception>
#define __CL_ENABLE_EXCEPTIONS

int main()
{
  std::vector<cl::Platform> platforms;
  cl::Platform::get(&platforms);

  assert(platforms.size() > 0);

  auto myPlatform = platforms.front();
  std::cout << "Using platform: " << myPlatform.getInfo<CL_PLATFORM_NAME>() << std::endl;

  std::vector<cl::Device> devices;
  myPlatform.getDevices(CL_DEVICE_TYPE_GPU, &devices);

  auto myDevice = devices.front();
  std::cout<< "Using device: "<< myDevice.getInfo<CL_DEVICE_NAME>() << std::endl;

  std::ifstream helloworldfile("helloWorldKernel.cl");
  std::string src(std::istreambuf_iterator<char>(helloworldfile), (std::istreambuf_iterator<char>()));

  cl::Program::Sources source(1,std::make_pair(src.c_str(), src.length() + 1));

  cl::Context context(myDevice);
  cl::Program program(context,source);

  cl::CommandQueue queue(context,myDevice);

  try
  {
    program.build("-cl-std=CL1.2");
  } catch(cl::Error& e)
  {
    std::cout << e.what() << std::cout;
  }

  int err;
  int szChar = 16;
  char  buf[szChar];

  cl::Buffer memBuf(context, CL_MEM_READ_WRITE, sizeof(char) * szChar);

  cl::Kernel kernel(program, "helloWorld", &err);

  if(err != CL_SUCCESS)
  {
    std::cout<<" Error in creating kernel, error: "<< err << std::endl;
    exit(1);
  } 


  kernel.setArg(0,memBuf);

  queue.enqueueTask(kernel);

  err =  queue.enqueueReadBuffer(memBuf,CL_TRUE, 0, sizeof(buf), buf);

  if(err != CL_SUCCESS)
  {
    std::cout<<" Error in reading from device, error: "<< err << std::endl;
    exit(1);
  }

  std::cout << buf;
  std::cin.get();

  return 0;

}

I got the error that the

helloWorld.cc:46:12: error: expected type-specifier
   } catch (cl::Error& e)
            ^
helloWorld.cc:46:21: error: expected unqualified-id before ‘&’ token
   } catch (cl::Error& e)
                     ^
helloWorld.cc:46:21: error: expected ‘)’ before ‘&’ token
helloWorld.cc:46:21: error: expected ‘{’ before ‘&’ token
helloWorld.cc:46:23: error: ‘e’ was not declared in this scope
   } catch (cl::Error& e)
                       ^
helloWorld.cc:46:24: error: expected ‘;’ before ‘)’ token
   } catch (cl::Error& e)

I was wondering if the class exist or at least exist in specific version of library OpenCL, if so, how should be invoke it.


You need to define __CL_ENABLE_EXCEPTIONS or if you're using the more up to date #include <CL/cl2.hpp> you need CL_HPP_ENABLE_EXCEPTIONS.

The define needs to be before you include the opencl header.