title: How to use C++ shared library in Go code with the help of SWIG?
body: <p>I have a <strong>3rd party C++ .so library</strong> that I want to use in my <strong>Go code</strong>. I can make it work with plain C/C++, but fail to connect to Go.</p>
  
  <p>The library has a big header file and the function I need is declared in it like so (renamed for a simpler look):</p>
  
  <pre><code>typedef unsigned long (*INTERFACE_EXAMPLE_FUNC)(int example_var);
  #ifdef PC_STATIC_LIBS
  extern "C" unsigned long Example_func(int example_var);
  #endif //PC_STATIC_LIBS
  </code></pre>
  
  <p><em>(I assume it's easier to link a static lib, so I put #define PC_STATIC_LIBS on top of the header)</em></p>
  
  <p>I've spent many hours trying to link this library without SWIG, but <strong>cgo</strong> wouldn't parse the C++ header, giving me tons of error messages.
  So, I'm trying to use SWIG now. Currently my swig interface file <strong>example.i</strong> looks as follows:</p>
  
  <pre><code>%module example
   %{
   #include "libheader.h"
   extern unsigned long Example_func(int example_var);
   %}
   extern unsigned long Example_func(int example_var);
  </code></pre>
  
  <p><strong>Steps I take:</strong></p>
  
  <ol>
  <li>run swig command: <code>swig -go -cgo -c++ -intgosize 64 -use-shlib -soname examplelib.so example.i</code></li>
  <li><code>go install</code></li>
  </ol>
  
  <p><strong>Results I get:</strong></p>
  
  <pre><code>/usr/bin/ld: $WORK/b001/_x003.o: in function `_wrap_Example_func_examplelib_8c0447b5964daffe':
  ./examplelib_wrap.cxx:302: undefined reference to `Example_func'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>All the files that are being used are in the same directory. I also have a copy of the library placed in my /usr/local/lib and <code>ldconfig -p</code> shows it.</p>
  
  <p>What am I doing wrong here?</p>