title: go run throes error " undefined reference to `dlopen'"
body: <p>I am trying to integrate Golang script with existing C shared library.
  The C shared library in turn loads other shared libraries at run time.</p>
  <p>While I am trying to run
  go run gotest.go</p>
  <p>it throws error</p>
  <pre><code>./libtest.so: undefined reference to `dlopen'
  ./libtest.so: undefined reference to `dlclose'
  ./libtest.so: undefined reference to `dlerror'
  ./libtest.so: undefined reference to `dlsym'
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>I have created separate C executable to load this libtest.so at run time and that is working properly.</p>
  <p>Any idea what should be done to fix the issue?</p>
  <p>I have tried following commands</p>
  <blockquote>
  <p>sudo go build -compiler=gccgo -gccgoflags=&quot;-ltest&quot; gotest.go</p>
  <p>also tried the command</p>
  <p>sudo go build -compiler=gccgo -gccgoflags=&quot;-ltest -ldl&quot; gotest.go</p>
  <p>Also tried the command</p>
  <p>go run gotest.go</p>
  <p>===============================================</p>
  </blockquote>
  <p>Here is the code I am using</p>
  <pre><code>testapi.h
  typedef Students{
          char name[40];
          int  id;
  } Student;
  
  Student* getStudent();
  
  test.c
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &quot;mylib.h&quot;
  #include &quot;testapi.h&quot;
  
  int rc = 0;
  Student *s = 0;
  Student* getStudent(){
     
     rc = loadmylib(); //This function loads another shared library
     if (!rc)
     {
         rc = initlib(); //This calls dlsym to get the necessary function from opened shared library
         if (!rc)
         {
             s= (student *)malloc(sizeof(Student));
      
             //use the function pointer received from dlsym to populate the Student struct 's'
         }
      }
      return s;
  }
  </code></pre>
  <p>I have following entries in my gotest.go file</p>
  <pre><code>package main
  
  /*
  #cgo CFLAGS: -I.
  #cgo LDFLAGS: -L. -ltest -ldl
  #include &quot;testapi.h&quot;
  */
  import &quot;C&quot;
  import (
      &quot;fmt&quot;
  )
  
  type Student struct{
       name string
       id int
  }
  
  func main() {
      st := C.getStudent()
      fmt.Println(&quot;Name: &quot;, string(st.name))
  }
  </code></pre>
  <p>I have copied testapi.h, gotest.go, mylib.h, libmylib.so, libtest.so in the same directory.
  Then run the command</p>
  <blockquote>
  <p>go run gotest.go</p>
  </blockquote>
  <p>It has thrown the error &quot; error: undefined reference to 'getStudent' &quot;</p>
  <p>What am I doing wrong here?
  Am I supposed to run command</p>
  <blockquote>
  <p>sudo go build -compiler=gccgo -gccgoflags=&quot;-ltest&quot; gotest.go</p>
  </blockquote>