4

I try to call a C function pointer as a callback from a Go function but get a linker error I don't understand.

package main

/*
typedef void (*int_int)(int, int);
void bridge_int_int(int a, int b, int_int f){
    f(a, b);
}
*/
import "C"

//export IntIntCallback
func IntIntCallback(a, b C.int, cb C.int_int) {
    C.bridge_int_int(a, b, cb)
}

func main() {
    // Noop, required to make CGO happy: `-buildmode=c-*` requires exactly one
    // main package, which in turn needs a `main` function`.
}

The error is:

c:/mingw32/bin/../lib/gcc/i686-w64-mingw32/11.2.0/../../../../i686-w64-mingw32/bin/ld.exe: $WORK\b001\_x002.o: in function `bridge_int_int':
./test.go:6: multiple definition of `bridge_int_int'; $WORK\b001\_x001.o:/tmp/test.go:6: first defined here
collect2.exe: error: ld returned 1 exit status

I don't understand where this multiple definition problem comes from.

1 Answers1

2

Problem solved. See: https://pkg.go.dev/cmd/cgo#hdr-C_references_to_Go

Using //export in a file places a restriction on the preamble: since it is copied into two different C output files, it must not contain any definitions, only declarations. If a file contains both definitions and declarations, then the two output files will produce duplicate symbols and the linker will fail. To avoid this, definitions must be placed in preambles in other files, or in C source files.

Moving the following code into a separate .go file:

/*
typedef void (*int_int)(int, int);
void bridge_int_int(int a, int b, int_int f){
    f(a, b);
}
*/
import "C"

and changing it to

/*
typedef void (*int_int)(int, int);
extern bridge_int_int(int a, int b, int_int f);
}
*/
import "C"
...

in the original file solved the problem