使用gccgo构建Go - fork / exec没有这样的文件或目录

时间:2017-03-17 21:54:22

标签: gcc go compilation gccgo

我正在尝试在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)

可以理解对问题的看法?

1 个答案:

答案 0 :(得分:0)

您应首先安装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