EDITED for future reference.

  1. You set your 'GOROOT' wrong. Set it to C:\Go\

  2. Include C:\Go\bin to your 'Path'

Answer from Xeph on Stack Overflow
🌐
Reddit
reddit.com › r/golang › cross compiling with cgo_enabled=1, "unrecognized command line option -m64"
r/golang on Reddit: Cross compiling with CGO_ENABLED=1, "unrecognized command line option -m64"
December 3, 2021 -

I'm trying to cross compile a project that depends on a C library. I get this error during build:

aarch64-unknown-linux-musl-gcc: error: unrecognized command line option '-m64'

In go env I see GOGCCFLAGS has -m64. I know I can't change GOGCCFLAGS directly, so is there anyway I can tell go to not add that compiler option? I can imagine that there can be a version of such a cross compiler that would accept this compiler option, but I don't know how to get it.

Top answer
1 of 3
1
I don't think I can help you with your specific problem, but I've often found that concrete examples of how other people have done it are useful. So here's how I recently managed to cross compile for armv7 and arm64: Here's the different builds in goreleaser's format: https://github.com/binwiederhier/ntfy/blob/main/.goreleaser.yml#L5-L37 That translates to this for arm64 (the spacing and quotes may be missing, cause this is what goreleaser prints for me): go build -tags=sqlite_omit_load_extension,osusergo,netgo -ldflags=-linkmode=external -extldflags=-static -s -w -X main.version=v1.5.0-next -X main.commit=7ba268887b7d782563ede18a4346bcf99ce306b0 -X main.date=2021-12-03T14:46:42Z -o /home/pheckel/Code/ntfy/dist/ntfy_arm64_linux_arm64/ntfy . With these envs: CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOOS=linux GOARCH=arm64 GOARM= GOMIPS= GOMIPS64= Hope it helps.
2 of 3
1
How are you invoking go build? I don't have a aarch64-unknown-linux-musl toolchain handy, but I do have a aarch64-linux-gnu-gcc cross toolchain and this works for me: package main // int c = 1; import "C" import "fmt" func main() { fmt.Println(C.c) } Build: $ CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go build main.go $ qemu-aarch64-static -L /usr/aarch64-linux-gnu ./main 1 My GOGCCFLAGS includes -m64: $ CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go env GOGCCFLAGS ... -m64 ... And aarch64-linux-gnu-gcc doesn't like it: aarch64-linux-gnu-gcc: error: unrecognized command-line option ‘-m64’ But somehow it's not getting in the way.
🌐
GitHub
github.com › golang › go › issues › 18981
cmd/go: `go install` with CGO_ENABLED=0 tries to install standard packages, which can fail · Issue #18981 · golang/go
February 7, 2017 - -rw-r--r-- 1 jaten jaten 19172 Feb 7 16:57 _all.o -rw-r--r-- 1 jaten jaten 1169185 Feb 7 16:57 _go_.o -rw-r--r-- 1 jaten jaten 21364 Feb 7 16:57 __.PKGDEF [jaten@biggie tmp]$ go env GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/jaten/go" GORACE="" GOROOT="/usr/local/go" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" GOGCCFLAGS="-fPIC -m64 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build885638224=/tmp/go-build -gno-record-gcc-switches" CXX="g++" CGO_ENABLED="0" PKG_CONFIG="pkg-config" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" [jaten@biggie tmp]$ go version go version go1.8rc3 linux/amd64 [jaten@biggie tmp]$ unset CGO_ENABLED [jaten@biggie tmp]$ cd ..
Author   golang
Discussions

