Skip to content

cmd/go: cgo passes -mthreads to the C compiler on Windows, rejected by clang targeting MSVC #80290

Description

@m-this

Go version

go1.26.4 windows/amd64

What did you do

Built a cgo program on Windows using clang as the C compiler, specifically the one bundled with Visual Studio 2026 (clang 22.1.3, targets x86_64-pc-windows-msvc by default):

set CGO_ENABLED=1
set CC=clang
go build .

Any build that pulls in runtime/cgo reproduces it. Standalone repro with a failing CI run on a stock GitHub windows-latest runner: https://github.com/m-this/go-cgo-mthreads-repro (run: https://github.com/m-this/go-cgo-mthreads-repro/actions/runs/28890285104, built with go build -x so the full compiler invocation is visible).

What did you see happen

# runtime/cgo
clang: error: unsupported option '-mthreads' for target 'x86_64-pc-windows-msvc'

cmd/go unconditionally appends -mthreads to the compiler command line for cgo builds on Windows (ccompilerCmd in src/cmd/go/internal/work/exec.go), visible in GOGCCFLAGS=-m64 -mthreads .... The flag is MinGW-only. Clang used to accept it with an "argument unused during compilation" warning, but since the driver started treating MinGW link flags as target specific (llvm/llvm-project D151590 and follow-ups), it is a hard error for non-MinGW targets. So any cgo build with CC=clang on such a toolchain fails at runtime/cgo, before compiling any user code.

#16932 reported the broader "cgo assumes MinGW" problem in 2016 and was closed without a fix.

What did you expect to see

The build succeeding. -mthreads could be passed only when the compiler actually targets MinGW, or probed with the existing gccSupportsFlag mechanism like other optional flags.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions