Juicy
Juicy

Reputation: 12530

Compiling multiple programs into a single binary to save space

Applications like busybox and dropbear contain several programs in a single binary.

Are there tools out there that facilitate this process? Could I compile several C programs from different authors into a single binary relatively easy?

I ask because I'm compiling a few things for a small Linux embedded device and I'm trying to save as much space as possible.

Upvotes: 0

Views: 393

Answers (2)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215617

I'm not aware of any general tool to do this, and in any case, it's only a space-saving measure when there's a large overlap between the code for the programs to be combined. For busybox the savings are not so much overlap between the code for the individual applets, but rather sharing a single copy of a static-linked libc. For dynamic linking, much of that benefit disappears.

Upvotes: 2

Potatoswatter
Potatoswatter

Reputation: 137950

No, this isn't a form of data compression. You generally can't take two similar applications and leverage their functional similarities to save disk space.

What those applications do is check the value of the first command-line argument, argv[0], which tells how the binary was invoked. Then the program does the right thing.

If you have access to the source code of a few applications and you know that they are reduplicating the exact same code (i.e., they are incorporating functions from the same static library), you have a couple choices:

  • Move the duplicated code (static libraries) into a shared library.
  • Try to implement your proposed strategy by writing your own main function, and compiling them all simultaneously.

It's hard to know how much work this would be without seeing the build systems for the programs in question, but it will probably not be "fairly easy."

If there's not any common static library, but they're just doing similar things, then you're talking about refactoring them. That could easily turn into a complete rewrite of all the programs. Could be worth it, if the result will be more than the sum of its parts, and it's perhaps easier than starting from scratch, but categorically we can say "lots of work."

Upvotes: 3

Related Questions