title: Problems with creating a CUDA shared library and libpthread
body: <p>I am currently trying to create a library with CUDA routines but I am running into trouble. I will explain my problems using a rather minimal example, my actual library will be larger.</p>
  
  <p>I have successfully written <code>test.cu</code>, a source file containing a <code>__global__</code>  CUDA function and a wrapper around it (to allocate and copy memory). I can also successfully compile this file into a shared library using the following commands:</p>
  
  <pre><code>nvcc -c test.cu -o test.o -lpthread -lrt -lcuda -lcudart -Xcompiler -fPIC
  gcc -m64 -shared -fPIC -o libtest.so test.o -lpthread -lrt -lcuda -lcudart -L/opt/cuda/lib64
  </code></pre>
  
  <p>The resulting <code>libtest.so</code> exports all my needed symbols.</p>
  
  <p>I now compile my purely C <code>main.c</code> and link it against my library:</p>
  
  <pre><code>gcc -std=c99 main.c -o main -lpthread -ltest -L.
  </code></pre>
  
  <p>This step is also successful, but upon executing <code>./main</code> all CUDA functions that are called return an error:</p>
  
  <pre><code>test.cu:17:cError(): cudaGetDeviceCount: [38] no CUDA-capable device is detected
  test.cu:17:cError(): cudaMalloc: [38] no CUDA-capable device is detected
  test.cu:17:cError(): cudaMemcpy: [38] no CUDA-capable device is detected
  test.cu:17:cError(): cudaMemcpy: [38] no CUDA-capable device is detected
  test.cu:17:cError(): cudaFree: [38] no CUDA-capable device is detected
  </code></pre>
  
  <p>(Error messages are created through a debugging function of my own)</p>
  
  <p>During my initial steps I encountered the exact same problem, as I was directly creating an executable from <code>test.cu</code>, because I forgot to link against libpthread (<code>-lpthread</code>). But, as you can see above, I have linked all source files against libpthread. According to <code>ldd</code>, both <code>libtest.so</code> and <code>main</code> depend on libpthread, as it should be.</p>
  
  <p>I am using CUDA 5 (yes, I do realize it is a beta) with gcc 4.6.3 and nvidia driver version 302.06.03 on ArchLinux.</p>
  
  <p>Some help in solving this problem would be more than appreciated!</p>