-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Comments
Change https://go.dev/cl/651935 mentions this issue: |
Related Issues
Related Code Changes
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
@cjungm helped with resolving the issue. |
cgo header files are used with C code, not C++ code. Is there a problem with C code? |
I understand that the header file in cgo is used only in C code. However, I felt that the 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! |
cc @golang/compiler |
I would expect that MSVC would support some way to write a |
It currently works in both C and C++. However, it doesn't work with the latest C++ Language Standard in MSVC, and there is a solution for that. With the ISO C++ 14 Standard (/std:c++14), it builds correctly. With the ISO C++ 17 Standard (/std:c++17) or higher, it does not build correctly.
|
Please paste plain text as plain text. Images are much harder to read. Thanks. That said, I still don't understand whether there is a way to write this code that will work for both C and C++. |
FYI i was able to workaround the issue by defining
See the 🟢 CI: https://github.com/FedeDP/container_plugin/actions/runs/13538922159/job/37835554110?pr=20 Info found here: https://developercommunity.visualstudio.com/t/_Fcomplex-and-_Dcomplex-removed-in-Windo/10108779#T-N10301376 and here: microsoft/STL#3280 (comment) |
My proposed solution is to modify the code as follows. #ifdef _MSC_VER
#if !defined(__cplusplus) || _MSVC_LANG <= 201402L
#include <complex.h>
typedef _Fcomplex GoComplex64;
typedef _Dcomplex GoComplex128;
#else
#include <complex>
typedef std::complex<float> GoComplex64;
typedef std::complex<double> GoComplex128;
#endif
#else
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
#endif Of course, I have also uploaded this change to Gerrit. Since there is a note at the top of the header file stating that it is automatically generated and should not be modified manually, I tried to avoid making direct changes to it.
However, I made the modification because, without it, the code cannot be built in ISO C++17 or later. I also hope this change helps others who might encounter the same issue. Appendix |
Hello, I am working on resolving this issue together with @KangJiJi . First of all, thank you for sharing your valuable input. @FedeDP We have reviewed your suggestion regarding However, this approach seems to downgrade the functionality of the new MSVC version to mimic that of an older version. Info found here: https://learn.microsoft.com/en-us/answers/questions/1187748/error-compiling-a-c-file-generated-from-golang-err |
SGTM ! |
Go version
go version go1.23.3 windows/amd64
Output of
go env
in your module/workspace: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:
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:
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.
The text was updated successfully, but these errors were encountered: