dsl2014 2015-07-11 19:22
浏览 10

是否可以使用依赖项编译golang代码? [关闭]

Is it possible to compile golang code with dependencies? For an executable file was small.

  • 写回答

1条回答 默认 最新

  • dqwh0109 2015-07-11 20:50
    关注

    As mentioned in "Linking golang statically" (Vincent Batts):

    As long as the source being compiled is native go, the go compiler will statically link the executable.
    Though when you need to use cgo, then the compiler has to use its external linker.

    A pure go program will show like this:

    $> go build ./code-pure.go
    $> ldd ./code-pure
            not a dynamic executable
    $> file ./code-pure
    ./code-pure: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), statically linked, not stripped
    

    This isn't so with cgo, unless you add an extra flag like:

    go build --ldflags '-extldflags "-static"' ./code-cgo.go
    # or, with gccgo
    go build -compiler gccgo --gccgoflags "-static" ./code-cgo.go
    

    Reminder, even with Go 1.5 (which uses go for compilation, and not gc), gccgo will still be there.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)