How to reduce compiled file size?

前端 未结 12 745
野性不改
野性不改 2020-11-28 20:59

Lets compare c and go: Hello_world.c :

#include
int main(){
    printf(\"Hello world!\");
}

Hello_world.go:

         


        
12条回答
  •  野性不改
    2020-11-28 21:19

    By default gcc links dynamically and go - statically.

    But if you link you C code statically, you might get a binary with a bigger size.

    In my case:

    • go x64 (1.10.3) - generated binary with size 1214208 byte
    • gcc x64 (6.2.0) - generated binary with size 1421312 byte

    both binaries are statically linked and without debug_info.

    go build -ldflags="-s -w" -o test-go test.go
    gcc -static -s -o test-c test.c
    

提交回复
热议问题