title: Separating out .cu and .cpp(using c++11 library)
body: <p>I am trying to convert a c++ program I have which uses random library which is a C++11 feature. After having read through a couple of similar posts here, I tried by separating out the code into three files. At the outset I would like to say that I am not very conversant at C/C++ and mostly use R at work.</p>
  
  <p>The main file looks as follows.</p>
  
  <pre><code>#ifndef _KERNEL_SUPPORT_
  #define _KERNEL_SUPPORT_
  #include &lt;complex&gt;
  #include &lt;random&gt;
  #include &lt;iostream&gt;
  #include "my_code_header.h"
  using namespace std;
  std::default_random_engine generator;
  std::normal_distribution&lt;double&gt; distribution(0.0,1.0);
  const int rand_mat_length = 24561;
  double rand_mat[rand_mat_length];// = {0};
  void create_std_norm(){
    for(int i = 0 ; i &lt; rand_mat_length ; i++)
      ::rand_mat[i] = distribution(generator);
  }
  .
  .
  .
  int main(void)
  {
    ...
    ...
    call_global();
    return 0;
  }
  #endif
  </code></pre>
  
  <p>The header file looks as follows.</p>
  
  <pre><code>#ifndef mykernel_h
  #define mykernel_h
  void call_global();
  void two_d_example(double *a, double *b, double *my_result, size_t length, size_t width);
  #endif
  </code></pre>
  
  <p>And the .cu file looks like the following.</p>
  
  <pre><code>#ifndef _MY_KERNEL_
  #define _MY_KERNEL_
  #include &lt;iostream&gt;
  #include "my_code_header.h"
  #define TILE_WIDTH 8
  using namespace std;
  __global__ void two_d_example(double *a, double *b, double *my_result, size_t length, size_t width)
  {
    unsigned int row = blockIdx.y*blockDim.y + threadIdx.y;
    unsigned int col = blockIdx.x*blockDim.x + threadIdx.x;
    if ((row&gt;length) || (col&gt;width)) {
      return;
    }
    ... 
  }
  void call_global()
  {
    const size_t imageLength = 528;
    const size_t imageWidth = 528;
    const dim3 threadsPerBlock(TILE_WIDTH,TILE_WIDTH);
    const dim3 numBlocks(((imageLength) / threadsPerBlock.x), ((imageWidth) / threadsPerBlock.y));
    double *d_a, *d_b, *mys ;
  
    ...
    cudaMalloc((void**)&amp;d_a, sizeof(double) * imageLength);
    cudaMalloc((void**)&amp;d_b, sizeof(double) * imageWidth);
    cudaMalloc((void**)&amp;mys, sizeof(double) * imageLength * imageWidth);
  
    two_d_example&lt;&lt;&lt;numBlocks,threadsPerBlock&gt;&gt;&gt;(d_a, d_b, mys, imageLength, imageWidth);
    ...  
    cudaFree(d_a);
    cudaFree(d_b);
  
  
  }
  
  #endif
  </code></pre>
  
  <p>Please note that the <code>__global__</code> has been removed from .h since I was getting the following error owing to it being compiled by g++. </p>
  
  <pre><code>In file included from my_code_main.cpp:12:0:
  my_code_header.h:5:1: error: ‘__global__’ does not name a type
  </code></pre>
  
  <p>When I compile the .cu file with nvcc it is all fine and generates a my_code_kernel.o. But since I am using C++11 in my .cpp I am trying to compile it with g++ and I am getting the following error. </p>
  
  <pre><code>/tmp/ccR2rXzf.o: In function `main':
  my_code_main.cpp:(.text+0x1c4): undefined reference to `call_global()'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I understand that this might not have to do anything with CUDA as such and may just be the wrong use of including the header at both places. Also what is the right way to compile and most importantly link the my_code_kernel.o and my_code_main.o(hopefully)? Sorry if this question is too trivial! </p>