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/cgo: cgo-generated header incompatible with latest C++ standards in MSVC #71921

Open
KangJiJi opened this issue Feb 24, 2025 · 7 comments
Open
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@KangJiJi
Copy link

Go version

go version go1.23.3 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\KangJi\AppData\Local\go-build
set GOENV=C:\Users\KangJi\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\KangJi\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\KangJi\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,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.23.3
set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\KangJi\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
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 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\KangJi\AppData\Local\Temp\go-build1436118867=/tmp/go-build -gno-record-gcc-switches

What did you do?

I fixed an issue where cgo-generated header files were not compatible with MSVC when using ISO C++ 17 or later. The headers only worked with ISO C++ 14 or earlier due to certain constructs that were deprecated or removed in newer C++ standards.

To reproduce the issue:

  • Build a DLL using cgo to generate the corresponding header file.
  • Try including the generated header in an MSVC project with C++ standard set to ISO C++ 17 or later (e.g., /std:c++17 or /std:c++20).
  • Compilation fails due to incompatibility with newer C++ standards.

What did you see happen?

When compiling the cgo-generated header file in an MSVC project with ISO C++ 17 or later, the compiler produced errors due to the use of constructs that are no longer valid in newer C++ standards.

Example error messages:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error: missing ';' before identifier 'GoComplex64'
error C4430: missing t¥pe specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error: missing ';' before identifier 'GoComplex128'

What did you expect to see?

I expected the cgo-generated header file to be compatible with MSVC regardless of the C++ standard version being used. It should compile without errors in projects using ISO C++ 17, C++ 20, and later, just as it does with ISO C++ 14 or earlier.

The current output is incorrect because it prevents projects that use newer C++ standards from integrating with cgo-generated code. This issue limits compatibility and forces users to downgrade their C++ standard or modify the generated header manually, which is not an ideal solution. The header should be generated in a way that ensures compatibility with modern C++ versions.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Feb 24, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/651935 mentions this issue: cgo: generated headers are incompatible with the latest C++ standards in MSVC

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Feb 24, 2025
@KangJiJi
Copy link
Author

KangJiJi commented Feb 24, 2025

@cjungm helped with resolving the issue.

@ianlancetaylor
Copy link
Member

cgo header files are used with C code, not C++ code. Is there a problem with C code?

@KangJiJi
Copy link
Author

I understand that the header file in cgo is used only in C code.

However, I felt that the #ifdef __cplusplus shows that there is also consideration for C++ compatibility. That's why I created an issue to highlight one of the advantages of Go, which is writing more universal code.

If we only focus on compatibility with C, the BugReport tag might not be the most appropriate.

I left a new code suggestion on Gerrit. Thank you!

@prattmic
Copy link
Member

cc @golang/compiler

@prattmic prattmic added this to the Backlog milestone Feb 24, 2025
@prattmic prattmic added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 24, 2025
@ianlancetaylor
Copy link
Member

I would expect that MSVC would support some way to write a typedef for a complex number that will work for both C and C++. Are you really sure that can't be done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants