0

Running any simple "Hello, world!" that uses cgo on a (current as of today) stock cygwin generates an error:

$ go version
go version go1.16.5 windows/amd64

$ go test -run TestCamera
# runtime/cgo
gcc_libinit_windows.c: In function ‘x_cgo_sys_thread_create’:
gcc_libinit_windows.c:58:12: error: implicit declaration of function ‘_beginthre
ad’ [-Werror=implicit-function-declaration]
   58 |  thandle = _beginthread(func, 0, arg);
      |            ^~~~~~~~~~~~
cc1: all warnings being treated as errors

Is go not supported on cygwin for some reason? Or, how can I fix this?

example code: literally any cgo, for example https://github.com/kyleconroy/hello-cgo/blob/master/hello.go

BadZen
  • 4,083
  • 2
  • 25
  • 48
  • Literally any cgo. I added a link to an example "Hello World!" using cgo, but just writing it (it's obvious/trivial as any Hello World) should be faster than downloading that. If not, I don't see how it's even possible to answer this... (and, it's a question "does support ", not at all about a particular piece of code) – BadZen Jun 07 '21 at 03:59
  • No, Cygwin is not supported. – JimB Jun 07 '21 at 12:56
  • JimB - Can you point to some official document which describes which environments are supported under windows (or in general), and which are not? I could not find such a thing in the docs. – BadZen Jun 08 '21 at 01:03
  • Go supports both windows and Linux natively. Cygwin is not officially supported by the fact that there no declaration of support. It is a platform with a lot of quirks, and the go team has no interest or time to try and work around its bugs when native support is available. – JimB Jun 09 '21 at 00:21
  • But cgo depends on a C compiler, so it must have a C compiler. So some set of C compilers must be supported. It therefore depends on a C library, so some subset of C libraries must be supported. There's no such thing as "native" C compilation! I am asking which compilers/C libraries are supported. I can't imagine that no one has ever thought about that / written it down / told users... – BadZen Jun 10 '21 at 14:38
  • The error looks like the main problem is that go detects it's running under `windows/amd64` while you want it to use this fake gnu-linux environment, so it still may be the fact that Cygwin is not supported. You may be able to override things with `GOOS`, `CC`, or `CC_FOR_TARGET`, but you're just fighting the tooling. As for which c compiler is supported, `cgo` generally targets `gcc`, and `mingw-w64` is recommended here: https://github.com/golang/go/wiki/cgo#windows. – JimB Jun 10 '21 at 14:49
  • "(for instance, mingw-w64)" is not really sufficient. That describes a /family/ of toolchains, I want to know exactly which are supported. If you're not familiar with cygwin, it's two things: 1) a set of binaries / distribution of GNU software, and 2) a compatibility library which you can link against. The tools use (2), and one of the several toolchains that ship with Cygwin use (2) by default. mingw-w64 compilers are in Cygwin, don't do (2), and Go shouldn't care what runs the compilers. I still need a support matrix. Seperate question / not for comments, tho. – BadZen Jun 11 '21 at 15:54

2 Answers2

2

If I download these files [1], I can run these commands as expected:

PS C:\hello-cgo> go mod init hello
PS C:\hello-cgo> go build
PS C:\hello-cgo> .\hello.exe
Hello CGO!

However I should say that I am not using Cygwin, but MSYS2 [2]. Specifically, these packages:

mingw-w64-x86_64-gcc
mingw-w64-x86_64-binutils
mingw-w64-x86_64-crt-git
mingw-w64-x86_64-gmp
mingw-w64-x86_64-headers-git
mingw-w64-x86_64-libwinpthread-git
mingw-w64-x86_64-windows-default-manifest
mingw-w64-x86_64-winpthreads-git
mingw-w64-x86_64-zlib
mingw-w64-x86_64-zstd

I think if you use Pacman, you can just install the first one, and it will pull in the rest (I use my own MSYS2 package manager). I know this is not quite what you asked for, but maybe it will help you.

  1. https://github.com/kyleconroy/hello-cgo
  2. https://repo.msys2.org/distrib
Zombo
  • 1
  • 62
  • 391
  • 407
  • This isn't really an answer (because I'm asking about Cygwin), but was the hint I needed to at least find a workaround, because this target toolchain exists in cygwin as well. Things /seem/ to work if I set the CC environment variable to the compiler mingw-w64-x86_64-gcc.exe in the Cygwin shell. But it would be nice to know what Go's actual requirements and support matrix is! Perhaps this is another question... – BadZen Jun 08 '21 at 01:05
  • 2
    @BadZen as someone with extensive Cygwin experience (https://stackoverflow.com/search?q=user:1002260+[cygwin]) - I will say that Cygwin is an awful platform for programming. I would strongly suggest dumping it for MSYS2 or even https://github.com/mstorsjo/llvm-mingw - do yourself a favor – Zombo Jun 08 '21 at 01:33
  • Not an option, sadly :/ – BadZen Jun 10 '21 at 14:35
0

I have no problems running go on cygwin after installing mingw64-x86_64-gcc-core and export CC='D:\cygwin64\bin\x86_64-w64-mingw32-gcc.exe' as described on https://stackoverflow.com/a/43615612/2935741.

ce72
  • 128
  • 2
  • 4