title: Compiling a basic openCL program error: unresolved includes inside cl.hpp
body: <p>I am new to coding in openCL and am following the tutorial found here: <a href="https://www.marcusbannerman.co.uk/index.php/research/teaching-resources/77-supercomputing-on-graphics-cards-an-introduction-to-opencl-and-the-c-bindings.html" rel="nofollow">https://www.marcusbannerman.co.uk/index.php/research/teaching-resources/77-supercomputing-on-graphics-cards-an-introduction-to-opencl-and-the-c-bindings.html</a> and am running into issues. </p>
  
  <p>This is my first code I am attempting in openCL and so far having troubles. I have succesfully placed targeted the libraries in my IDE (netbeans) as they come up as an option when writing code, but I get an error with the cl.hpp file saying that there are unresolved includes inside .</p>
  
  <p>Errors:</p>
  
  <pre><code>main.cpp:7:18: cl.hpp: No such file or directory
  main.cpp: In function `int main()':
  main.cpp:24: error: `cl' has not been declared
  main.cpp:24: error: `Context' undeclared (first use this function)
  main.cpp:24: error: (Each undeclared identifier is reported only once for each function it appears in.)
  main.cpp:24: error: expected `;' before "context"
  main.cpp:27: error: `cl' has not been declared
  main.cpp:27: error: `Sources' undeclared (first use this function)
  main.cpp:27: error: expected `;' before "source"
  main.cpp:28: error: `source' undeclared (first use this function)
  main.cpp:31: error: `cl' has not been declared
  main.cpp:31: error: `Program' undeclared (first use this function)
  main.cpp:31: error: expected `;' before "program"
  main.cpp:34: error: `cl' has not been declared
  main.cpp:34: error: `Device' undeclared (first use this function)
  main.cpp:34: error: template argument 1 is invalid
  main.cpp:34: error: template argument 2 is invalid
  main.cpp:35: error: invalid type in declaration before '=' token
  main.cpp:35: error: `context' undeclared (first use this function)
  main.cpp:35: error: `CL_CONTEXT_DEVICES' undeclared (first use this function)
  main.cpp:35: error: expected primary-expression before ')' token
  main.cpp:39: error: `program' undeclared (first use this function)
  main.cpp:41: error: `cl' has not been declared
  main.cpp:41: error: `Error' has not been declared
  main.cpp:41: error: invalid catch parameter
  main.cpp:44: error: `err' undeclared (first use this function)
  main.cpp:46: error: `CL_PROGRAM_BUILD_LOG' undeclared (first use this function)
  main.cpp:46: error: invalid types `int[int]' for array subscript
  main.cpp:52: error: `cl' has not been declared
  main.cpp:52: error: `Kernel' undeclared (first use this function)
  main.cpp:52: error: expected `;' before "kernel"
  main.cpp:55: error: `cl' has not been declared
  main.cpp:55: error: `CommandQueue' undeclared (first use this function)
  main.cpp:55: error: expected `;' before "cmdQ"
  main.cpp:60: error: `cl_float' undeclared (first use this function)
  main.cpp:60: error: template argument 1 is invalid
  main.cpp:60: error: template argument 2 is invalid
  main.cpp:60: error: invalid type in declaration before ';' token
  /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h: At global scope:
  /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h: In instantiation of `std::back_insert_iterator&lt;int&gt;':
  main.cpp:61:   instantiated from here
  /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h:363: error: `int' is not a class, struct, or union type
  main.cpp: In function `int main()':
  main.cpp:64: error: `cl' has not been declared
  main.cpp:64: error: `Buffer' undeclared (first use this function)
  main.cpp:64: error: expected `;' before "inputBuffer"
  main.cpp:67: error: `cl' has not been declared
  main.cpp:67: error: expected `;' before "outputBuffer"
  main.cpp:73: error: `kernel' undeclared (first use this function)
  main.cpp:73: error: `inputBuffer' undeclared (first use this function)
  main.cpp:74: error: `outputBuffer' undeclared (first use this function)
  main.cpp:77: error: `cl' has not been declared
  main.cpp:77: error: `KernelFunctor' undeclared (first use this function)
  main.cpp:77: error: expected `;' before "func"
  main.cpp:80: error: `func' undeclared (first use this function)
  main.cpp:86: error: template argument 1 is invalid
  main.cpp:86: error: template argument 2 is invalid
  main.cpp:86: error: invalid type in declaration before '(' token
  main.cpp:86: error: request for member `size' in `input', which is of non-class type `int'
  main.cpp:89: error: `cmdQ' undeclared (first use this function)
  main.cpp:89: error: request for member `size' in `input', which is of non-class type `int'
  main.cpp:89: error: invalid types `int[int]' for array subscript
  main.cpp:92: error: request for member `size' in `input', which is of non-class type `int'
  main.cpp:93: error: invalid types `int[size_t]' for array subscript
  main.cpp:93: error: invalid types `int[size_t]' for array subscript
  main.cpp:93: error: invalid types `int[size_t]' for array subscript
  main.cpp:94: error: invalid types `int[size_t]' for array subscript
  main.cpp:94: error: invalid types `int[size_t]' for array subscript
  main.cpp:99: error: `cl' has not been declared
  main.cpp:99: error: `Error' has not been declared
  main.cpp:99: error: invalid catch parameter
  main.cpp:105:2: warning: no newline at end of file
  /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: In function `_OutputIterator std::generate_n(_OutputIterator, _Size, _Generator) [with _OutputIterator = std::back_insert_iterator&lt;int&gt;, _Size = unsigned int, _Generator = int (*)()]':
  main.cpp:61:   instantiated from here
  /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:1006: error: no match for 'operator=' in '(&amp;__first)-&gt;std::back_insert_iterator&lt;_Container&gt;::operator* [with _Container = int]() = __gen()'
  /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h:338: note: candidates are: std::back_insert_iterator&lt;int&gt;&amp; std::back_insert_iterator&lt;int&gt;::operator=(const std::back_insert_iterator&lt;int&gt;&amp;)
  nbproject/Makefile-Debug.mk:66: recipe for target `build/Debug/Cygwin-Windows/main.o' failed
  make[2]: *** [build/Debug/Cygwin-Windows/main.o] Error 1
  make[2]: Leaving directory `/cygdrive/c/Users/Thomas/SkyDrive/Documents/Research Documents/openCL/Testing/helloworld'
  nbproject/Makefile-Debug.mk:59: recipe for target `.build-conf' failed
  make[1]: *** [.build-conf] Error 2
  make[1]: Leaving directory `/cygdrive/c/Users/Thomas/SkyDrive/Documents/Research Documents/openCL/Testing/helloworld'
  nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
  make: *** [.build-impl] Error 2
  
  
  BUILD FAILED (exit value 2, total time: 2s)
  </code></pre>
  
  <p>Let me know if you need more info! Any help on setting it up on windows properly would be appreciated!</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  //The OpenCL C++ bindings, with exceptions
  #define __CL_ENABLE_EXCEPTIONS
  #include &lt;cl.hpp&gt;
  
  const size_t problemSize = 1024;
  
  //The compute kernel we will run
  const char * kernelSrc = 
    "__kernel void squareArray(__global float* input, "
    "                          __global float* output)"
    "{"
    "  output[get_global_id(0)] = input[get_global_id(0)] * input[get_global_id(0)];"
    "}";
  
  int main()
  {
    try {
      /***************     OpenCL Initialisation      ***************/
      //Open a context to run the openCL kernel in
      cl::Context context(CL_DEVICE_TYPE_GPU);
  
      //Gather all the kernel sources for the OpenCL program
      cl::Program::Sources source;
      source.push_back(std::make_pair(kernelSrc, strlen(kernelSrc)));
  
      //Make an OpenCL program
      cl::Program program(context, source);
  
      //Get all the available devices in the context
      std::vector&lt;cl::Device&gt; devices 
        = context.getInfo&lt;CL_CONTEXT_DEVICES&gt;();
  
      //Build the kernel sources for all devices in the context
      try {
        program.build(devices);
      }
      catch (cl::Error&amp; err)
        {
      //Get the build log for the first device
      std::cerr &lt;&lt; "Building failed, " &lt;&lt; err.what() &lt;&lt; "(" &lt;&lt; err.err() &lt;&lt; ")" 
            &lt;&lt; "\nRetrieving build log\n" 
            &lt;&lt; program.getBuildInfo&lt;CL_PROGRAM_BUILD_LOG&gt;(devices[0])
            &lt;&lt; "\n";
      return -1;
        }
  
      //Get the squareArray kernel to use in calculations
      cl::Kernel kernel(program, "squareArray");
  
      //Make a queue to put jobs on the first compute device
      cl::CommandQueue cmdQ(context, devices[0]);
  
      /***************   Preparing the data buffers   ***************/
  
      //Create a vector of random input values
      std::vector&lt;cl_float&gt; input;
      std::generate_n(std::back_inserter(input), problemSize, rand);
  
      //Start copying this data to the graphics card
      cl::Buffer inputBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(cl_float) * input.size(), &amp;input[0]);
  
      //Make a buffer to hold the output of the kernel
      cl::Buffer outputBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_float) * input.size());
  
  
      /***************  Running on the graphics card  ***************/
  
      //Set the two arguments of the squareArray kernel
      kernel.setArg(0, inputBuffer);
      kernel.setArg(1, outputBuffer);
  
      //Get a Functor which will run the kernel on every input item in blocks of 64 threads
      cl::KernelFunctor func = kernel.bind(cmdQ, cl::NDRange(input.size()), cl::NDRange(64));
  
      //Run the kernel and wait for it to finish
      func().wait();
  
  
      /***************  Checking the outputted data   ***************/
  
      //Make a buffer to hold the outputted data
      std::vector&lt;cl_float&gt; output(input.size());
  
      //Request a blocking copy of the data from the graphics card
      cmdQ.enqueueReadBuffer(outputBuffer, true, 0, sizeof(cl_float) * input.size(), &amp;output[0]);
  
      //Now check the answer
      for (size_t i(0); i &lt; input.size(); ++i)
        if (input[i]*input[i] != output[i]) 
      std::cout &lt;&lt; "Found a mistake " &lt;&lt; input[i] &lt;&lt; "^2 != " &lt;&lt; output[i] &lt;&lt; "\n";
  
      std::cout &lt;&lt; "Finished!\n";
      return 0;
    }
    catch (cl::Error&amp; err)
      {
        std::cerr &lt;&lt; "An OpenCL error occured, " &lt;&lt; err.what()
          &lt;&lt; "\nError num of " &lt;&lt; err.err() &lt;&lt; "\n";
        return -1;
      }
  }
  </code></pre>