title: CUDA: How to reuse kernels in multiple files (for unit testing)
body: <p>How can I go about reusing the same kernel without getting fatal linker errors due to defining the symbol multiple times?</p>
  
  <p>In Visual Studio I get:</p>
  
  <pre><code>"fatal error LNK1169: one or more multiply defined symbols found"
  </code></pre>
  
  <p>I have three files: <code>Interface.h</code>, <code>Kernel.cu</code>, and <code>Wrapper.cu</code>. My current structure is as follows (ala the <a href="http://developer.nvidia.com/cuda-cc-sdk-code-samples" rel="nofollow noreferrer">"C++ integration" SDK example</a>):</p>
  
  <pre><code>//Interface.h 
  extern "C" void myCfunction( ... );
  
  //Kernel.cu
  __global__ void my_kernel( ... );
  
  //Wrapper.cu
  #include "Interface.h"
  #include "Kernel.cu"
  
  extern "C" void myCfunction( ... );
  
  void myCfunction( ... ) {
      // Call CUDA kernel in Kernel.cu
      my_kernel&lt;&lt;&lt; ... &gt;&gt;&gt;( ... );
  }
  </code></pre>
  
  <p>This all works fine.</p>
  
  <p>But if I add another C function in another file which also includes <code>Kernel.cu</code> and uses those kernels, I get the above error. e.g.</p>
  
  <pre><code>//NewWrapper.cu
  #include "Kernel.cu"
  
  extern "C" void myNewCfunction( ... );
  
  void myNewCfunction( ... ) {
      // Call CUDA kernel in Kernel.cu
      my_kernel&lt;&lt;&lt; ... &gt;&gt;&gt;( ... );
      // Now I get linker errors...
  }
  </code></pre>
  
  <p>So how can I reuse the kernels in <code>Kernel.cu</code> among many C functions in different files.</p>
  
  <p>The purpose of this by the way is unit testing, and integrating my kernels with CPP unit, if there is no way to reuse kernels (there must be!) then other suggestions for unit testing kernels within my existing CPP unit framework would be appreciate.</p>