Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have compiled one utility and created the binary on my Linux System. But the size of this binary is 2.3 MB and I need to reduce the size of this binary. I tried applying STRIP on the binary but its not sufficient. So is there any option(configuration maybe), that I can apply while compiling the utility?

Thanks in advance for your reply.

share|improve this question
    
If possible add the compiler (as well as the line used to compile). Just keep in mind that most compile time switches (other than optimization) will just move the code, e.g. from the binary to a dependency. –  Mario Oct 21 '13 at 9:32
    
Thanks Mario for your reply. But by Add compile did you mean to add CC option? Because I have already added it as I am cross compiling it. –  nyk_mat Oct 21 '13 at 9:44
    
Yes, but the available options depend on the Compiler being used. For example you can optimize for speed or for size and you can determine whether you'd like to statically or dynamically link dependencies. –  Mario Oct 21 '13 at 10:27
    
@nyk_mat Is this about the Linux kernel or a program that is intended to run on Linux? In case of the Linux kernel, use make menuconfig and disable what you do not need. For other programs, check whether you can disable features (for autoconf projects, ./configure --disable-FEATURE --without-PACKAGE where FEATURE and PACKAGE have to be replaced. See ./configure --help) –  Lekensteyn Oct 21 '13 at 11:00
    
Please use comments to request clarifications. Post your edit as a comment, then rollback changes by clicking the 'edited' link under your original question and 'rollback' under previous revision. –  gronostaj Oct 24 '13 at 5:55

2 Answers 2

up vote 3 down vote accepted

You can use the -os flag if you're using GCC to compile. You can find it here in the GCC flags page -

-Os
    Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.

    -Os disables the following optimization flags:

              -falign-functions  -falign-jumps  -falign-loops 
              -falign-labels  -freorder-blocks  -freorder-blocks-and-partition 
              -fprefetch-loop-arrays

If you for some reason must have those flags, well, this isn't for you. In addition you're compromising performance for size

You could also use various methods of executable compression like upx, post compilation to make your executable size smaller. This is also a performance/size tradeoff (though fairly minimal), but can be used on top of whichever optimization you choose to use. The size reduction here can be rather dramatic.

share|improve this answer

You can use following methods to reduce executable size:

  • Make sure that executable is compiled without debugging symbols. This will happen if your makefile had flag -g present in gcc command line options. This is typically default, because it allows to easily debug such executable. You can use utility strip to remove debugging symbols from existing executable. Very often, this alone can reduce size of executable 2 or more times, sometimes as much as 10 times.
  • Compile all necessary libraries dynamically (especially C runtime library glibc on Linux). Static compilation (-s) is good to avoid dependencies, but it comes at price of significantly increasing executable size. For example, if your executable needs libxml2, add -lxml2 to gcc command line to link libxml2 dynamically. Downside of this approach is that your executable will crash if necessary library is not installed on your target system (you must make sure to run sudo apt-get install libxml2 first).
  • Enable optimization by size -Os. However, typical gains are very small, rarely more than 5%-10%.
  • Compress your executable using upx (install it with sudo apt-get install upx-ucl). upx supports packing not only for Intel platform, but for many other platforms, like arm, mips, powerpc, etc. Note that while upx may give impressive reduction in size (2x-5x), it should not be used blindly. Unlike standard executables, ones compressed with upx cannot share the same cache pages. For example, if your program is 2MB in size and you start 100 copies of it, it will only consume about 2 MB of memory - kernel will reuse cached pages. If you compress it with upx to say 1 MB and start 100 copies, it will consume 200 MB of memory. However, it will only need to read 1 MB from disk and this will reduce start time for first copy considerably because upx uncompression is very fast (much faster than reading from disk).
share|improve this answer
    
Thanks Journeyman Geek, I completely forgot about optimization flags. I will use it. But I didn't know about upx, so do I need to compile it for my system? If I compile it on my PC and compress binaries and then copy them on board, will it work? Or do I need to cross compile this for my system? –  nyk_mat Oct 24 '13 at 10:06
    
1. I am not Journeyman geek. 2. No, you do not compile upx for your system - it is already cross-platform and can compress "foreign" executables just fine –  mvp Oct 24 '13 at 16:30
    
Sorry mvp, my bad. and Thanks for reply. My system has i686 processor, so i downloaded ucl-1.03-10.fc14.i686 rpm. When I try to install it, it shows that package is already installed with warning that 57bbccba is not installed. But when I tried rpm -q ucl-1.03-10.fc14.i686, it shows that package is not installed. So do I need to install this key on my system? –  nyk_mat Oct 25 '13 at 4:26
    
It seems like you are using old Fedora box. You should be able to install it using simply sudo yum install upx (this is how I installed it on my Fedora 16 box). –  mvp Oct 25 '13 at 4:29
    
yeah, I am using fc14. –  nyk_mat Oct 25 '13 at 5:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.