This should work:

Copygo env -w CGO_ENABLED=1

But if you don't have a C compiler installed on your machine, you will get another error message after setting this variable and trying to use go run -race .:

cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH%

If so, here are VS Code's instructions on how to install it

  • Windows: https://code.visualstudio.com/docs/cpp/config-mingw
  • Linux: https://code.visualstudio.com/docs/cpp/config-linux
Answer from Krista Korund on Stack Overflow
🌐
Medium
medium.com › @pengcheng1222 › exploring-cgo-enabled-in-go-23cf5cf2fe88
Golang — Exploring CGO_ENABLED in Go | by Allen Ning | Medium
November 20, 2024 - CGO_ENABLED is a pivotal environment variable in Go’s build process, controlling the use of cgo, the tool that facilitates calling C code from Go. By default, its value varies based on the operating system and architecture, typically being set to 1 on standard platforms like Windows, macOS, and Linux (for amd64, 386, and arm architectures), enabling the integration of C libraries in Go applications.
Discussions

Consider using `CGO_ENABLED=1` for default macOS build
I've personally encountered the ... app for mac that makes external requests (docker compose, flyctl, Pocketbase). Issue is that whenever request happens, in case of Pocketbase it's OAuth request, it hangs my network stack and system reboot is required. ... Beta Was this translation helpful? Give feedback. ... Hm, that's very strange behavior. Thanks for letting me know. I don't want to set CGO_ENABLED=1 because ... More on github.com
🌐 github.com
1
1
October 30, 2022
cmd/link: cross compile from MacOS to Windows with CGO_ENABLED=1 and -buildmode=c-archive not working
What version of Go are you using (go version)? $ go version go version go1.20.2 darwin/arm64 Does this issue reproduce with the latest release? Yes What operating system and processor architecture ... More on github.com
🌐 github.com
16
March 24, 2023
go - How to compile an amd64 binary that uses C on an M1 (arm64) Mac - Stack Overflow
In my case, I also needed to set CGO_ENABLED to true. The app now compiles fine for amd64 with GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build ... Sign up to request clarification or add additional context in comments. ... Congrats! I will update my answer to point to your answer. 2021-12-25T22:23:58.837Z+00:00 ... The answer to the wasm question (as you posted) talks about cgo. cgo invokes platform compiler with platform specific headers/libs (on Mac... More on stackoverflow.com
🌐 stackoverflow.com
Releaser build with CGO_ENABLED=0 doesn't run on Mac M1
Describe the bug When I run latest v4.3.1 Mac arm binary I get: 2023/08/18 13:47:54 /home/runner/go/pkg/mod/gorm.io/gorm@v1.24.1-0.20221019064659-5dd2bb482755/gorm.go:206 [error] failed to initiali... More on github.com
🌐 github.com
1
August 18, 2023
🌐
Go Packages
pkg.go.dev › cmd › cgo
cgo command - cmd/cgo - Go Packages
April 7, 2026 - It is disabled by default when ... can override the default 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....
🌐
GitHub
github.com › golang › go › issues › 59221
cmd/link: cross compile from MacOS to Windows with CGO_ENABLED=1 and -buildmode=c-archive not working · Issue #59221 · golang/go
March 24, 2023 - wsong@Work: ~/Development/test $ cat go.mod module test go 1.20 wsong@Work: ~/Development/test $ cat test.c // Functions exported by Go. extern int Test(); int test() { Test(); return 0; } wsong@Work: ~/Development/test $ cat test.go package main //#include "errno.h" import "C" func main() {} //export Test func Test() C.int { return 0 } wsong@Work: ~/Development/test $ cat example/main.c extern int test(); int main(void) { test(); return 0; } ... CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -v -a -buildmode=c-archive .
Author   golang
🌐
GitHub
github.com › micro › micro › issues › 2049
Releaser build with CGO_ENABLED=0 doesn't run on Mac M1 · Issue #2049 · micro/micro
August 18, 2023 - Download https://github.com/mi... is a CGO enabled package, you are required to set the environment variable CGO_ENABLED=1 and have a gcc compiler present within your path....
Author   micro
Find elsewhere
🌐
GitHub
github.com › go-task › task › issues › 1272
`CGO_ENABLED` cannot be set to `1` · Issue #1272 · go-task/task
July 21, 2023 - Setting the environment variable within the command string itself (e.g. CGO_ENABLED=1 go env CGO_ENABLED) results in the expected behaviour.
Author   go-task
🌐
Reddit
reddit.com › r/golang › cross compiling to darwin/arm64 (apple silicon, m1 chip) with cgo requires cgo_enabled=1
r/golang on Reddit: Cross compiling to darwin/arm64 (apple silicon, M1 chip) with cgo requires CGO_ENABLED=1
February 26, 2021 -

I guess I should have known this, but spent all day down wrong rabbit holes of compilers and deployment targets, trying to figure out why go build was skipping my files with import "C" in them when compiling to darwin/arm64 for new apple silicon.

I've done cross-compiling to wasm and bsd before, but never with cgo, so I never new CGO_ENABLED=1 had to be set to cross-compile with cgo.

Hope this can save someone else time too.

Top answer
1 of 2
19

Many things are only available as C libraries, and re-implementing that all in Go would be costly. cgo has its downsides, but it can be a good trade-off. Even the standard library uses it (net for DNS lookups, os/user for user lookups) because it doesn't re-implement 100% of the behaviour in Go.

Cross-compiling C code is still rather hard; you'll need the target architecture's C compiler and toolchain (e.g. CC=aarch64-linux-musl-gccgo build to build an arm64 binary). None of that is installed by default so for most people cgo simply won't work when cross-compiling; they need to take manual steps to set it up first.

cgo often isn't strictly required (like in the net and os/user packages), so disabling it by default seems the most user-friendly option.

But there are no such constraints on the native platform, and it's expected to work by default without any user setup; so why not enable it by default?

2 of 2
8

If you're running on an Alpine image, it is impossible to compile and run Go programs in Alpine images right away. You must disable CGO by setting the environment variable CGO_ENABLED to false (the default value is true).

You can do this either by:

  • Adding go env -w CGO_ENABLED=0 like Robert mentions in his comment or,
  • Setting the env value prefixing it in the go command, e.g.: CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags '-s -w' -tags lambda.norpc -o bin/<YOUR_FUNC>/bootstrap <YOUR_PATH>/main.go

See: https://megamorf.gitlab.io/2019/09/08/alpine-go-builds-with-cgo-enabled/

🌐
GoLinuxCloud
golinuxcloud.com › home › programming › getting started with cgo using visual studio code
Getting started with CGO using Visual Studio Code | GoLinuxCloud
January 7, 2024 - 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.
🌐
Google Groups
groups.google.com › g › golang-nuts › c › Nyb9jU2onfQ
building a linux binary with -race on a mac
I am writing and building my code on a mac. The code is a linux binary though so its obviously a cross compilation. This works fine: GOOS=linux GOARCH=amd64 go build . ... GOOS=linux GOARCH=amd64 go build -race . go build: -race requires cgo; enable cgo by setting CGO_ENABLED=1
🌐
GitHub
github.com › orgs › community › discussions › 46166
Compile a Go project with `CGO_ENABLED=1` across Linux/Darwin/Windows · community · Discussion #46166
February 2, 2023 - An env section that sets CGO_ENABLED to 1 (either for a step, or the whole job or even workflow). Or you can just include CGO_ENABLED=1 in the command line, run sections just run your commands in a shell.
🌐
Bytebase
bytebase.com › blog › engineering › how to cross compile with cgo using goreleaser and github actions
How to cross compile with CGO using GoReleaser and GitHub Actions | Bytebase
August 24, 2022 - build constraints exclude all Go files in /go/pkg/mod/github.com/pganalyze/pg_query_go/v2@2.1.2/parser · This error message may seem a bit puzzling, but it points to exactly what’s wrong. So let's go to the package "pg_query_go/parser" and find out. The package "pg_query_go/parser" contains only one Go file, parser.go, but otherwise, it's all C code. The "parser.go" is a CGO file that imports "C." It's easy to guess that the Go parser ignores this file when not set CGO_ENABLE=1.
🌐
Medium
medium.com › crypdex › cross-compiling-go-for-docker-on-arm64-with-cgo-enabled-1-507ee5b39032
Cross-compiling Go for Docker on ARM64 with CGO_ENABLED=1 | by David Michael | CRYPDEX | Medium
February 21, 2019 - FROM golang:1.11 AS builder # Magic ...thub.com/org/repo WORKDIR /go/src/github.com/go/repoRUN CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOOS=linux GOARCH=arm64 go build -o app ....
🌐
Boinkor
boinkor.net › 2023 › 05 › building-a-golang-program-with-cgo
Building a golang program with cgo - Andreas Fuchs’ Journal
May 25, 2023 - There are two main things you need to do: First, explicitly opt out of disabling the usage of the cgo compiler by setting CGO_ENABLED=1 on the compiler’s process environment. (This defaults to on, but your environment might have it turned off!
🌐
Hugo
gohugo.io › installation › macos
macOS
1 week ago - To install the extended edition of Hugo: ... To build and install the extended edition, first install a C compiler such as GCC or Clang and then run the following command: CGO_ENABLED=1 go install -tags extended github.com/gohugoio/hugo@latest
🌐
Reddit
reddit.com › r/golang › cross build on linux with cgo with github ci/cd
r/golang on Reddit: cross build on Linux with cgo with github ci/cd
October 27, 2021 -

hi,

I'm trying to cross build on Linux a project which uses https://github.com/shirou/gopsutil.

Everything fine except for MacOS: I get "not implement". Looking at https://github.com/shirou/gopsutil/tree/master/v3/cpu code it seems it's because I don't build with cgo and the MacOS (darwin) version only works with cgo.

How do I go build on Linux with cgo with MacOS as target ?

CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build main.go

gives an error (gcc: error: unrecognized command-line option '-arch). I guess I'm missing a cross compile C tool chain ?

Complementary to this, is there any github actions (ci/cd) that can do that ? Ideally I'd like to have binaries for all platforms built and published when a tagged release is committed. Can Github do that (with cgo, I know it can without) ?

sample code : https://gist.github.com/kgersen/ca265719c76ed1a41e98c2801135b3b8 (cross building on Linux for darwin and running on darwin gives 'not implemented').