-
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: Go type not supported in export: struct #18412
Comments
You can only export basic function signatures. @ianlancetaylor, are the rules documented somewhere? |
The documentation, in https://golang.org/cmd/cgo, is "Not all Go types can be mapped to C types in a useful way." That said, struct types could be handled better than they are. Right now they are a TODO in cmd/cgo/Package.cgoType. Because of that TODO, you can't //export a Go function that uses a struct type. And, frankly, using it in C would be tricky, since C does not have But, of course, we should find a way to make this work. This issue can track that. |
Actually, I think this is working as intended.
The correct program should use a type defined in C, rather than in Go,
because otherwise the external C programs have to manually define
a C struct matching the Go struct, which is non-trivial and error-prone.
(cgo is about interfacing C to Go, not about making Go interfaces
available to C, so it's understandable that cgo doesn't support exporting
arbitrary Go types to C -- you should do the other way around, and
define the necessary structs in C.)
For example, this works today:
package main
/*
struct Vertex {
int X;
int Y;
};
*/
import "C"
import "fmt"
//export getVertex
func getVertex(X, Y C.int) C.struct_Vertex {
return C.struct_Vertex{X, Y}
}
func main() {
fmt.Println(getVertex(1, 2))
}
[Edit: restored indentation in the code.]
|
I'll keep this open as a documentation CL. |
Change https://golang.org/cl/52852 mentions this issue: |
go version
go version go1.7.4 linux/amd64
go env
What did you do?
I am trying to build a shared object from go code (code has a struct and some functions). I am exporting all the functions and then run go build, but I see this error:
Go type not supported in export: struct
. There's this thread on the go-nuts mailing list about this issue, but there doesn't seem to be a resolution.Here's an example program that generates the error:
vertex.go
go build commandline output
What did you expect to see?
I expect the
go build
to run successfully, and generate the shared object.What did you see instead?
I see an error
Go type not supported in export: struct
.The text was updated successfully, but these errors were encountered: