securecurve
securecurve

Reputation: 5817

Can I control static or dynamic library linking while compiling? Does each create diff. binary sizes?

In one of the Go seminars, the lecturer said that when he compiled the Go application, statically linked, the size of the resulting binary was about 600 MB big, but when he compiled the same app with dynamic linking, the resulting binary turned into 10 MB.

I'm not sure what he is talking about, can dynamic vs. static linking during compilation make that difference in space of binary, and do I have control over that?

Upvotes: 4

Views: 2481

Answers (1)

wlredeye
wlredeye

Reputation: 1014

By default Go uses static linking so everything (your code and packages sources) compiles in one big binary.

Since Go 1.5 released you can compile Go shared library using -buildmode=shared option for go build or go install. Then you can compile your app binary with -linkshared flag. Details can be found here.

Ofcourse, if you link packages dynamically your binary size will be less than with static linking, but total application size will not be reduced because you just "put your code in other place". So dynamic linking makes sense only if you need to share the same packages between different apps.

Upvotes: 5

Related Questions