go: runtime/cgo: gcc_libinit_windows.c:136:27: error: implicit declaration of function '_beginthread'

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

1.20.3

Does this issue reproduce with the latest release?

Yes

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

Windows 10 Intel x86-64

What did you do?

I tried to run a simple code to execute a function from a DLL file.

package main

import "C"

import (
	"fmt"
	"log"
	"runtime"
	"syscall"
	"unsafe"
)

const (
	SDL_INIT_TIMER          = 0x00000001
	SDL_INIT_AUDIO          = 0x00000010
	SDL_INIT_VIDEO          = 0x00000020
	SDL_INIT_JOYSTICK       = 0x00000200
	SDL_INIT_HAPTIC         = 0x00001000
	SDL_INIT_GAMECONTROLLER = 0x00002000
	SDL_INIT_EVENTS         = 0x00004000
	SDL_INIT_SENSOR         = 0x00008000
	SDL_INIT_NOPARACHUTE    = 0x00100000
	SDL_INIT_EVERYTHING     = SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_SENSOR
)

func main() {
	runtime.LockOSThread()

	err := x()
	if err != nil {
		log.Fatal(err)
	}
}

func x() (err error) {
	var sdl2 = syscall.NewLazyDLL("SDL2.dll")
	SDL_Init := sdl2.NewProc("SDL_Init")
	SDL_GetError := sdl2.NewProc("SDL_GetError")

	var ret uintptr
	ret, _, _ = SDL_Init.Call(SDL_INIT_EVERYTHING)
	fmt.Printf("Return: %d\n", ret)

	ret, _, _ = SDL_GetError.Call()
	fmt.Printf("Return: %d\n", ret)
	s := C.GoString(ret)
	fmt.Printf("Error: %s\n", s)
	C.free(unsafe.Pointer(ret))

	return nil
}

CGO is enabled, gcc is installed with Cygwin64 and has version 11.3.0-1. SDL is the latest version, 2.26.5.

What did you expect to see?

I expected not to see errors at compile time.

What did you see instead?

# runtime/cgo
gcc_libinit_windows.c: In function '_cgo_beginthread':
gcc_libinit_windows.c:136:27: error: implicit declaration of function '_beginthread'; did you mean '_cgo_beginthread'? [-Werror=implicit-function-declaration]
  136 |                 thandle = _beginthread(func, 0, arg);
      |                           ^~~~~~~~~~~~
      |                           _cgo_beginthread
cc1: all warnings being treated as errors

Compilation finished with exit code 1

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 36 (12 by maintainers)

Most upvoted comments

that happen because you installed gcc through cygwin64 so, try to install MinGW. download it from this link https://github.com/niXman/mingw-builds-binaries/releases make sure to choose a compatible version if you have Windows 64 choose mingw64 and so
then extract it into Windows partition in my case was C:\ partition then change the gcc path in environment variables in my case the path after the update was “C:\mingw64\bin” then restart vs code if it opens then try the command again.

if it needs to CG_ENABELED=1 then open powershell as admin and write this command “go env -w CGO_ENABLED=1”

删除 其他的gcc 环境 只是用tdm-gcc ,就可以编译成功

LLVM with its CLang compiler

I think we do support LLVM based C compiler for Windows. Have you tried that?

The LLVM project tries to unify compilers of different languages and different vendors

We have “gollvm” project that uses LLVM backend for Go https://go.googlesource.com/gollvm . You’re welcome to try that. It doesn’t support Windows yet, though. But that is beyond the scope of this issue. Thanks.