Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
cmd/cgo: cgo gets confused by equivalent #defined types #13636
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:
When trying to compile it I got this error message:
It thinks that the func signature is
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 C.__u8 to a Go 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?
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 C.__u8) as type 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