18

I've been trying to understand what exactly Cygwin and MinGW are and find it very confusing. If you want to program in C++ don't you just need a C++ compiler? From what I've read it seems like they strive to provide a Unix like operating system on Windows but I don't see the significance of this. I mean is there some reason that C++ can't natively be compiled on Windows? I also read that they contain libraries, is this because the C++ core language doesn't support many functions so very common libraries such as math.h come with it?

Celeritas
  • 12,953
  • 32
  • 95
  • 174
  • You don't need them to compile C++ on Windows. They just provide you with GCC. – Blender Apr 29 '13 at 07:32
  • @Blender so what you're saying is they allow for GCC to be run on Window and since GCC is so popular this is desirable? – Celeritas Apr 29 '13 at 07:34
  • 2
    GCC is just one of the many C/C++ compilers out there. – Blender Apr 29 '13 at 07:35
  • @Celeritas Cygwin is not required to use GCC on Windows, MinGW(-w64) is (to provide system headers and libraries). It is desirable to have GCC on Windows, because it has better C++11 language support (library is still lacking in view of MSVS2012), fortran, C99 (C11 upcoming) support, etc... All things MSVC is lacking and will never get (this has been true for about 10-15 years). – rubenvb Apr 29 '13 at 07:48
  • possible duplicate of [What is the difference between Cygwin and MinGW?](http://stackoverflow.com/questions/771756/what-is-the-difference-between-cygwin-and-mingw) – Cole Johnson Jan 30 '14 at 02:05

2 Answers2

12

This explanation is bound to be already written down elsewhere, but here goes anyway...

Cygwin is a POSIX compatible runtime built on top of the Win32 API. It provides a largely compliant POSIX C library, and around that, there is a sort of "Cygwin Distro" with package manager that allows you to install a ton of Unix programs ported to run under Cygwin. These include a Unix shell, GCC (for Cygwin), X, a bunch of cross-compilers, etc...

MinGW(-w64) are projects providing Free (as in Public Domain) Win32 headers and libraries. You can see this as a Free replacement for the headers and libraries in the Windows SDK (of old). These can be used in conjunction with GCC. Both projects and other third parties provide native GCC builds for Windows, which do not need Cygwin at all. These packages can be seen as a "full" (to the extent of completeness of MinGW(-w64)) replacement for the Windows SDK.

Note that Cygwin also provides Cygwin to Win32 cross-compilers: these run on the Cygwin platform but produce native Win32 executables.

Another player, MSYS, is a lightweight fork of Cygwin, which provides only the minimum amount of tools to run autotools build scripts on Windows. MSYS must be used in conjunction with the native MinGW(-w64) tools, it is not Cygwin and you cannot easily extend MSYS.

So "MinGW(-w64)" often refers to the whole Free Windows GNU Toolchain (native or a cross-compiler running on top of Cygwin), although strictly speaking it is only the headers and libraries.

To use GCC's C++ compiler, I suggest you use either my packages (to create 32-bit and 64-bit executables) or mingw-builds. Alternatively, you can install Cygwin and use its setup.exe to install the MinGW-w64 cross-compiler and use it from there, but if you do not need near-perfect Unix emulation for build scripts, I'd suggest against it.

EDIT You may or may not be aware that there is another alternative, the old Interix, which sidesteps the Win32 subsystem and builds directly on top of the NT kernel. This is provided as Windows Services for Unix or Subsystem for Unix Applications (available for Windows 7 and Windows 8 and older versions of Windows). This solution is available for Windows Pro/Ultimate version customers and provides you with the closest to native Unix support you are going to get. Heck, you can even use Visual Studio to debug your Unix software :)

