1

我正在尝试在 RHEL 6.8 上运行一个简单的 helloworld.go。

% cat helloworld.go 
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

GC 编译器工作正常

%  go build -compiler gccgo helloworld.go 
go build command-line-arguments: : fork/exec : no such file or directory

相反,出于某种原因,我需要使用 gccgo 进行编译,但永远无法编译。更不用说最终静态编译它了。

% go build -compiler gccgo -gccgoflags '-static' helloworld.go
go build command-line-arguments: : fork/exec : no such file or directory
% go build -compiler gccgo -gccgoflags '-static-libgo' helloworld.go
go build command-line-arguments: : fork/exec : no such file or directory


% go version 
go version go1.7 linux/amd64
% gcc --version
gcc (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6)

对问题的见解将不胜感激?

4

1 回答 1

-1

你应该先安装 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

于 2017-11-14T16:47:13.487 回答