dongxia2068 2018-01-24 21:02
浏览 97

如何静态编译go编译器?

How do I compile the go compiler statically with CGO support?

Compiling the sources with CGO_ENABLED=0 and GO_LDFLAGS='-extldflags "-static"' results in a statically linked binary:

../bin/go: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

However, using the linker flags and CGO_ENABLED=1 does not produced a statically linked binary.

This is what I have done so far:

FROM debian:stretch-slim AS builder                                                                                                   

ENV CGO_ENABLED=1                                                                                                                     
ENV GO_LDFLAGS='-extldflags "-static"'                                                                                                
ENV GOROOT_BOOTSTRAP=/usr/local/go                                                                                                    
RUN apt update && apt upgrade -y && apt install -y curl git build-essential make cmake                                                
RUN curl -fLOSs "https://dl.google.com/go/go1.9.3.linux-amd64.tar.gz" \                                                               
&& tar -C /usr/local -xzf "go1.9.3.linux-amd64.tar.gz"                                                                                
RUN git clone https://go.googlesource.com/go \                                                                                        
&& cd go \                                                                                                                            
&& git checkout go1.9.3 \                                                                                                             
&& cd src \                                                                                                                           
&& ./make.bash                                                                                                                        
RUN apt install -y file && /usr/bin/file /go/bin/go                                                                                   


FROM scratch                                                                                                                         
ENV GOPATH=/code                                                                                                                     
COPY --from=builder /go/bin /usr/local/go/bin                                                                                        
CMD ["/usr/local/go/bin/go"] 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用