rubenvb
  • 69,525
  • 30
  • 173
  • 306
  • Could you please explain what it means to be "a POSIX compatible runtime"? I've red the Wikipedia article on POSIX but it's still not clear to me. – Celeritas Apr 29 '13 at 07:39
  • POSIX defines an OS interface consisting of a bunch of C functions. These consist of the C99 (C11?) standard library and every additional function detailed [here](http://pubs.opengroup.org/onlinepubs/9699919799/). Examples are `fork`, `access`, all of pthreads (although MinGW(-w64) have libraries wrapping that part of the Win32 API: pthreads-win32 and winpthreads), etc... POSIX also defines a Shell and utilities, which is provided by Cygwin (and in a less perfect manner, MSYS). This is all stuff that needs emulation, and thus is slower in execution than coding against the native Win32 API. – rubenvb Apr 29 '13 at 07:46
  • So basically C++ contains some functions, such as `fork()` which exist on Unix operating systems but not Windows operating systems? Since these are technically part of the language they still must be supported regardless of the operating system so a point of Cygwin/MinGW is Windows knows what to do with a C++ program that executes the code `fork()`? It seems strange that and OS dependant functions are made part of the core C++ language, is this because C/C++ was originally designed for Unix? For example wouldn’t it make more sense to need to write `system(fork())`? – Celeritas Apr 29 '13 at 08:22
  • 1
    @Celeritas `fork` is not part of the C standard library. It's a POSIX function. MSVC has a C89 standard library, MinGW(-w64) has a (near-)C99 standard library. All this is strictly C, not C++. C++11 does reference C99, so in principle all C99 functionality is available in C++, but this does certainly not include POSIX stuff like `fork` and pthreads. – rubenvb Apr 29 '13 at 09:06
12

NOTE: this answer does not pretend to be exhaustive, it just want to present a simple understanding of a concept, out of ideological definitions. The use of imprecise language is intentional. So OS gurus, please don't expect perfect definitions.

The difference resides in the roots of POSIX / UNIX and windows.

POSIX is a programming C interface for operating systems (among other things, like Shell and utilities).

Unix and later Linux implements such interfaces in themselves, Windows natively - doesn't. It has its own way to manage operating system entities (Win32). Mac OS also doesn't.

GCC is a compiler suite common on systems that offer a POSIX interface.

To use it on Windows, you have -at this point- two alternatives:

  • Add to windows a library (code and headers) that add the POSIX interface to windows (translating the POSIX calls into Windows calls) and port all the GNU programs (Linux based) by compiling them to use that library: this is what Cygwin does.

  • Rewrite POSIX programs so that they include inside themselves everything related to POSIX-Windows mapping, producing native Windows executable: this is what MinGW and MSYS are about.

Bot methods have advantages and drawbacks. In short, in Cygwin, if something compiles under POSIX, it compiles under Cygwin (at least in theory). But the result is a unix-looking program running under windows.

In MinGW, you can write native windows programs (by calling the native Win32 C interface), but you cannot compile everything uses POSIX functionality not present in windows. The most evident case is the missing of the standard <thread> interface of MinGW and a limited <locale> support.

Emilio Garavaglia
  • 18,858
  • 2
  • 41
  • 60
  • Thanks I am looking for a simplification. "POSIX is a programming C interface for operating systems" what is meant by *programing interface* you mean the way a program interacts with the OS? – Celeritas Apr 29 '13 at 08:42
  • 4
    @Celeritas: exactly. POSIX presents the OS to a hosted program as a set of callable C functions. Note that Windows does the same, but with another set of functions. – Emilio Garavaglia Apr 29 '13 at 08:46
  • GCC does not really expect a POSIX interface: it can compile on/for a number of different systems (including Windows), and not all of them have complete POSIX C interfaces. And note that using MinGW-w64's winpthreads, libstdc++ can provide ``, ``, and `` functionality, although it might still have some quirks. – rubenvb Apr 29 '13 at 09:49
  • @rubenvb: just in case you have problem with small fonts: **NOTE**: this answer does not pretend to be exhaustive, it just want to present a simple understandment of a concept, out of ideological a religious definitions. The use of imprecise language is intentional. So OS gurus,m please go away from here. – Emilio Garavaglia Apr 29 '13 at 09:53
  • 1
    You can be imprecise without being incorrect. I subtletized some subtleties in your answer for you. – rubenvb Apr 29 '13 at 09:54
  • When you say the Mac OS doesn't support POSIX, you mean the pre-X versions, right? The Mac OS X line is a UNIX variant and does support POSIX (I think). – CTMacUser Apr 29 '13 at 09:56
  • 1
    @CTMacUser: yes. It was just another example of non POSIX, non Win32 system. It will be always more difficult to find non-posix, sice poix compatibility is increasing. Even Win6 and Win7 are now more "posix compliant" than the original Win32, although some missing functionality are still there. – Emilio Garavaglia Apr 29 '13 at 12:32
  • 2
    @rubenvb: Thanks for the edits. About incorrectness, if being correct requires so many cases and sub-cases, there is the risk to distract the OP from the real intent of an "high level" answer, thus making it useless for its own scope. We can write an entire book on this subjects, but that's far from the OP intent who just wanted a simple and effective way to identify the two things. Your position is legitimate, but may easily run off-scope. – Emilio Garavaglia Apr 29 '13 at 12:41
  • @rubenvb I noticed an edit you made said GCC is not just a compiler but a compiler suite. What is a compiler suite, what more does it have than just a compiler? This goes back to my question about why is it not enough just to have a compiler. Thanks, I think I'm getting it. – Celeritas Apr 29 '13 at 17:49
  • Celeritas: GCC stands for the **G**NU **C**ompiler **C**ollection and provides compilers and runtimes for (obj-)c(++), Fortran, Go, Java, and Ada. You need more than this though: you most likely need a linker and some other object file tools. For the GNU toolchain, these are part of GNU Binutils. The third part you might use is a debugger, which for the GNU toolchain would be GDB. I call all this together (Compiler+system and headers and libraries+linker+debugger) a "toolchain". In principle, each component can be replaced by another variant if the output and input are compatible. – rubenvb Apr 29 '13 at 18:57