title: Building a dll with a gcc using a library file (.a) converted from import library (.lib)
body: <p><strong>Step 1.</strong> I have an import lib file (<strong>main.lib</strong>) for my executable (<strong>main.exe</strong>) which exports some symbols. Those symbols exported using extern "C".</p>
  
  <p><strong>Step 2.</strong> I also have a source file (<strong>extra.cpp</strong>) which exports some extra functions. I generate a static linking library (<strong>extra.lib</strong>) from it, and I include <strong>main.lib</strong> into it as those extra functions are the users of exports from <strong>main.exe</strong>.</p>
  
  <p><strong>Step 3.</strong> A dll (<strong>bbb.dll</strong>) is built linking with this library (<strong>extra.lib</strong>) to call those extra functions from <strong>main.exe</strong>.
  (note that <strong>bbb.dll</strong> is loaded and used by <strong>main.exe</strong> in first place.)</p>
  
  <p>Now I'm trying to repeat steps 2 and 3 using Mingw(gcc) instead of MS Visual Studio(cl). Let's call it Step 2x and 3x.
  Since main.exe is a big project with many files ... the building <strong>libmain.a</strong> from sources is not a good option, I found <a href="https://stackoverflow.com/questions/13664882/using-libraries-compiled-with-gcc-in-a-visualc-project-and-vice-versa/14844991#14844991]">here</a> that <strong>main.lib</strong> can be converted to .a file using following commands: </p>
  
  <pre><code>reimp -d main.lib
  dlltool -k -d main.def -l libmain.a
  # reimp creates the .def file. 
  # dlltool uses the .def to create the .a that is linked in to the app. 
  </code></pre>
  
  <p><strong>Step 2x.</strong> </p>
  
  <pre><code>gcc -c -o extra.o -O1 -s -x c++ extra.cpp
  ar rs libextra.a extra.o
  ar rs libextra.a libmain.a
  </code></pre>
  
  <p><strong>Step 3x.</strong> </p>
  
  <pre><code>g++ -O2 -o bbb.dll -shared -x c++ bbb.cpp  -static-libgcc -static-libstdc++  -Wl,-s -Wl,--kill-at,--export-all-symbols,--enable-stdcall-fixup -Wl,--large-address-aware -lextra 
  </code></pre>
  
  <p>I'm getting linker errors at step 3x.</p>
  
  <pre><code>libmain.a(lextra.o):extra.cpp:(.text+0x38): undefined reference to `A_Function_In_main'
  </code></pre>
  
  <p><strong>Is it a name mangling difference problem?</strong></p>
  
  <p><strong>Or is it even possible to build <em>bbb.dll</em> using gcc this way?</strong></p>
  
  <p><strong>What do I do wrong?</strong></p>
  
  <p>I'm using Visual Studio 2012 on Windows 7. MinGW with gcc 4.6.1.</p>
  
  <p><strong><em>Checking if 'A_Function_In_main'  is in libmain.a:</em></strong></p>
  
  <pre><code>nm libmain.a &gt; libmain_dump.txt
  </code></pre>
  
  <p>The block of text in libmain_dump.txt regarding 'A_Function_In_main'</p>
  
  <pre><code>dshms00350.o:
  00000000 b .bss
  00000000 d .data
  00000000 i .idata$4
  00000000 i .idata$5
  00000000 i .idata$6
  00000000 i .idata$7
  00000000 t .text
  00000000 T A_Function_In_main
  00000000 I __imp_A_Function_In_main
           U _head_libmain_a
  </code></pre>