title: How to cross-compile from linux/amd64 to darwin/arm64 with cgo?
body: <p>I am trying to cross-compile <code>darwin/arm64</code> on <code>linux/amd64</code>, I got the below error when I build code with <code>CGO_ENABLED=1</code>, I understand the error, but I don't how to solve.</p>
  <p>My code must enable <code>CGO_ENABLED</code> feature to compile.</p>
  <p>ErrorMessage:</p>
  <pre><code>GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 CC=gcc go build -o dist/darwin-arm64 cmd/main.go
  # runtime/cgo
  gcc: error: arm64: No such file or directory
  gcc: error: unrecognized command line option '-arch'
  make: *** [makefile:6: darwin] Error 2
  </code></pre>
  <p>Host System:</p>
  <pre><code>Linux 83433d127edb 5.13.0-28-generic #31~20.04.1-Ubuntu SMP Wed Jan 19 14:08:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
  </code></pre>
  <p>Go Environment:</p>
  <pre><code>GO111MODULE=&quot;&quot;                                                                                                                       
  GOARCH=&quot;amd64&quot;                                                                                                                       
  GOBIN=&quot;&quot;                                                                                                                             
  GOCACHE=&quot;/root/.cache/go-build&quot;                                                                                                      
  GOENV=&quot;/root/.config/go/env&quot;                                                                                                         
  GOEXE=&quot;&quot;                                                                                                                             
  GOEXPERIMENT=&quot;&quot;                                                                                                                      
  GOFLAGS=&quot;&quot;                                                                                                                           
  GOHOSTARCH=&quot;amd64&quot;                                                                                                                   
  GOHOSTOS=&quot;linux&quot;                                                                                                                     
  GOINSECURE=&quot;&quot;                                                                                                                        
  GOMODCACHE=&quot;/root/go/pkg/mod&quot;                                                                                                        
  GONOPROXY=&quot;&quot;                                                                                                                         
  GONOSUMDB=&quot;&quot;                                                                                                                         
  GOOS=&quot;linux&quot;                                                                                                                         
  GOPATH=&quot;/root/go&quot;                                                                                                                    
  GOPRIVATE=&quot;&quot;                                                                                                                         
  GOPROXY=&quot;https://proxy.golang.org,direct&quot;                                                                                            
  GOROOT=&quot;/usr/lib/go-1.18&quot;                                                                                                            
  GOSUMDB=&quot;sum.golang.org&quot;                                                                                                             
  GOTMPDIR=&quot;&quot;                                                                                                                          
  GOTOOLDIR=&quot;/usr/lib/go-1.18/pkg/tool/linux_amd64&quot;                                                                                    
  GOVCS=&quot;&quot;                                                                                                                             
  GOVERSION=&quot;go1.18&quot;                                                                                                                   
  GCCGO=&quot;gccgo&quot;                                                                                                                        
  GOAMD64=&quot;v1&quot;                                                                                                                         
  AR=&quot;ar&quot;                                                                                                                              
  CC=&quot;gcc&quot;                                                                                                                             
  CXX=&quot;g++&quot;                                                                                                                            
  CGO_ENABLED=&quot;1&quot;                                                                                                                      
  GOMOD=&quot;/home/worker/go.mod&quot;                                                                                                          
  GOWORK=&quot;&quot;                                                                                                                            
  CGO_CFLAGS=&quot;-g -O2&quot;                                                                                                                  
  CGO_CPPFLAGS=&quot;&quot;                                                                                                                      
  CGO_CXXFLAGS=&quot;-g -O2&quot;                                                                                                                
  CGO_FFLAGS=&quot;-g -O2&quot;                                                                                                                  
  CGO_LDFLAGS=&quot;-g -O2&quot;                                                                                                                 
  PKG_CONFIG=&quot;pkg-config&quot;                                                                                                              
  GOGCCFLAGS=&quot;-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1206945591=/tmp/go-build -gno-record-gcc-switches&quot;
  </code></pre>