title: Massive executable size with -L/usr/local/lib
body: <p><em>I encountered today a bizarre problem relating to binary sizes produced by (cygwin) g++.</em></p>
  
  <p>When compiling a C++ program that uses standard library functions and passing <code>-L/usr/local/lib</code> as an option, the binary size is absolutely massive (12MB massive).</p>
  
  <p><code>iostream</code> seems to have the largest effect from what I've tested.</p>
  
  <p>I've identified the 30MB file <code>libstdc++.a</code> in <code>/usr/local/lib</code> as the source of the problem through trial and error. That is, I copied the contents of <code>/usr/local/lib</code> into a separate dir, added that one to the link path instead of <code>/usr/local/lib</code>, and deleted files until the binary size dropped to normal.</p>
  
  <p><strong>Trials:</strong></p>
  
  <pre><code>KEY:
  (&lt;group&gt;)
  `&lt;command&gt;` -&gt; &lt;size of resulting binary in bytes&gt;
  </code></pre>
  
  <p>control 1 (literally nothing) <strong>[no effect]</strong>:</p>
  
  <pre><code>int main() {}
  </code></pre>
  
  <pre><code>(control)
  `g++ -Wall test.cpp` -&gt; 159,574
  `g++ -Wall -Os test.cpp` -&gt; 159,610
  `g++ -Wall -Os -s test.cpp` -&gt; 8,704
  (test)
  `g++ -Wall test.cpp -L/usr/local/lib` -&gt; 159,574
  `g++ -Wall -Os test.cpp -L/usr/local/lib` -&gt; 159,610
  `g++ -Wall -Os -s test.cpp -L/usr/local/lib` -&gt; 8,704
  </code></pre>
  
  <p>control 2 (using some other library dynamically - eg stb_image...) <strong>[no effect]</strong>:</p>
  
  <pre><code>#include "stb_image.h"
  int main() {
      int width, height, channels;
      unsigned char *data = stbi_load("image.jpg", &amp;width, &amp;height, &amp;channels, 0);
  }
  </code></pre>
  
  <pre><code>(control)
  `g++ -Wall test.cpp stb_image.so` -&gt; 159,944
  `g++ -Wall -Os test.cpp stb_image.so` -&gt; 159,980
  `g++ -Wall -Os -s test.cpp stb_image.so` -&gt; 8,704
  (test)
  `g++ -Wall test.cpp -L/usr/local/lib stb_image.so` -&gt; 159,944
  `g++ -Wall -Os test.cpp -L/usr/local/lib stb_image.so` -&gt; 159,980
  `g++ -Wall -Os -s test.cpp -L/usr/local/lib stb_image.so` -&gt; 8,704
  </code></pre>
  
  <p>vector (templates) <strong>[slight effect]</strong>:</p>
  
  <pre><code>#include &lt;vector&gt;
  int main() {
      std::vector&lt;int&gt; v;
      v.push_back(2);
  }
  </code></pre>
  
  <pre><code>(control)
  `g++ -Wall test.cpp` -&gt; 190,228
  `g++ -Wall -Os test.cpp` -&gt; 160,429
  `g++ -Wall -Os -s test.cpp` -&gt; 8,704
  (test)
  `g++ -Wall test.cpp -L/usr/local/lib` -&gt; 1,985,106
  `g++ -Wall -Os test.cpp -L/usr/local/lib` -&gt; 906,760
  `g++ -Wall -Os -s test.cpp -L/usr/local/lib` -&gt; 72,192
  </code></pre>
  
  <p>iostream <strong>[major effect]</strong>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  int main() {
      std::cout &lt;&lt; "iostream" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <pre><code>(control)
  `g++ -Wall test.cpp` -&gt; 161,829
  `g++ -Wall -Os test.cpp` -&gt; 161,393
  `g++ -Wall -Os -s test.cpp` -&gt; 8,704
  (test)
  `g++ -Wall test.cpp -L/usr/local/lib` -&gt; 11,899,614
  `g++ -Wall -Os test.cpp -L/usr/local/lib` -&gt; 11,899,344
  `g++ -Wall -Os -s test.cpp -L/usr/local/lib` -&gt; 828,416
  </code></pre>
  
  <p>Cannot be replicated in C w/ gcc, which makes sense I guess given the problem file is named <em>libstdc++</em>.</p>
  
  <p>If you need more trials, let me know.</p>
  
  <p>My question is: <strong><em>Why?</em></strong> Why does adding a directory with <code>libstdc++.a</code> to the search path increase the binary size so? To my knowledge, nothing should be linked from the linker search path unless stated explicitly with <code>-l&lt;library&gt;</code>. Does it have something to do with <code>/usr/local/lib</code> being searched first, and <code>-lstdc++</code> being added implicitly, therefore perhaps linking the wrong library...?</p>