title: makefile: cpp + cu files - error of undefined reference
body: <p>Consider three files: main.cpp, func_prototypes.h and test_kernels.cu. I'd like to build a project out from those three files. I tried to base on CUDA samples to construct a 'makefile', but I fail. The execution of make return an error of undefined reference. In what follows, there are my three files and the makefile</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #include &lt;cstdlib&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  #include &lt;new&gt;
  
  #include &lt;cuda.h&gt;
  
  using namespace std;
  
  #include "func_prototypes.h"
  
  typedef float mytype;
  
  int main(){
  
      mytype *vec;
      unsigned int N = 1024;
  
      vec = new mytype[N];
  
      for(int i = 0; i &lt; N; i++){
          vec[i] = i;
      }
  
      cout &lt;&lt; "Calling CUDA function.\n";
  
      getSquares(vec,N);
  
      cout &lt;&lt; "result:\n";
  
      for(int i = 0; i &lt; N; i++){
          cout &lt;&lt; vec[i] &lt;&lt; " ";
      }
  
      ResetCUDA();
      return EXIT_SUCCESS;
  
  }
  </code></pre>
  
  <p><strong>func_prototypes.h</strong></p>
  
  <pre><code>template &lt;class type&gt;
  void getSquares(type *v, unsigned const int N);
  void ResetCUDA();
  </code></pre>
  
  <p><strong>test_kernels.cu</strong></p>
  
  <pre><code>#include &lt;cuda.h&gt;
  #include &lt;new&gt;
  
  #define BlockSize 256
  
  template &lt;class type&gt;
  __global__
  void getSquareKernel(type *v, unsigned const int N){
      int tIdx = blockIdx.x*blockDim.x + threadIdx.x;
  
      if(tIdx &lt; N){
          v[tIdx] *= v[tIdx];
      }
  }
  
  
  
  template &lt;class type&gt;
  void getSquares(type *v, unsigned const int N){
  
      int threads = BlockSize;
      int blocks = ceil(N/threads);
  
      type *d_v;
      cudaMalloc(&amp;d_v,N*sizeof(type));
      cudaMemcpy(d_v,v,N*sizeof(type),cudaMemcpyHostToDevice);
  
      getSquareKernel&lt;&lt;&lt;blocks,threads&gt;&gt;&gt;(d_v,N);
  
      cudaMemcpy(v,d_v,N*sizeof(type),cudaMemcpyDeviceToHost);
  
      cudaFree(d_v);
  
  }
  
  void ResetCUDA(){
      cudaDeviceReset();
  }
  </code></pre>
  
  <p><strong>makefile</strong></p>
  
  <pre><code>############################# Makefile ##########################
  CUDA_PATH       ?= /usr/local/cuda-5.0
  CUDA_INC_PATH   ?= $(CUDA_PATH)/include
  CUDA_BIN_PATH   ?= $(CUDA_PATH)/bin
  
  ifeq ($(OS_SIZE),32)
      CUDA_LIB_PATH  ?= $(CUDA_PATH)/lib
  else
      CUDA_LIB_PATH  ?= $(CUDA_PATH)/lib64
  endif
  
  ifeq ($(OS_SIZE),32)
      LDFLAGS :=  -L$(CUDA_LIB_PATH) -lcudart
      CPPFLAGS    :=  -m32
  else
      LDFLAGS :=  -L$(CUDA_LIB_PATH) -lcudart
      CPPFLAGS    :=  -m64
  endif
  # Debug build flags
  ifeq ($(dbg),1)
        CPPFLAGS      += -g
        NVCCFLAGS = -g -G
  endif
  
  INCLUDES    := -I$(CUDA_INC_PATH) -I. -I.. -I../../common/inc
  
  CPP     =       icpc
  NVCC        =       $(CUDA_BIN_PATH)/nvcc
  
  SOURCE  =       main.cpp
  AUX     =       test_kernels.cu
  
  all:    test
  test_kernels.o: $(AUX)
      $(NVCC) $(NVCCFLAGS) -o test_kernels.o -c $(AUX) $(NVCCFLAGS) $(INCLUDES)
  main.o: $(SOURCE)
      $(CPP) $(CPPFLAGS) -o main.o -c $(SOURCE) $(CPPFLAGS) $(INCLUDES)
  test:   test_kernels.o  main.o
      $(CPP) -o test test_kernels.o main.o $(LDFLAGS)
  run: test
      ./test
  clean:
      rm -rf test *.o
  </code></pre>
  
  <p>The returned error is <code>main.o:main.cpp:function main: error: undefined reference to 'void getSquares&lt;float&gt;(float*, unsigned int)'
  make: *** [test] Error 1</code></p>
  
  <p>Does anyone know where is my mistake?</p>
  
  <p><strong>EDIT:</strong> For the record, my OS is Ubuntu 12.04 x86_64, kernel 3.2.0-39</p>