pieterk
pieterk

Reputation: 295

glibc linking causes smaller binary which is broken

I am trying to compile a program on Ubuntu 16.04 (e.g. zbackup) into a single fat binary that can run on BusyBox Linux. Using a customised CMAKE script I am linking with the following options which seems to make the binary grow to 4MB+. This is okay, it seems things are being shoved into the binary.

  -lcrypto
  -lz
  -ldl
  -static-libstdc++
  -static-libgcc

Next I try adding the -static flag so that I can include the final piece which is missing however when I add the -static option and then build again the binary is actually smaller at approx 2MB+ and it is broken. When I try to run the binary it simply says file or folder not found

I just can't seem to pin down why the -static option is causing the output binary to be broken.

Upvotes: 0

Views: 97

Answers (1)

user2766918
user2766918

Reputation: 584

From the gcc man page:

-static On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.

Thus, it's preventing linking to some shared libraries, which is what is breaking you, and likely what's making it smaller. Likely it is not linking to the crypto library, etc. Again, use objdump -t before and after, and figure out which symbols are disappearing.

Upvotes: 1

Related Questions