c++mingwcross-compilingmsys2

What decides whether a binary compiled with MinGW would have access to Win32 API or POSIX APIs?


I am recently learning about MinGW and MSYS2. My understanding is that MinGW is a Windows port of GCC that produces binaries which run natively in Windows (like MSVC), and MSYS2 is something that gives the application running in it (?) access to POSIX APIs and libraries, but the binary still has to be compiled for Windows.

But what I don't understand is what decides whether the application will have access to POSIX APIs or not? Is it that the MinGW offered on its own website creates binaries that only have access to Win32 API, while the version of MinGW bundled with MSYS2 produces binaries that only have access to POSIX APIs (or both?)? Or is it that if you run MinGW from Windows (Command Prompt/PowerShell), then the resulting binary will only have access to Win32API, while running MinGW from inside MSYS2 Bash shell will cause the resulting binary to have access to POSIX APIs? Or is the resulting binary the same either way, and whether it'll have access to Win32 API or POSIX APIs depends on whether you run the binary from Command Prompt/PowerShell or MSYS2 Bash?

I have read this question which is similiar to what I want to ask, but the answer didn't help much as the answerer seemed to misunderstand and thought by "POSIX layer" the asker meant POSIX command-line utilities provided by MSYS2 and not the POSIX headers/API. What further confused me is that I found this question where the asker used MingGW in MSYS2 to compile a C program and the resulting binary requires msys-2.0.dll. Does this mean that binary would (only?) have access to POSIX APIs, and is this because he called MinGW from within MSYS2 instead of calling it directly (and calling it directly would have caused the resulting binary to not require msys-2.0.dll and only have access to Win32 API?)? I also found this question whose answer is also an explanation of MinGW & MSYS2/Clang, but the answer says:

As a simplification, it's like this:

  • Compile something in Cygwin and you are compiling it for Cygwin.
  • Compile something in MinGW and you are compiling it for Windows.

...

...And I don't really understand what the answerer means by compiling in Cygwin, isn't Cygwin a Linux environment emulator like MSYS2? How can you compile a program in it? Does he/she mean calling MinGW from inside Cygwin's Bash shell?

Sorry if I'm making a big misunderstanding somewhere, I'm just very confused about everything related to MSYS2 and MinGW.


Solution

  • The terminology is confusing here.

    MSYS2 is a software distribution project. It distributes several different compiler flavors, and packages built with each flavor (each flavor + packages built with it is called an "environment").

    One of the environments is "MSYS"1, and it is a Cygwin fork. It is not MinGW. The rest of compiler flavors in there are MinGW.

    You get access to POSIX API in your program if you build it with MSYS- (aka Cygwin-) -flavored compiler.

    this question where the asker used MingGW in MSYS2 to compile a C program and the resulting binary requires msys-2.0.dll

    Therefore they didn't in fact use "mingw", they used the MSYS-flavored gcc (aka /usr/bin/gcc, as opposed to, say, /mingw64/bin/gcc).

    is this because he called MinGW from within MSYS2 instead of calling it directly

    Doesn't matter. It only matters which gcc.exe you're running.


    1 Not to be confused with the other "MSYS" (which some now retroactively call "MSYS1"), which was a minimal set of Linux-like utilities distributed with the original MinGW. Those (both MSYS1 and the original MinGW) are mostly irrelevant today, most people use the newer MinGW-w64 (which is also what MSYS2 uses too), and -w64 is often omitted for brevity.