Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
Calling C function pointer as callback from Go code

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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda