title: Linking C code with Cuda code
body: <p>So I'm writing a very basic CUDA code (vector addition) to teach myself the basics of CUDA programming. I've got it working when I write one .cu file, but now I am trying to make it work with a .c and .cu file linked together. My main.c file is as follows:</p>
  
  <pre><code>#include "Test.h"
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char *argv[]) {
          int n = 1000;
          size_t size = n * sizeof(float);
          int i;
  
          float *h_a = malloc(size), *h_b = malloc(size), *h_c = malloc(size);
  
          for(i = 0; i &lt; n; i++) {
                  h_a[i] = h_b[i] = i;
          }
  
          addVec(h_a, h_b, h_c, n);
  
          exit(0);
  }
  </code></pre>
  
  <p>Here, Test.h simply says:</p>
  
  <pre><code>void addVec(float *, float *, float *, int);
  </code></pre>
  
  <p>My vecAdd.cu file says:</p>
  
  <pre><code>#include "Test.h"
  
  __global__ void vecAdd(float *a, float *b, float *c, int n) {
          int i = blockDim.x * blockIdx.x + threadIdx.x;
  
          if(i &lt; n)
                  c[i] = a[i] + b[i];
  }
  
  void addVec(float *a, float *b, float *c, int n) {
          float *d_a, *d_b, *d_c;
          size_t size = n * sizeof(float);
  
          cudaMalloc(&amp;d_a, size);
          cudaMalloc(&amp;d_b, size);
          cudaMalloc(&amp;d_c, size);
  
          ...
  }
  </code></pre>
  
  <p>I then run the commands:</p>
  
  <pre><code>gcc -c -Wall -O3 main.c -o ../obj/main.o
  nvcc -c -O3 vecAdd.cu -o ../obj/vecAdd.o
  gcc -L/usr/local/cuda/lib64 -lcudart ../obj/main.o ../obj/vecAdd.o -o ../bin/nvTest
  </code></pre>
  
  <p>The first two work fine. The last one, when I try to link the two object files, tells me that I have an undefined reference to addVec, though it is defined in vecAdd.cu... what am I doing wrong?</p>