title: getting ld to ignore undefined references when making a DLL
body: <p>I'm trying to get <code>gcc</code>'s <code>ld</code> to ignore unresolved references when putting together a shared library from a bunch of object files compiled with <code>-fpic</code> flag.</p>
  
  <p>I tried a bunch of options so far such as (replaced the long file names of the many object files with a few small ones for brevity):</p>
  
  <pre><code>ld --allow-shlib-undefined --unresolved-symbols=ignore-all -shared 1.o 2.o -o lib0.so
  ld -G 1.o 2.o -o lib0.so
  </code></pre>
  
  <p>(I've red <a href="https://www.cs.duke.edu/~ola/courses/programming/libraries.html" rel="noreferrer">somewhere</a> that <code>-G</code> will allow unresolved references, but had no luck with it.)</p>
  
  <p>Running it though <code>gcc</code> (with <code>-Wl,--unresolved-symbols=ignore-all</code>) results in fewer unresolved references as it links by default with</p>
  
  <pre><code>-lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -liconv -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt
  </code></pre>
  
  <p>but it still complains about not having <code>-lopengl32</code> and <code>-lgdi32</code>.</p>
  
  <p>Compiling the lib without <code>-fpic</code>, stashing all the object files in a <code>.a</code> with <code>ar</code> and static linking to it while having the exe program link to <code>-lopengl32</code> and <code>-lgdi32</code> results in working completely fine.</p>
  
  <p>The actual error messages (replaced the long file and function names for brevity):</p>
  
  <pre><code>[file].o: In function `[function]': [file].cpp:19: undefined reference to `memcpy'
  [file].o: In function `[function]': [file].cpp:26: undefined reference to `memcpy'
  
  [file].o:[file].cpp:(.xdata+0x4c): undefined reference to `__gxx_personality_seh0'
  [file].o:[file].cpp:(.xdata+0x74): undefined reference to `__gxx_personality_seh0'
  [file].o:[file].cpp:(.rdata$[file]]+0x20):undefined reference to `__cxa_pure_virtual'
  [file].o:[file].cpp:(.rdata$[file]]+0x28): undefined reference to `__cxa_pure_virtual'
  </code></pre>
  
  <p>How can I get <code>ld</code> to ignore undefined references (at least from <code>libopengl32</code> and <code>libgdi32</code> if not <code>libstdc++</code> etc too) and let the program thats going to use it link to them?</p>