你应该先安装 gccgo 编译器。在我的 Fedora 26 桌面上,运行dnf install gcc-go
安装。
$ which gccgo
/usr/bin/gccgo
go build --compiler gccgo -x
WORK=/tmp/go-build848309745
mkdir -p $WORK/_/home/sam/sandbox/go/src/hello/_obj/
mkdir -p $WORK/_/home/sam/sandbox/go/src/hello/_obj/exe/
cd /home/sam/sandbox/go/src/hello
/usr/bin/gccgo -I $WORK -c -g -m64 -fgo-relative-import-path=_/home/sam/sandbox/go/src/hello -o $WORK/_/home/sam/sandbox/go/src/hello/_obj/_go_.o ./helloworld.go
ar rc $WORK/_/home/sam/sandbox/go/src/libhello.a $WORK/_/home/sam/sandbox/go/src/hello/_obj/_go_.o
cd .
/usr/bin/gccgo -o $WORK/_/home/sam/sandbox/go/src/hello/_obj/exe/a.out $WORK/_/home/sam/sandbox/go/src/hello/_obj/_go_.o -Wl,-( -m64 -Wl,--whole-archive -Wl,--no-whole-archive -Wl,-)
cp $WORK/_/home/sam/sandbox/go/src/hello/_obj/exe/a.out hello
如果您需要链接静态,也需要安装包 glibc-static 和 libgo-static。安装后,使用 gccgo 编译并通过静态链接
$ go build -compiler gccgo -x --gccgoflags "-static"
WORK=/tmp/go-build815863722
mkdir -p $WORK/_/home/sam/sandbox/go/src/hello/_obj/
mkdir -p $WORK/_/home/sam/sandbox/go/src/hello/_obj/exe/
cd /home/sam/sandbox/go/src/hello
/usr/bin/gccgo -I $WORK -c -g -m64 -fgo-relative-import-path=_/home/sam/sandbox/go/src/hello -o $WORK/_/home/sam/sandbox/go/src/hello/_obj/_go_.o -static ./helloworld.go
ar rc $WORK/_/home/sam/sandbox/go/src/libhello.a $WORK/_/home/sam/sandbox/go/src/hello/_obj/_go_.o
cd .
/usr/bin/gccgo -o $WORK/_/home/sam/sandbox/go/src/hello/_obj/exe/a.out $WORK/_/home/sam/sandbox/go/src/hello/_obj/_go_.o -Wl,-( -m64 -Wl,--whole-archive -Wl,--no-whole-archive -Wl,-) -static
cp $WORK/_/home/sam/sandbox/go/src/hello/_obj/exe/a.out hello