New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/cgo: link error when using pointers to static C functions #19836
Comments
Isn't it that C |
(In the example above, you can invoke |
Ok. I thought they are in different compilation units... Thanks. |
@bcmills Looking at the cgo intermediate outputs @cherrymui might be right, the Go code is not in the same compilation unit as the This is how ...
static void print_hello() { ... }
...
CGO_NO_SANITIZE_THREAD
void
_cgo_66eac9955d07_Cfunc_print_hello(void *v)
{
struct {
char unused;
} __attribute__((__packed__, __gcc_struct__)) *_cgo_a = v;
_cgo_tsan_acquire();
print_hello();
_cgo_tsan_release();
} And this is the go wrapper in //go:cgo_unsafe_args
func _Cfunc_f1() (r1 _Ctype_void) {
_cgo_runtime_cgocall(_cgo_66eac9955d07_Cfunc_f1, uintptr(unsafe.Pointer(&r1)))
...
} This does not happen when taking the address of a C function because the non-static trampoline is not used and |
Taking as background my last comment, this issue could be fixed by:
This approach has the disadvantage that the address retrieved in Go will be different from the address retrieved in C, but I can't think on any other easy way to solve this issue. |
If I try to pass a pointer to a static C function, I get a link error.
cgocallback/main.go:
The workaround is to use external linkage for functions used as function pointers, but in some cases that means I have to use more verbose names to avoid collisions.
(Possibly related to #19835.)
The text was updated successfully, but these errors were encountered: