title: Static and Dynamic/Shared Linking with MinGW
body: <p>I want to start with a simple linking usage to explain my problem. Lets assume that there is a library <code>z</code> which could be compiled to shared library libz.dll(D:/libs/z/shared/libz.dll) or to static library libz.a (D:/libs/z/static/libz.a).</p>
  
  <p>Let I want to link against it, then I do this: </p>
  
  <pre><code>gcc -o main.exe main.o -LD:/libs/z/static -lz
  </code></pre>
  
  <p>According to <a href="http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html">this documentation</a>, gcc would search for libz.a, which is </p>
  
  <blockquote>
    <p>archive files whose members are object files</p>
  </blockquote>
  
  <p>I also can do the following:</p>
  
  <pre><code>gcc -o main.exe main.o -LD:/libs/z/shared -lz
  </code></pre>
  
  <p>It is not mentioned in the documentation above that <code>-l</code> flag will search for <code>lib&lt;name&gt;.so</code>.</p>
  
  <p>What will happen if I libz.a and libz.dll will be in the same directory? How the library will be linked with a program? Why I need the flags <code>-Wl,-Bstatic</code> and <code>-Wl,-Bdynamic</code> if <code>-l</code> searches both for shared and static libraries?</p>
  
  <p>Why some developers provide .a files with .dll files for the same modules, if I compile a shared library distribution?</p>
  
  <p>For example, Qt provides .dll files in bin directory with .a files in lib directory. Is it the same library, but built like shared and static, respectively? Or .a files are some kind of dummy libraries which provide linking with shared libraries, where there are real library implementations?</p>
  
  <p>Another example is OpenGL library on Windows. Why every compiler must provide the static OpenGL lib like libopengl32.a in MingW?</p>
  
  <p>What are files with .dll.a and .la extensions used for?</p>
  
  <p>P.S. There are a lot of questions here, but I think each one depends on the previous one and there is no need to split them into several questions.</p>