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: Opaque pointers handled incorrectly #6473

lukescott opened this issue Sep 24, 2013 · 2 comments


Copy link

commented Sep 24, 2013

What steps will reproduce the problem?

This code:

#ifdef __cplusplus
class Test1 {};
class Test2 {};
class Test3 {};
typedef struct Test1 Test1;
typedef void Test2;
typedef int Test3;

Should generate Go types and use them in functions.

What is the expected output?


// ...
type _Ctype_void [0]byte
type _Ctype_int int32
type _Ctype_Test1 _Ctype_void
type _Ctype_Test2 _Ctype_void
type _Ctype_Test3 _Ctype_int

func _Cfunc_TestFunc1(*_Ctype_Test1) _Ctype_void
func _Cfunc_TestFunc2(*_Ctype_Test2) _Ctype_void
func _Cfunc_TestFunc3(*_Ctype_Test3) _Ctype_void

What do you see instead?


// ...
type _Ctype_Test2 _Ctype_void
type _Ctype_Test3 _Ctype_int
type _Ctype_int int32
type _Ctype_void [0]byte

func _Cfunc_TestFunc1(*[0]byte) _Ctype_void
func _Cfunc_TestFunc2(unsafe.Pointer) _Ctype_void
func _Cfunc_TestFunc3(*_Ctype_Test3) _Ctype_void

Which compiler are you using (5g, 6g, 8g, gccgo)?

gc default
g++ 4.8 (GNU) OR clang++

Which operating system are you using?

OS X 10.8.5

Which version are you using?  (run 'go version')

go version devel +f4d1cb8d9a91 Thu Sep 19 22:34:33 2013 +1000 darwin/amd64

Please provide any additional information below.

Run it like this:

CXX=g++-4.8 go tool cgo -- cgotest.go
CXX=clang++ go tool cgo -- cgotest.go

Test case attached.


  1. (7656 bytes)

This comment has been minimized.

Copy link

commented Oct 18, 2013

Comment 1:

You're lying to cgo. Since you're already lying, it won't hurt to lie a little more:
typedef struct Test1 { int x; } Test1;
and that will work fine.

Status changed to WorkingAsIntended.


This comment has been minimized.

Copy link

commented Oct 18, 2013

Comment 2:

For Test3, sure I am. And it works. Test2 isn't a unique type, it's just an alias of
void (another typedef of void would be considered the same type in C).
Test1 is a unique and honest type. It declares "something" that can be passed around as
a pointer, but not understandable by C.
These two types are considered different in C:
typedef struct Foo Foo;
typedef struct Bar Bar;
Yet Go translates both explicitly to [0]byte. The two should be separately translated to
_Ctype_Foo _Ctype_Bar.
Strangely in my example Test2 is given a type, but that type is not used. Test1 is a
legitiment type, yet none is generated or used.
I guess to make this work I will have to continue lying to cgo. Although I don't
consider "typedef struct Test1 Test1;" a lie at all.

@golang golang locked and limited conversation to collaborators Jun 25, 2016

This issue was closed.

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