mingw w64 - 'CGO_CFLAGS' is not recognized as an internal or external command, - Stack Overflow
I am getting an error while compiling make file using mingw64. I have already installed go in my system C:\Users\geogi\Downloads\magicard-recover-master\magicard>mingw32-make makefile windows m... More on stackoverflow.com
🌐 stackoverflow.com
January 19, 2023
windows - Golang: Getting started - "go" is not recognized as an internal or external command" - Stack Overflow
Trying to get started with Go but I cant manage to set up the work environnement properly. More on stackoverflow.com
🌐 stackoverflow.com
go - unable to set CGO_ENABLED=1 - Stack Overflow
So I am trying to run my code from visual studio code using the command go run -race . This is giving me the error: go: -race requires cgo; enable cgo by setting CGO_ENABLED=1 I tried setting it by More on stackoverflow.com
🌐 stackoverflow.com
cmd/go: cannot compile Go binary with external linking without cgo with v1.22.0
Go version go version go1.22.0 linux/amd64 Output of go env in your module/workspace: GO111MODULE='' GOARCH='amd64' GOBIN='' GOCACHE='~/.cache/go-build' GOENV='~... More on github.com
🌐 github.com
14
February 22, 2024
🌐
Stack Overflow
stackoverflow.com › questions › 75168323 › cgo-cflags-is-not-recognized-as-an-internal-or-external-command
mingw w64 - 'CGO_CFLAGS' is not recognized as an internal or external command, - Stack Overflow
January 19, 2023 - CGO_CFLAGS="-I/include" CGO_LDFLAGS="-L/lib/Windows -Wl,--enable-stdcall-fixup,-rpath=/lib/Windows -lHCNetSDK" GOOS=windows CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ go build -ldflags "-s -w" -o build/Windows/magicard-recover.exe src/magicard-recover.go 'CGO_CFLAGS' is not recognized as an internal or external command, operable program or batch file.
🌐
Go Packages
pkg.go.dev › cmd › cgo
cgo command - cmd/cgo - Go Packages
April 7, 2026 - 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 as well as when the CC environment variable is unset and the default C compiler (typically gcc or clang) cannot be found on the system PATH.
🌐
Go Packages
pkg.go.dev › cmd › go › internal › help
help package - cmd/go/internal/help - Go Packages
For example, the following build constraint constrains a file to build when the "linux" and "386" constraints are satisfied, or when "darwin" is satisfied and "cgo" is not: //go:build (linux && 386) || (darwin && !cgo) It is an error for a file to have more than one //go:build line.
Find elsewhere
🌐
GitHub
github.com › golang › go › issues › 65887
cmd/go: cannot compile Go binary with external linking without cgo with v1.22.0 · Issue #65887 · golang/go
February 22, 2024 - GO111MODULE='' GOARCH='amd64' GOBIN='' GOCACHE='~/.cache/go-build' GOENV='~/.config/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='amd64' GOHOSTOS='linux' GOINSECURE='' GOMODCACHE='~/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='linux' GOPATH='~/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/snap/go/10506' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/snap/go/10506/pkg/tool/linux_amd64' GOVCS='' GOVERSION='go1.22.0' GCCGO='gccgo' GOAMD64='v1' AR='ar' CC='gcc' CXX='g++' CGO_ENABLED='1' GOMOD='~/bootboot/mykernel/go/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build4216832776=/tmp/go-build -gno-record-gcc-switches'
Author   golang
🌐
YouTube
youtube.com › watch
'go' is not recognized as an internal or external command,operable program or batch file SOLVED - YouTube
In this tutorial we will see how to fix the error 'go' is not recognized as an internal or external command, operable program or batch file in golang
Published   May 14, 2024
🌐
GitHub
github.com › golang › go › issues › 60869
CGO_ENABLED=0 not working on latest golang image · Issue #60869 · golang/go
June 19, 2023 - I googled and set an CGO_ENABLED=0 environment property when building, expecting this to solve my problem.
Author   golang
🌐
Practical Go Lessons
practical-go-lessons.com › chap-4-setup-your-dev-environment
Setup your dev environment - Practical Go Lessons
GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/maximilienandile/Library/Caches/go-build" GOENV="/Users/maximilienandile/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/maximilienandile/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/maximilienandile/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GOVCS="" GOVERSION="go1.16" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO
🌐
Google Groups
groups.google.com › g › golang-nuts › c › 9nqpDGzxLgI
cross-compile (linux->windows) with -race failing, even with CGO_ENABLED=1
If you have that setup, you can specify the cross compiler to use with CC_FOR_TARGET (or CXX_FOR_TARGET) https://golang.org/cmd/cgo/
🌐
Bobby Hadz
bobbyhadz.com › blog › go-is-not-recognized-as-internal-or-external-command
'go' is not recognized as an internal or external command | bobbyhadz
April 4, 2024 - If you still get the error that "'go' is not recognized as an internal or external command", you have to add the path to the go executable to your system's PATH environment variable.
🌐
Reddit
reddit.com › r/mhf › 'go' is not recognized as an internal or external command, operable program or batch file.
r/MHF on Reddit: 'go' is not recognized as an internal or external command, operable program or batch file.
January 17, 2022 -

hi guys, I was trying to show off MHFZ to a friend of mine yesterday after I was able to create a server and login into MHFZ but then this error happened. I didn't change any file location and I followed the guide faithfully. I tried to delete everything and extract them from the zip file again but the error is still there please help me.

🌐
GitHub
github.com › golang › go › issues › 16981
cmd/cgo: if CGO_ENABLED=0, cgo files are silently ignored · Issue #16981 · golang/go
September 3, 2016 - CGO_ENABLED=0 go get github.com/FiloSottile/justcgo/plusgo ... This is the simplified case, which might seem like the correct behavior, but how I found this was by cross-compiling github.com/mattn/go-sqlite3/_example/simple, which imports _ github.com/mattn/go-sqlite3, which register a database/sql driver. I expected the command to fail, instead it built a binary.
Author   golang
🌐
GitHub
github.com › golang › go › issues › 64875
cmd/go: -buildmode=pie requires external (cgo) linking, but cgo is not enabled · Issue #64875 · golang/go
December 27, 2023 - $ CGO_ENABLED=0 /usr/lib64/go/1.21/bin/go build -buildmode=pie hellogo.go # command-line-arguments loadinternal: cannot find runtime/cgo
Author   golang
🌐
Medium
medium.com › @pengcheng1222 › exploring-cgo-enabled-in-go-23cf5cf2fe88
Golang — Exploring CGO_ENABLED in Go | by Allen Ning | Medium
November 20, 2024 - The choice of setting CGO_ENABLED to 1 by default on these common platforms stems from a balance between usability and functionality. It caters to ease of use, allowing developers to leverage C libraries without additional setup, and ensures compatibility with essential system libraries.
🌐
Grokbase
grokbase.com › p › gg › golang-nuts › 138p3b42km › go-nuts-re-question-about-running-hello-go
[go-nuts] Question about running hello.go - Grokbase
Michael J Silverstri: It finds 'go.exe', but not 'go' C:\>go 'go' is not recognized as an internal or external command, operable program or batch file. C:\>go.exe Go is a tool for managing Go source code.