Skip to content
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

Closed
ghost opened this issue Dec 22, 2016 · 5 comments

Comments

@ghost
Copy link

commented Dec 22, 2016

go version

go version go1.7.4 linux/amd64

go env

# go env
GOARCH="amd64"
GOBIN="/root/go/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go_pkgs"
GORACE=""
GOROOT="/root/go"
GOTOOLDIR="/root/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build716839048=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

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

package main

import "C"
import "fmt"

type Vertex struct {
  X,Y int
}

//export getVertex
func getVertex(X,Y int) Vertex {
  return Vertex{X,Y}
}

func main() {
  fmt.Println(getVertex(1,2))
}

go build commandline output

# go build -buildmode=c-shared -o vertex.so vertex.go
# command-line-arguments
./struct.go:6:13: Go type not supported in export: struct {
	X, Y int
}
./struct.go:6:13: Go type not supported in export: struct {
	X, Y int
}

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.

@bradfitz

This comment has been minimized.

Copy link
Member

commented Dec 22, 2016

You can only export basic function signatures. @ianlancetaylor, are the rules documented somewhere?

@bradfitz bradfitz changed the title Go type not supported in export: struct cmd/cgo: Go type not supported in export: struct Dec 22, 2016

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Dec 22, 2016

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 :=. You would have to define the struct type in C yourself, and be certain that the C definition is identical to the Go definition. This particular example, for example, is easy to mishandle, especially if you want the code to be portable, since int is different in C and Go.

But, of course, we should find a way to make this work. This issue can track that.

@minux

This comment has been minimized.

Copy link
Member

commented Dec 22, 2016

@bradfitz bradfitz added this to the Go1.9 milestone Dec 22, 2016

@bradfitz

This comment has been minimized.

Copy link
Member

commented Dec 22, 2016

I'll keep this open as a documentation CL.

@bradfitz bradfitz modified the milestones: Go1.9Maybe, Go1.9 Jun 7, 2017

@gopherbot

This comment has been minimized.

Copy link

commented Aug 2, 2017

Change https://golang.org/cl/52852 mentions this issue: cmd/cgo: document that structs and arrays don't work with //export

@gopherbot gopherbot closed this in c3c2e45 Aug 8, 2017

@golang golang locked and limited conversation to collaborators Aug 8, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.