On Tue, Jun 14, 2016 at 3:15 PM, Dragos Harabor <dh...@harabor.com> 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

-- 
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.

Reply via email to