title: Golang calling CUDA library
body: <p>I am trying to call a CUDA function from my Go code.
  I have the following three files.</p>
  
  <p>test.h:</p>
  
  <pre><code>int test_add(void);
  </code></pre>
  
  <p>test.cu:</p>
  
  <pre><code>__global__ void add(int *a, int *b, int *c){
         *c = *a + *b;
  }
  
  int test_add(void) {
         int a, b, c; // host copies of a, b, c
         int *d_a, *d_b, *d_c; // device copies of a, b, c
         int size = sizeof(int);
         // Allocate space for device copies of a, b, c
         cudaMalloc((void **)&amp;d_a, size);
         cudaMalloc((void **)&amp;d_b, size);
         cudaMalloc((void **)&amp;d_c, size);
        // Setup input values
        a = 2;
        b = 7;
  
        // Copy inputs to device
        cudaMemcpy(d_a, &amp;a, size, cudaMemcpyHostToDevice);
        cudaMemcpy(d_b, &amp;b, size, cudaMemcpyHostToDevice);
        // Launch add() kernel on GPU
       add&lt;&lt;&lt;1,1&gt;&gt;&gt;(d_a, d_b, d_c);
       // Copy result back to host
       cudaMemcpy(&amp;c, d_c, size, cudaMemcpyDeviceToHost);
       // Cleanup
       cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
      return 0;
  }
  </code></pre>
  
  <p>test.go:</p>
  
  <pre><code>package main
  
  import "fmt"
  
  //#cgo CFLAGS: -I.
  //#cgo LDFLAGS: -L. -ltest
  //#cgo LDFLAGS: -lcudart
  //#include &lt;test.h&gt;
  import "C"
  
  
  func main() {
       fmt.Printf("Invoking cuda library...\n")
       fmt.Println("Done ", C.test_add())
  }
  </code></pre>
  
  <p>I am compiling CUDA code with:</p>
  
  <pre><code>nvcc -m64 -arch=sm_20 -o libtest.so --shared -Xcompiler -fPIC test.cu
  </code></pre>
  
  <p>All three files - test.h, test.cu and test.go are in the same directory.
  The error I am getting when I try to build with go is "undefined reference to `test_add'".</p>
  
  <p>I have very little experience with C/C++ and am a total novice in CUDA.</p>
  
  <p>I've been trying to solve my problem for two days now and would be
  very grateful for any input.</p>
  
  <p>Thanks.</p>