cgo creates invalid C code that works
with -O0 using gcc. The code it generates is valid C++
just not valid C.

In Clear Linux gcc compiler uses at least
-O1 optimization flag (if not optimization is specified), this cause an
issue when cgo is used. The go compiler removes any optimization flag
(even -O0 that should work
correctly) letting gcc use -O1 as default.

Since Solus and Clear Linux patch Go in their distribution, normal go users don't run into this problem, but anyone on those distros who tries to compile go from source runs into this problem.

I don't see any disadvantage to upstreaming this patch. Although go shouldn't be expected to play nicely with any old arbitrarily patched gcc, being explicit about the -O0 requirement instead of implicitly assuming gcc's default seems better all around. Making it easier for Solus and Clear Linux users to build (and contribute to) go makes this a win/win IMO.

Tagging the original author of this patch @jcvenegas in case he wants to submit this and himself and get due credit.

This comment has been minimized.

Because of copyright reasons I don't particularly want to look at a patch that has not gone through the review process. If the original author wants to send in the patch, that would be great.

Otherwise, I take the problem to be that on some Linux distros the compiler defaults to -O1, but the code generated by cgo only works correctly with -O0. Therefore, we should explicitly pass -O0. For #14669 we explicitly removed -O options, but for these distros we need to explicitly add -O0. That seems fine.