Answers to: Size of the Library/Executable is high in LINUXhttp://linuxexchange.org/questions/2556/size-of-the-libraryexecutable-is-high-in-linux<p>HI All,</p> <p>We have a 32bit Gui application created using C++. We ported the application from Solaris to Linux. Issue we are facing is the size of the library and executable is very large in LINUX compared to Solaris.</p> <p>Red Hat Enterprise Linux 5.4 is the Linux version we using.</p> <p>Please find a sample dynamic library created. We would like to know the following behavior of LINUX is normal or not.</p> <p>Consider we created two files <a href="http://test1.cc">test1.cc</a> and <a href="http://test2.cc">test2.cc</a>. Both having a single line of code.</p> <p>a-2720@N530 /data1/users/a-2720/samp :ls -lrt <a href="http://test1.cc">test1.cc</a> <a href="http://test2.cc">test2.cc</a> -rw-rw-r-- 1 a-2720 mcs 21 May 18 06:16 <a href="http://test1.cc">test1.cc</a> -rw-rw-r-- 1 a-2720 mcs 21 May 18 06:16 <a href="http://test2.cc">test2.cc</a></p> <p>a-2720@N530 /data1/users/a-2720/samp :cat <a href="http://test1.cc">test1.cc</a></p> <pre><code>#include&lt;iostream.h&gt; </code></pre> <p>a-2720@N530 /data1/users/a-2720/samp :cat <a href="http://test2.cc">test2.cc</a></p> <pre><code>#include&lt;iostream.h&gt; </code></pre> <p>Thus the files have only only one line inside them</p> <p>I created a Shared library using these files.</p> <h2>SOLARIS</h2> <p>CC -c -library=iostream -g -mt <a href="http://test1.cc">test1.cc</a></p> <p>CC -c -library=iostream -g -mt <a href="http://test2.cc">test2.cc</a></p> <p>CC -G -h <a href="http://libtestsolaris.so">libtestsolaris.so</a> test1.o test2.o -o <a href="http://libtestsolaris.so">libtestsolaris.so</a> -library=iostream </p> <p>a-2720@N530 /data1/users/a-2720/samp :ls -lrt test1.o test2.o <a href="http://libtestsolaris.so">libtestsolaris.so</a></p> <p>-rw-rw-r-- 1 a-2720 mcs 20944 May 18 06:16 test1.o</p> <p>-rw-rw-r-- 1 a-2720 mcs 20944 May 18 06:16 test2.o</p> <p>-rwxrwxr-x 1 a-2720 mcs 7384 May 18 06:16 <a href="http://libtestsolaris.so">libtestsolaris.so</a></p> <h2>LINUX</h2> <p>CC -m32 -c -library=iostream -g -mt <a href="http://test1.cc">test1.cc</a></p> <p>CC -m32 -c -library=iostream -g -mt <a href="http://test2.cc">test2.cc</a></p> <p>CC -m32 -G -h <a href="http://libtestlinux.so">libtestlinux.so</a> test1.o test2.o -o <a href="http://libtestlinux.so">libtestlinux.so</a> -library=iostream </p> <p>/data1/users/adarsh/samp :ls -lrt test1.o test2.o <a href="http://libtestlinux.so">libtestlinux.so</a></p> <p>-rw-r--r-- 1 adarsh ifo 20220 May 18 06:44 test1.o</p> <p>-rw-r--r-- 1 adarsh ifo 20220 May 18 06:44 test2.o</p> <p>-rwxr-xr-x 1 adarsh ifo 41680 May 18 06:44 <a href="http://libtestlinux.so">libtestlinux.so</a></p> <p>Here we can see that the Linux shared library are in much bigger size than solaris once. Please note that the source file for </p> <p>these libraries are same. Our application uses thousand of files having these header files and hence a notable difference in size occurs.</p> <p>We would like to know this size difference is a normal behavior of LINUX.</p> <h2>System Details</h2> <p>/data1/users/adarsh/samp :cat /etc/*-release Red Hat Enterprise Linux Server release 5.4 (Tikanga)</p> <p>/data1/users/adarsh/samp :uname -a Linux N280 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux</p> <p>Thanks in advance.</p> <p>Sanush Chacko</p>enMon, 23 May 2011 17:14:44 -0400Answer by who_knowshttp://linuxexchange.org/questions/2556/size-of-the-libraryexecutable-is-high-in-linux/2559<p>It seems to me that you're comping without optmization. -O2 should be the minimum in your case. -Os is another choice.</p>who_knowsMon, 23 May 2011 17:14:44 -0400http://linuxexchange.org/questions/2556/size-of-the-libraryexecutable-is-high-in-linux/2559Answer by Jazzhttp://linuxexchange.org/questions/2556/size-of-the-libraryexecutable-is-high-in-linux/2558<p>Have you tried using another compiler like <strong>g++</strong>? <strong>g++</strong> has a lot of <a href="http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html">optimization options</a>, most notably <strong>-Os</strong>. </p> <p>I haven't created shared libraries yet, so the following might not be correct or the best way to do it, but it might help you:</p> <pre><code>g++ -Os -fPIC -shared <a href="http://test1.cc">test1.cc</a> -o test1.o g++ -Os -fPIC -shared <a href="http://test2.cc">test2.cc</a> -o test2.o g++ -shared -Os test1.o test2.o -o <a href="http://libtest.so">libtest.so</a> ls -lA -rwxr-xr-x 1 jazz users 5616 19. Mai 15:08 <a href="http://libtest.so">libtest.so</a> -rw-r--r-- 1 jazz users 19 19. Mai 14:59 <a href="http://test1.cc">test1.cc</a> -rwxr-xr-x 1 jazz users 6500 19. Mai 15:09 test1.o -rw-r--r-- 1 jazz users 19 19. Mai 14:59 <a href="http://test2.cc">test2.cc</a> -rwxr-xr-x 1 jazz users 6500 19. Mai 15:03 test2.o </code></pre> <p><br> Furthermore, I changed </p> <pre><code>#include&lt;iostream.h&gt; </code></pre> <p>to </p> <pre><code>#include&lt;iostream&gt; </code></pre> <p>in the <em>test-files</em>. The difference is explained <a href="http://www.decompile.com/cpp/faq/diff_btw_h_and_no_h.htm">here</a>. <br><br><br> You can also use <strong>strip</strong>! Strip can reduce the size of the object file by a significant amount, but debugging is hindered afterwards. <br><br><br> All this was done on the following <em>x86_64 system</em>:</p> <pre><code>Linux substitutedhostname 2.6.38-ARCH #1 SMP PREEMPT Fri May 13 09:24:47 CEST 2011 x86_64 Intel(R) Core(TM) i7 CPU M 620 @ 2.67GHz GenuineIntel GNU/Linux </code></pre>JazzThu, 19 May 2011 09:25:25 -0400http://linuxexchange.org/questions/2556/size-of-the-libraryexecutable-is-high-in-linux/2558