golang/go

cmd/link: trying to get a C function pointer from Go breaks if the function is stdcall

Open

#47,992 opened on Aug 26, 2021

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Go (19,008 forks)batch import
NeedsInvestigationOS-Windowscompiler/runtimehelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

What version of Go are you using (go version)?

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

What did you do?

I was working over on github.com/keithcat1/gobass, adding a way for users to pass Go functions as a C callback. This involves creating a Go wrapper function which calls the user-provided callback. My problem is that BASS requires its callbacks to have the CALLBACK attribute. On windows this boils down to __stdcall. On Windows/386 I get a linker error when trying to get the pointer to a stdcall function from Go. I think this is because stdcall functions have their name changed from for example 'add' to '_add@8' and Go can't account for this. Here is an example: example.zip Get either a 32-bit MinGW toolchain from something like https://www.winlibs.com Extract or LLVM MinGW, cd into the example, set GOARCH=386 and go build. If using GCC you'll get a warning about stdcall fixups, and if using Clang the linking process will fail entirely. The way to get around this for me was to make functions that returned the C function pointers so that the C comppiler could do the right thing.

Contributor guide