Go version
go version go1.22.4 windows/amd64
Output of go env
in your module/workspace:
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\wukeke\AppData\Local\go-build
set GOENV=C:\Users\wukeke\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=10.88.9.190
set GOMODCACHE=C:\Users\wukeke\go\pkg\mod
set GONOPROXY=10.88.9.190
set GONOSUMDB=10.88.9.190
set GOOS=windows
set GOPATH=C:\Users\wukeke\go
set GOPRIVATE=10.88.9.190
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\wukeke\AppData\Local\Temp\go-build3614417178=/tmp/go-build -gno-record-gcc-switches
What did you do?
test code
$ cat test.go
package main
import "C"
import "fmt"
//export test
func test() {
fmt.Println("just test")
}
func main() {
}
cross-tools
$ aarch64-linux-gnu-ar -V
GNU ar (Linaro_Binutils-2019.12) 2.28.2.20170706
Copyright (C) 2017 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.
build command
$ CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ AR=aarch64-linux-gnu-ar CGO_ENABLED=1 GOOS=linux GOARCH=arm64 GOARM=7 go build -ldflags "-s -w" -buildmode=c-archive -o test.a test.go
What did you see happen?
command-line-arguments
C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running ar failed: exec: "ar": executable file not found in %PATH%
What did you expect to see?
The go build command should successfully use the aarch64-linux-gnu-ar tool as specified by the AR environment variable.
by the way If I copy aarch64-linux-gnu-ar.exe to ar.exe, then the compilation can succeed.
Comment From: gabyhelp
Related Issues and Documentation
- cross compile dynamic library for arm on linux/amd64 #39106 (closed)
- Error building a simple program with GOOS=darwin GOARCH=arm64 environment variables #39033 (closed)
- cmd/link: add internal linking support for pie build mode on arm #50405
- cmd/link: cross compile from MacOS to Windows with CGO_ENABLED=1 and -buildmode=c-archive not working #59221 (closed)
- GOARCH error #64264 (closed)
- In arm64 environment, c# calls go dll exception #66833 (closed)
- cmd/compile: cross compiled arm64 binary can not execute and no response, the program seem to be hung #65361
- cmd/go: get ignores GOARCH environment variable #36022 (closed)
- GOOS=android GOARCH=arm doesn't compile on amd64 #17736 (closed)
- cmd/go: error: unrecognized command line option '-rdynamic' when cross-compiling to ARM with -pie #36633
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: ianlancetaylor
The Go tools don't look at the AR environment variable. Maybe they should.
You can do what you want using -ldflags=-extar=$AR
.
Comment From: seankhliao
Does it look at AR
only for gccgo? https://go.googlesource.com/go/+/b3040679ad0eccaaadb825ed8c0704086ecc23eb/src/cmd/go/internal/work/gccgo.go#50
Comment From: ianlancetaylor
Yes, that is how it works today. Which does seem odd. I think it happened because gccgo has needed to run ar
from the start, but the gc toolchain only started using ar
later on as part of supporting -buildmode=c-archive
.
Comment From: cherrymui
At tip, specifically after CL 592375, the linker now uses $CC --print-prog-name ar
to find the ar
command (if the -extar
flag is not explicitly set). So setting CC=aarch64-linux-gnu-gcc
should be enough for it to find the right ar
. Could you try the tip of the master branch, or Go 1.23rc1? Thanks.
Comment From: gopherbot
Timed out in state WaitingForInfo. Closing.
(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)