title: Using a C function from a C++ program - extern keyword problem
body: <p>I am having a problem with a C++ program. I want to use a function which is defined in a C file in a C++ file.  Here is my code for the C++ file: </p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  extern void squre_array();
  using namespace std;
  
  int main() {
      squre_array();
  }
  </code></pre>
  
  <p>Now here is the code for the C file in which I defined <code>squre_array()</code>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;cuda.h&gt;
  
  __global__ void square_array(float *a, int N)
  {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx&lt;N) 
      a[idx] = a[idx] * a[idx];
  }
  void squre_array()
  {
    float *a_h, *a_d; 
    const int N = 10;  
    size_t size = N * sizeof(float);
    a_h = (float *)malloc(size);        
    cudaMalloc((void **) &amp;a_d, size);   
    for (int i=0; i&lt;N; i++) a_h[i] = (float)i;
    cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
    int block_size = 4;
    int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
    square_array &lt;&lt;&lt; n_blocks, block_size &gt;&gt;&gt; (a_d, N);
  
    cudaMemcpy(a_h, a_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
    // Print results
    for (int i=0; i&lt;N; i++) printf("%d %f\n", i, a_h[i]);
  
    free(a_h); 
    cudaFree(a_d);
  }
  </code></pre>
  
  <p>Now can any one tell me how to link this function to my C++ program?
  Every time I compile the program, I got the error message:</p>
  
  <pre><code>cpp:: undefined reference to `squre_array()'
  </code></pre>
  
  <p>Can anyone tell me what I was doing wrong?
  How can I link the <code>squre_array()</code> function into my C++ program?</p>