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: cgo gets confused by equivalent #defined types #13636
Comments
We need a test case. Even then, it is likely that there is nothing we can do. In C, a typedef is an alias. In Go, it's a different type. This gives an inescapable mismatch in the type systems. Or, the problem may be with macros, in which case it is likely another version of #9601. |
I think it's a combination of typedefs and macro defines, I tried chasing the maze a bit but it's hard to do by hand in particular when there are many conditional includes and defines. For now I solved the problem by converting [16]C.__u8 to a Go [16]byte right away and passing that to the function instead. It involves an extra copy, but in this case it's only 16 bytes so not a big deal. As a general (perhaps temporary) solution, would it be possible to add an experimental cgo directive to declare identical types, e.g. __u8 == u8 == unsigned char? |
I'm sorry, I'm personally not too interested in temporary solutions, and, more importantly, I'm not interested in adding any solution without some kind of test case. |
I'm attaching a test case, I think it's equivalent to what I'm seeing in my more complicated library. |
Thanks. When I build this on my Ubuntu Trusty system, using uuid version 2.20.1-5.1ub, I get this: ./main.go:37: cannot use cdev.uuid_le.b (type [16]C.__u8) as type [16]C.unsignedchar in argument to dump I can recreate this using this standalone test case:
In this cut down version, it's clear that the problem is the |
I'm using some 3rd party code that's supposed to run on multiple platforms, my example just tried to re-create what I think this 3rd party code is doing. The uuid library has its own __u8 definition that comes from linux/types.h I think. Not sure why this 3rd party code needs to |
Unfortunately I don't have a simple example for this one. I observed it while working with a .h file that was conditionally including all sorts of other .h files and having its own defines. The problem occurred with __u8 and u8 types. They were both eventually defined as "unsigned char" but cgo considered them different and even got confused in the following case:
[16]C.__u8
(checked with a Printf %#v)[16]C.__u8
as the only argument and passed it the value I got above:func uuidBytesToString(ub [16]C.__u8) string {...}
When trying to compile it I got this error message:
cannot use cHandle.id.b (type [16]C.__u8) as type [16]C.u8 in argument to uuidBytesToString
It thinks that the func signature is
[16]C.u8
instead of[16]C.__u8
, but even so these 2 types are eventuallyunsigned char
The text was updated successfully, but these errors were encountered: