-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: importing "C" with CGO disabled should be an error #12860
Comments
cmd/go is a little difficult to diagnose this problem
because when cgo is disabled, go/build will not
report any CgoFiles in struct Package (the files
that need cgo will be in the IgnoredGoFiles slice.)
|
@minux, could we augment go/build to set a field, NeedsCGO, or HasCGO, or On Wed, Oct 7, 2015 at 4:16 AM, Minux Ma [email protected] wrote:
|
I'm worrying about compatibility with the old behavior.
Essentially each file importing "C" used to have an implicit cgo build tag,
is there any packages out there that uses this hidden feature?
I guess we can provide a hint if the package fails to compile with cgo
disabled.
But what if a client of the package fails to compile due to lack of cgo
support in this package?
|
What if the user is providing a non-cgo implementation as well?
We shouldn't fail this package just because it is importing C. In Go, build is constrained to the matching files for the given context. We can improve the current "no buildable Go source files" error message but anything too smart will break the existing use cases. |
When there are no build directives in any Go source files, this should imply that all sources should be used on all platforms. (Except for files with platform specific names, e.g. source_windows.go) Any Go files that import C should build always, unless it is set for cgo-only with a build directive. Without such directive, not having cgo enabled should be an error. |
@pebbe I can't think of a backward-compatible way of doing that. Can you? Whether we like it or not, I think the solution here is for the "no buildable Go source files" to explain which files were excluded and why. For example:
|
The problem is when there are some source files that import C, and some that don't, the latter using functions that are defined in the first. Than you get errors like:
People report issues because they don't understand this. I tried adding this to Go sources that import C:
That doesn't work. My current solution is to add an empty C file and at least one Go file that doesn't import C. Perhaps there could be a build directive that says that it is an error if a file is not used in the build, that overrules the implicit cgo directive, like:
|
What about putting
How does that solve anything? I thought you already had Go files that don't import C in that package. |
There are two cases. Packages where some Go source files import C, and packages where all Go files import C. Without cgo, in the first case you get the "no buildable source files", and in the second the "undefined" errors. Both are confusing. They don't tell what the real problem is. In the second case, I only add an empty C file. This results in the error "C source files not allowed when not using cgo or SWIG". For this to work in the first case, you have to add an empty Go file as well, one that doesn't import C. |
That's #24068; consolidating into that issue. |
go version go1.5.1 linux/amd64
Building with
CGO_ENABLED=0
silently skips source files that haveimport "C"
, leading to incomprehensible error messages from other source files that depend on it.There should be an error, like:
The text was updated successfully, but these errors were encountered: