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

cmd/go: CGO_CFLAGS variable with spaces is parsed incorrectly on windows #45637

Closed
justinfx opened this issue Apr 19, 2021 · 6 comments
Closed
Labels
GoCommand cmd/go help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@justinfx
Copy link

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

$ go version
go version go1.16.2 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using?

windows10, amd64, intel

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\justin\AppData\Local\go-build
set GOENV=C:\Users\justin\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\justin\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\justin\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.2
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\justin\AppData\Local\Temp\go-build995163971=/tmp/go-build -gno-record-gcc-switches

What did you do?

While attempting to build a cgo project on windows, I am finding that the value CGO_CFLAGS is parsed incorrectly if an include path contains spaces.

.
├── go.mod
├── main.c
└── main.go
// main.go
package main

import "C"

func main() {}
// main.c
// <empty>
> set CGO_CFLAGS='-IC:/Program Files/Foo'
> go env CGO_CFLAGS
'-IC:/Program Files/Foo'

> go build -x
WORK=C:\Users\justin\AppData\Local\Temp\go-build1942282733
mkdir -p $WORK\b003\
cd C:\Program Files\Go\src\runtime\cgo
TERM='dumb' CGO_LDFLAGS='"-g" "-O2"' "C:\\Program Files\\Go\\pkg\\tool\\windows_amd64\\cgo.exe" -objdir "$WORK\\b003\\" -importpath runtime/cgo -import_runtime_cgo=false -import_syscall=false -- -I "$WORK\\b003\\" '-IC:/Program Files/Foo' -Wall -Werror "C:\\Program Files\\Go\\src\\runtime\\cgo\\cgo.go"
# runtime/cgo
gcc: error: '-IC:/Program: Invalid argument
gcc: error: Files/Foo': No such file or directory

I've also tried various combinations of quoting and escaping with no change in result:

set CGO_CFLAGS=-I'C:/Program Files/Foo'
set CGO_CFLAGS='-IC:/Program\ Files/Foo'
set CGO_CFLAGS='-IC:/Program\\ Files/Foo'

What did you expect to see?

I would expect the full single string value of "C:/Program Files/Foo" to be passed to the "-I" include flag when compiling.

What did you see instead?

The path is split on spaces, acting like multiple arguments to gcc

# runtime/cgo
gcc: error: '-IC:/Program: Invalid argument
gcc: error: Files/Foo': No such file or directory
@justinfx
Copy link
Author

Similar to #41400

@ianlancetaylor ianlancetaylor changed the title runtime/cgo: CGO_CFLAGS variable with spaces is parsed incorrectly on windows cmd/go: CGO_CFLAGS variable with spaces is parsed incorrectly on windows Apr 19, 2021
@ianlancetaylor ianlancetaylor added help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Apr 19, 2021
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Apr 19, 2021
@networkimprov
Copy link

cc @alexbrainman @mattn @zx2c4

@bminer
Copy link

bminer commented Feb 25, 2025

Is this still an issue? When I run go env, I get something like this:

set AR=ar
set CC=gcc
set CGO_CFLAGS="-I'C:/Program" Files (x86)/National "Instruments/Shared/ExternalCompilerSupport/C/include'"
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=1
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS="-L'C:/Program" Files (x86)/National "Instruments/Shared/ExternalCompilerSupport/C/lib64/msvc'"
set CXX=g++
...

Notice the strange extra double quote between Program and Files: -I'C:/Program" Files (x86)/...

It appears correct from cmd.exe:

echo %CGO_CFLAGS%
-I'C:/Program Files (x86)/National Instruments/Shared/ExternalCompilerSupport/C/include'

Running go version go1.24.0 windows/amd64

Some compilers don't like this very much:

CC=C:/Users/bminer/mingw64/bin/x86_64-w64-mingw32-gcc.exe go build ./cmd/web
# runtime/cgo
x86_64-w64-mingw32-gcc.exe: warning: Files: linker input file unused because linking not done
x86_64-w64-mingw32-gcc.exe: error: Files: linker input file not found: No such file or directory
x86_64-w64-mingw32-gcc.exe: warning: (x86)/National: linker input file unused because linking not done
x86_64-w64-mingw32-gcc.exe: error: (x86)/National: linker input file not found: No such file or directory
x86_64-w64-mingw32-gcc.exe: warning: Instruments/Shared/ExternalCompilerSupport/C/include': linker input file unused because linking not done
x86_64-w64-mingw32-gcc.exe: error: Instruments/Shared/ExternalCompilerSupport/C/include': linker input file not found: No such file or directory

whereas others build okay:

CC=C:/cygwin64/bin/x86_64-w64-mingw32-gcc.exe go build ./cmd/web
# build is okay!

@bminer
Copy link

bminer commented Feb 25, 2025

I wonder if the bug is here: https://github.com/golang/go/blob/master/src/cmd/cgo/gcc.go#L140

Or perhaps if the bug is downstream in the GCC compiler. For example, splitQuoted takes the input -I'C:/Program Files (x86)/National Instruments/Shared/ExternalCompilerSupport/C/include' and returns a single element array with the string -IC:/Program Files (x86)/National Instruments/Shared/ExternalCompilerSupport/C/include. I wonder if this is passed down correctly when the compiler is invoked...

@bminer
Copy link

bminer commented Feb 25, 2025

Actually, the more I look here, the more I think it's actually a compiler issue, not a Go bug. This issue can probably be closed.

@matloob
Copy link
Contributor

matloob commented Mar 28, 2025

Thanks for looking into this @bminer! It looks like this is working as intended. CGO_CFLAGS is a space separated list. double quotes can be used to quote elements with spaces.

@matloob matloob closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

6 participants