Skip to content

cmd/build: cgo cross build need to explictly set CGO_ENABLED=1 #72988

Duplicate of#70868
@linsite

Description

@linsite

Go version

go version go1.24.1 linux/arm64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE='on'
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/ulin/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/ulin/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1300834282=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/mnt/data/lean/g/excersize/go.mod'
GOMODCACHE='/home/ulin/gosrc/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ulin/gosrc'
GOPRIVATE=''
GOPROXY='https://goproxy.cn'
GOROOT='/usr/local/go'
GOSUMDB='off'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/ulin/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.24.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

cross compiling a tiny cgo program for riscv64.

package main

/*
#include <stdio.h>
#include <stdlib.h>

// A C function
void myprint() {
 printf("env: PATH=%s\n", getenv("PATH"));
}
*/
import "C"

func main() {
C.myprint()
}

CC=/usr/bin/riscv64-linux-gnu-gcc GOARCH=riscv64 go build m.go

What did you see happen?

go: no Go source files

What did you expect to see?

As the default CGO_ENABLED=1 showed in go env, the build should succeed.
But need to explicitly set CGO_ENABLED=1 in the build command.

FYI, go build complained "no Go source files" doesn't convey any useful information for one to figure out what's going wrong.

Activity

linsite

linsite commented on Mar 21, 2025

@linsite
ContributorAuthor

I wonder if this can be covered by #48540?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @seankhliao@linsite

        Issue actions

          cmd/build: cgo cross build need to explictly set `CGO_ENABLED=1` · Issue #72988 · golang/go