On Tuesday, June 14, 2016 at 3:22:54 PM UTC-7, Ian Lance Taylor wrote: > > On Tue, Jun 14, 2016 at 3:15 PM, Dragos Harabor <dh...@harabor.com > <javascript:>> wrote: > > Is the following code supposed to work? Not sure whether I'm doing > something > > wrong or cgo just doesn't support this style of #defines. I reduced it > down > > to a simple test case, but the actual problem I ran into came from some > 3rd > > party .h files that unfortunately I have no control over. > > > > package main > > > > /* > > #include <stdio.h> > > > > #define VERSION_A "1.2.3" > > #define VERSION_B VERSION_A > > > > void f() { > > printf("VERSION_A: %s, VERSION_B: %s\n", VERSION_A, VERSION_B); > > } > > */ > > import "C" > > > > import "fmt" > > > > func main() { > > C.f() > > > > fmt.Println("VERSION_A:", C.VERSION_A) > > > > // This does not compile > > fmt.Println("VERSION_B:", C.VERSION_B) > > } > > > > > > > > The error I get: > > > > % go run cgodefine.go > > # command-line-arguments > > > /tmp/go-build088092139/command-line-arguments/_obj/_cgo_main.o:(.data.rel+0x0): > > > > undefined reference to `VERSION_A' > > collect2: error: ld returned 1 exit status > > cgo unfortunately does not do a great job with macros. This is a bug. > The workaround is to write code like > > /* > #include "hdr.h" > > const char *versiona = VERSION_A; > */ > import "C" > > func F() { > fmt.Println(C.GoString(C.versiona)) > } > > Ian >
Thank you Ian. Are there any plans to fix it? This seems like a simple case, though I realize macros can be a lot more complicated. Unfortunately I cannot write code as your example above. What I'm really after is the go tool cgo -godefs form to generate some Go structs out of some C header files that I will then use to make IOCTL calls to a hardware driver. The cgo tool in my case will just be used as a Go code generator. Maybe I'm abusing it, but it seems to work except for quirks like the above **_Cvar_VERSION_B* Any link to the issue tracking this? I'd like to subscribe to it for updates. Thanks. Dragos -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.