title: Difference of usage between shared and static library
body: <p>I have a very simple sample of code :</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include "libMPSSE_spi.h"
  
  int main() {
     uint32 channels = 0;
     std::cout &lt;&lt; "erreur = " &lt;&lt;  SPI_GetNumChannels(&amp;channels) &lt;&lt; std::endl;
     std::cout &lt;&lt; "Number of available SPI channels = " &lt;&lt; (int)channels &lt;&lt; std::endl;
     return 0;
  }
  </code></pre>
  
  <p>That works fine when I link the static libMPSSE, but has an issue with the shared library.</p>
  
  <p>My two commands to build the file are :</p>
  
  <pre><code>g++ test.cpp -o test.o -I../../libs/MPSSE -I../../libs/FTDI/linux -L../../libs/MPSSE/linux/x64 -lMPSSE
  
  g++ test.cpp -o test.o -I../../libs/MPSSE -I../../libs/FTDI/linux ../../libs/MPSSE/linux/x64/libMPSSE.a -ldl
  </code></pre>
  
  <p>Both compilation works, but the execution output is not the same. With static linking the library works fine, and with dynamic linking it returns "other error".</p>
  
  <p>Note : I have built both library myself with the provided makefile (object are built before the same way for both libraries):</p>
  
  <pre><code>libMPSSE:   $(OBJECTS)
      $(CC)  -o libMPSSE.so -shared $(OBJECTS) -L /MinGW/lib -ldl
      $(AR) rcs libMPSSE.a $(OBJECTS)
  </code></pre>
  
  <p>What could explain the different behavior of both libraries?</p>
  
  <p>Note : in the example they provide with their library, they use the shared library with <code>dlopen</code> but if I have the library and header at link time, I shouldnt have to do that?</p>
  
  <p>Note2 : they both use libftd2xx.so and I use this command to run the executable (dynamic and static)</p>
  
  <pre><code>LD_LIBRARY_PATH=~/WS/Qt/main/LC4/main/libs/FTDI/linux/x64:~/WS/Qt/main/LC4/main/libs/MPSSE/linux/x64 ./test.o
  LD_LIBRARY_PATH=~/WS/Qt/main/LC4/main/libs/FTDI/linux/x64 ./test.o
  </code></pre>
  
  <p>Edit : code that work with shared library (from their example)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "libMPSSE_spi.h"
  #include &lt;dlfcn.h&gt;
  
  
  int main() {
  
      typedef FT_STATUS (*pfunc_SPI_GetNumChannels)(uint32 *numChannels);
      pfunc_SPI_GetNumChannels p_SPI_GetNumChannels;
      void *h_libMPSSE;
  
      h_libMPSSE = dlopen("../../libs/MPSSE/linux/x64/libMPSSE.so",RTLD_LAZY);
      if(!h_libMPSSE)
      {
          std::cout &lt;&lt;"Failed loading libMPSSE.so. Please check if the file exists in ";
          std::cout &lt;&lt; "the shared library folder(/usr/lib or /usr/lib64)" &lt;&lt; std::endl;
          return 1;
      }
      p_SPI_GetNumChannels = (pfunc_SPI_GetNumChannels)dlsym(h_libMPSSE, "SPI_GetNumChannels");
  
     uint32 channels = 0;
     std::cout &lt;&lt; "erreur = " &lt;&lt;  p_SPI_GetNumChannels(&amp;channels) &lt;&lt; std::endl;
     std::cout &lt;&lt; "Number of available SPI channels = " &lt;&lt; (int)channels &lt;&lt; std::endl;
     return 0;
  }
  </code></pre>