Golang构建失败,因为需要gcc。

huangapple go评论110阅读模式
英文:

golang build fails because of gcc being required

问题

我正在构建以下非常简单的go程序:

package main

import (
	"context"
	"flag"
	"fmt"
	"log"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
)

const namespace = "default"

func main() {
	home := homedir.HomeDir()
	defaultKubeConfigPath := fmt.Sprintf("%s/%s", home, ".kube/config")
	kubeconfig := flag.String("kubeconfig", defaultKubeConfigPath, "location to kubeconfig file")
	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		log.Fatal(err)
	}
	pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err)
	}
	for i := range pods.Items {
		fmt.Printf("%s\n", pods.Items[i].Name)
	}

	deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err)
	}
	for i := range deployments.Items {
		fmt.Printf("%s\n", deployments.Items[i].Name)
	}
}

使用以下Dockerfile

FROM golang:1.19.2-alpine3.15 as builder

COPY src /src

WORKDIR /src

RUN go build -race -ldflags "-s -w" -a -o client-go

FROM scratch

COPY --from=builder client-go /client-go

ENTRYPOINT [ "./client-go" ]

构建失败,错误如下:

#8 21.92 # runtime/cgo
#8 21.92 cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH

在哪里需要gcc,因为我没有使用任何cgo扩展?

有没有一种先验的方法来确定何时需要cgogcc

英文:

I am building the following ridiculously simple go program

package main

import (
	"context"
	"flag"
	"fmt"
	"log"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
)

const namespace = "default"

func main() {
	home := homedir.HomeDir()
	defaultKubeConfigPath := fmt.Sprintf("%s/%s", home, ".kube/config")
	kubeconfig := flag.String("kubeconfig", defaultKubeConfigPath, "location to kubeconfig file")
	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		log.Fatal(err)
	}
	pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err)
	}
	for i := range pods.Items {
		fmt.Printf("%s\n", pods.Items[i].Name)
	}

	deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err)
	}
	for i := range deployments.Items {
		fmt.Printf("%s\n", deployments.Items[i].Name)
	}
}

using the following Dockerfile

FROM golang:1.19.2-alpine3.15 as builder
COPY src /src
WORKDIR /src
RUN go build -race -ldflags "-s -w" -a -o client-go
FROM scratch
COPY --from=builder client-go /client-go
ENTRYPOINT [ "./client-go" ]

The build fails as folows:

#8 21.92 # runtime/cgo
#8 21.92 cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH

Where exactly is gcc needed since I am not using any cgo extensions?

Is there an a priori way of identifying when gcc for cgo is needed?

答案1

得分: 4

你需要设置CGO_ENABLED=0并且不启用竞争检测器。


cgo工具在预期可用的系统上默认启用本地构建。在交叉编译时,默认禁用cgo。您可以通过在运行go工具时设置CGO_ENABLED环境变量来控制此行为:将其设置为1以启用cgo的使用,将其设置为0以禁用cgo。如果启用了cgo,go工具将设置构建约束"cgo"。特殊导入"C"意味着"cgo"构建约束,就好像文件中还写着"// +build cgo"一样。因此,如果禁用了cgo,导入"C"的文件将不会被go工具构建。

https://pkg.go.dev/cmd/cgo

另请参阅https://stackoverflow.com/questions/64531437/why-is-cgo-enabled-1-default

使用CGO_ENABLED=0和设置了race标志,您将会得到一个错误:

#8 0.857 go: -race requires cgo; enable cgo by setting CGO_ENABLED=1

因此,为了在没有gcc的情况下进行编译,您应该使用以下命令:

RUN CGO_ENABLED=0 go build -ldflags "-s -w" -a -o client-go
英文:

You need to set CGO_ENABLED=0 and not enable the race detector.


> The cgo tool is enabled by default for native builds on systems where it is expected to work. It is disabled by default when cross-compiling. You can control this by setting the CGO_ENABLED environment variable when running the go tool: set it to 1 to enable the use of cgo, and to 0 to disable it. The go tool will set the build constraint "cgo" if cgo is enabled. The special import "C" implies the "cgo" build constraint, as though the file also said "// +build cgo". Therefore, if cgo is disabled, files that import "C" will not be built by the go tool.

https://pkg.go.dev/cmd/cgo

See also https://stackoverflow.com/questions/64531437/why-is-cgo-enabled-1-default

With CGO_ENABLED=0 and the race flag set, you'll get an error:

#8 0.857 go: -race requires cgo; enable cgo by setting CGO_ENABLED=1

So to get this to compile without gcc you'll want to use:

RUN CGO_ENABLED=0 go build -ldflags "-s -w" -a -o client-go

huangapple
  • 本文由 发表于 2022年10月29日 18:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/74244477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定