title: Creating DLL from CUDA using nvcc
body: <p>I want to create a .dll from a CUDA code (<code>kernel.cu</code>) in order to use this library from an external C program. After some attempts I just left a simple C function in .cu file. Code follows:</p>
  
  <p>kernel.cu</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "kernel.h"
  
  void hello(const char *s) {
          printf("Hello %s\n", s);
  }/*
  </code></pre>
  
  <p>kernel.h</p>
  
  <pre><code>#ifndef KERNEL_H
  #define KERNEL_H
  
  #include "cuda_runtime.h"
  #include "device_launch_parameters.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  void __declspec(dllexport) hello(const char *s);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif  // KERNEL_H
  </code></pre>
  
  <p>I tried to first generate a <code>kernel.o</code> object with <code>nvcc</code> and after i used <code>g++</code> for creating DLL as following:</p>
  
  <pre><code>nvcc -c kernel.cu -o kernel.o
  g++ -shared -o kernel.dll kernel.o -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\lib\x64" -lcudart
  </code></pre>
  
  <p>It works fine and generates <code>kernel.dll</code>. To test DLL file I wrote this simple program <code>main.c</code>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  void __declspec ( dllimport ) hello(const char *s);
  
  #ifdef __cplusplus
  }
  #endif
  
  int main(void) {
          hello("World");
          return 0;
  }
  </code></pre>
  
  <p>compiled with:</p>
  
  <pre><code>g++ -o app.exe main.c -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include" -L. -lkernel
  </code></pre>
  
  <p>Result is a memory access error when execution starts.</p>
  
  <p>Nevertheless, if I rename .cu file in .c (as it is just C code), using the same commands, it does work. nvcc's output changes, as far as I know because it uses default C compiler instead of CUDA one.</p>
  
  <p>What do you think, is it a problem related with nvcc? Or am I making any mistake?</p>
  
  <p><strong>EDIT</strong>: I forgot some info which may be important. Warnings appear in the first call to g++ (when dll is created) and they are different depending on whether .cu .c or .cpp.</p>
  
  <p>.cu</p>
  
  <pre><code>Warning: .drectve `/FAILIFMISMATCH:"_MSC_VER=1600" /FAILIFMISMATCH:"_ITERATOR_DEBUG_LEVEL=0" 
  /DEFAULTLIB:"libcpmt" /DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" /EXPORT:hello ' unrecognized
  </code></pre>
  
  <p>and it doesn't work.</p>
  
  <p>.cpp and .c</p>
  
  <pre><code>Warning: .drectve `/DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" /EXPORT:hello ' unrecognized
  </code></pre>
  
  <p>and it works.</p>