Navigation Menu

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: exported go functions can't use C struct types (regression from 1.2) #8148

Closed
cookieo9 opened this issue Jun 4, 2014 · 11 comments
Closed
Milestone

Comments

@cookieo9
Copy link
Contributor

cookieo9 commented Jun 4, 2014

Consider: http://play.golang.org/p/UuuyA-zII7

This package compiles in go 1.2 and the GetNum go function returns 42 as expected. In go
1.3 the compilation fails with:

In file included from $WORK/cgobreak/_obj/_cgo_export.c:2:
./code.go:40:30: warning: declaration of 'struct __0' will not be visible outside of
this function [-Wvisibility]
./code.go:40:12: error: conflicting types for 'goCallback'
./code.go:6:6: note: previous declaration is here
/var/folders/52/y5kwxpqj1vqd3n6j1ndnx3200000gn/T/go-build778103108/cgobreak/_obj/_cgo_export.c:8:23:
warning: declaration of 'struct __0' will not be visible outside of this function
[-Wvisibility]
/var/folders/52/y5kwxpqj1vqd3n6j1ndnx3200000gn/T/go-build778103108/cgobreak/_obj/_cgo_export.c:8:5:
error: conflicting types for 'goCallback'
./code.go:6:6: note: previous declaration is here

Comparing the results of manually running cgo of both versions shows that the function
generated by the export directive goes from:

int goCallback(Gen* p0)

in 1.2 to:

int goCallback(struct __0* p0)

Essentially the C-type is ignored and replaced with an unknown, anonymous struct pointer
type which causes clang to fail to compile it.

Tested on darwin/amd64 with both 1.2.2 and tip.
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-go1.3maybe.

@ianlancetaylor
Copy link
Contributor

Comment 2:

Workaround for this test case: give the struct a tag.  For example:
typedef struct gen_struct { int n; } Gen;

@cookieo9
Copy link
Contributor Author

cookieo9 commented Jun 4, 2014

Comment 3:

I can confirm that that works, but obviously it would be good to fix completely, since
there are many cases where you won't be able to change the definition of the struct.

@cookieo9
Copy link
Contributor Author

cookieo9 commented Jun 4, 2014

Comment 4:

Also in a real-world case, it fixes go-qml, which I noticed had problems with two of
it's types at tip.

@ianlancetaylor
Copy link
Contributor

Comment 5:

Owner changed to @ianlancetaylor.

Status changed to Started.

@ianlancetaylor
Copy link
Contributor

Comment 6:

Can you see if https://golang.org/cl/103080043 fixes the real world test cases?

@gopherbot
Copy link

Comment 7:

CL https://golang.org/cl/103080043 mentions this issue.

@cookieo9
Copy link
Contributor Author

cookieo9 commented Jun 4, 2014

Comment 8:

Re #6: go-qml now builds again with that CL for tip.

@ianlancetaylor
Copy link
Contributor

Comment 9:

This issue was closed by revision c22ed12.

Status changed to Fixed.

@gopherbot
Copy link

Comment 10:

CL https://golang.org/cl/103900046 mentions this issue.

@rsc
Copy link
Contributor

rsc commented Jun 7, 2014

Comment 11:

This issue was closed by revision 964b3798876c.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
rsc added a commit that referenced this issue May 11, 2015
…ypedef name in C code

««« CL 103080043 / 5e058e21b67d
cmd/cgo: for typedef of untagged struct, use typedef name in C code

Fixes #8148.

LGTM=cookieo9, rsc
R=rsc, cookieo9
CC=golang-codereviews
https://golang.org/cl/103080043
»»»

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews, r
https://golang.org/cl/103900046
@golang golang locked and limited conversation to collaborators Jun 25, 2016
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants