Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CC_FOR_windows_* not honored when cross-compiling #43345

Closed
scrouthtv opened this issue Dec 23, 2020 · 5 comments
Closed

CC_FOR_windows_* not honored when cross-compiling #43345

scrouthtv opened this issue Dec 23, 2020 · 5 comments

Comments

@scrouthtv
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.15.6 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/lenni/.cache/go-build"
GOENV="/home/lenni/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/lenni/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/lenni/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/lenni/git/easy-copy/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build575804832=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I created a small CGo Program which compiles on Windows:

// +build windows

package main

//#include <conio.h>
//#include <stdio.h>
import "C"

import "fmt"

func main() {
	ch := Getch()
	fmt.Printf("I got a %c\n", ch)
}

func Getch() rune {
	var ch int = int(C.getch())
	return rune(ch)
}

I am using Linux archlinux 5.9.14-arch1-1 #1 SMP PREEMPT Sat, 12 Dec 2020 14:37:12 +0000 x86_64 GNU/Linux.
When I compile this using

GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=/usr/bin/x86_64-w64-mingw32-cc go build main.go

I get an executable that runs as expected (for reference, getch() reads a single keypress without having to press enter).
If I replace the CC= with CC_FOR_windows_amd64, as mentioned in https://golang.org/cmd/cgo/#:~:text=When%20cross-compiling%2C%20you,run%20the%20go%20tool :

GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC_FOR_windows_amd64=/usr/bin/x86_64-w64-mingw32-cc go build main.go

The build fails with

# runtime/cgo
gcc: error: unrecognized command-line option ‘-mthreads’; did you mean ‘-pthread’?

So it looks like my linux gcc is called (which knows -pthread) instead of the mingw gcc (which knows -mthreads).

What did you expect to see?

Both commands work equally (the one with CC as well as the one with CC_FOR_windows_amd64 specified).

What did you see instead?

go build with CC specified works as intended. go build with CC_FOR_windows_amd64 fails because it uses the wrong c compiler.

@scrouthtv
Copy link
Author

Also see #40616

@ianlancetaylor
Copy link
Contributor

I think you are misreading the docs (and maybe the docs are confusing). Setting CC_FOR_windows_amd64 only applies when running make.bash.

@scrouthtv
Copy link
Author

What is make.bash and how do I use it? I found no docs on it so far.

@ianlancetaylor
Copy link
Contributor

make.bash is used when building a Go installation from source, as described at https://golang.org/doc/install/source.

When actually using Go, rather than building it from source, you need to set CC.

@scrouthtv
Copy link
Author

Oh ok, thank you very much.

@golang golang locked and limited conversation to collaborators Dec 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants