Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!