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: warning: built-in function ‘free’ declared as non-function #32411
Comments
Does the code work other than producing a warning? |
It’s actually a bit hard to trace in the real code, but the following code: package main
// #include <stdlib.h>
// void __attribute__((noinline)) call_free(void (*f)(void *), char *p) { f(p); }
import "C"
import "fmt"
func main() {
p := C.CString("test")
fmt.Printf("p: %#v\n", p)
C.call_free((*[0]byte)(C.free), p)
} appears to work as expected:
|
I can reproduce this doing
|
@yath This occurs because you are referencing the
The extern declaration of free is what the compiler is (correctly) complaining about. According to https://golang.org/cmd/cgo/#hdr-Go_references_to_C (you have to scroll down a bit), direct references to C function pointers are not supported. The document provides an example of how to work around this limitation in a non-trivial example. |
Hi @qbradq,
Sorry, I’m unable to find what you are referring to. The only mention of function pointers in the link you’ve provided is “Calling C function pointers is currently not supported”, but I’m not calling a function pointer, I’m just passing it to a different function. I can’t find anything about free (the only symbol that exposes this behavior) being special either. What am I missing? |
Note that that warning is coming from the C compiler. It's not coming from any Go tool. It is the C compiler that considers |
@yath I pointed out the documentation because I thought it might provide you with a solution to the real-world problem behind this ticket. I assumed there was a real-world problem behind the trivialized example given. |
@qbradq, the example you are referring to is the one following “Calling C function pointers is currently not supported, however you can declare Go variables which hold C function pointers and pass them back and forth between Go and C. C code may call function pointers received from Go. For example:”? If so, then that’s exactly what I’m doing here, except that If you are referring to a different example, please provide a more specific link. |
@yath This is the work-around I was suggesting. Maybe we could add an example to the documentation about how to work around C built-in symbols specifically. I am not sure a compile-time check would be appropriate as you can never be 100% sure what a C compiler will complain about. But one can be added for the symbols in a specific C standard. I'm just not sure what the Go team's policy is on how compatible they want cgo to be with strange and unusual compilers :) |
As I wrote initially, this is only happening with I’m not saying that what I’m doing (passing |
Unfortunately this is going to be one of those, "Your mileage may vary" kind of things. Do you think adding the following to the Cgo documentation would have helped you?
|
@yath |
It may be possible to work around this problem by having cgo pass |
If |
I think that it would only be needed when invoking the C compiler to determine the types of the functions, I don't think it would be needed for the final build. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
go run
the following program that is usingC.free
as a value. (In actual code, it’s passed to afoo_set_free_fun(void (*)(void *))
.) The warning is not emit when callingC.free()
, nor is it when e.g. printing the address ofC.getenv
.What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: