39

I'm not a C++ developer, but I've always been interested in compilers, and I'm interested in tinkering with some of the GCC stuff (particularly LLVM).

On Windows, GCC requires a POSIX-emulation layer (cygwin or MinGW) to run correctly.

Why is that?

I use lots of other software, written in C++ and cross-compiled for different platforms (Subversion, Firefox, Apache, MySQL), and none of them require cygwin or MinGW.

My understanding about C++ best-practice programming is that you can write reasonably platform-neutral code and deal with all the differences during the compilation process.

So what's the deal with GCC? Why can't it run natively on Windows?


EDIT:

Okay, the two replies so far say, basically, "GCC uses the posix layer because it uses the posix headers".

But that doesn't really answer the question.

Let's say I already have a set of headers for my favorite standard library. Why would I still need the posix headers?

Does GCC require cygwin/mingw to actually RUN?

Or does it only need the emulation layer for headers and libraries? If so, why can't I just give it a "lib" directory with the required resources?


EDIT AGAIN:

Okay, I'll try again to clarify the question...

I also write code in the D Programming Language. The official compiler is named "dmd" and there are official compiler binaries for both Windows and linux.

The Windows version doesn't require any kind of POSIX emulation. And the Linux version doesn't require any kind of Win32 emulation. If the compiler has assumptions about its environment, it hides those assumptions pretty well.

Of course, I have to tell the compiler where to find the standard library and where to find libraries to statically or dynamically link against.

GCC, by contrast, insists on pretending it's operating within a posix environment, and it asks ME to humor those assumptions by setting up an emulation layer.

But what, exactly, within GCC relies on that layer? Is it just looking for stdlib headers, and it assumes it'll find those headers within "/usr/lib"?

If that's the case, shouldn't I just be able to tell it to look in "C:/gcc/lib" to find those header files?

Or does GCC itself rely on the POSIX libraries to access the file system (and to do other low-level stuff)? If that's the case, then I wonder why they don't just statically link with their favorite windows POSIX libraries. Why require the user to set up the dependencies, when they could build those dependencies right into the application?

benjismith
  • 16,559
  • 9
  • 57
  • 80
  • There are several *correct* answers to chose from now. MinGW does not require cygwin1.dll. If you want to know what the core gcc team doesn't support Microsoft Windows, you should ask that. – Jon 'links in bio' Ericson Dec 10 '08 at 01:22
  • Not to mention that it makes as much sense as this question: Why do you need WINE or something to run Visual Studio and VC++ on Linux? – David Thornley Feb 01 '10 at 15:12

11 Answers11

37

Actually, the question premise is wrong: MinGW GCC does NOT require Cygwin.

You will see you don't need Cygwin at all. It runs natively on Windows (32-bit, at least). Both the toolchain and the produced binaries are independent of Cygwin.

The MinGW compilers available in Cygwin are different: they are built on the Cygwin platform, to generate code which does not depend on the Cygwin runtime. The compilers themselves do depend on Cygwin in that case. But that's because you installed them from Cygwin.

Greg Mattes
  • 33,090
  • 15
  • 73
  • 105
David Cournapeau
  • 78,318
  • 8
  • 63
  • 70
29

The Cygwin version of GCC requires Cygwin to be installed, for programs it compiles.

The MinGW version does not require anything after compiling, other than a working copy of Windows.

You can't really mix the Cygwin environment, and the MinGW compilers together, because Cygwin changes the paths of the precompiled libraries.

If you need a bash style shell, but don't want to use Cygwin, I would recommend MSYS.

Cygwin in contrast to MinGW

copied from MinGW Wiki

Cygwin applications by principle are not considered a "Native Win32 application" because it relies on the Cygwin® POSIX Emulation DLL or cygwin1.dll for Posix functions and does not use win32 functions directly. MinGW on the other hand, provides functions supplied by the Win32 API. While porting applications under MinGW, functions not native to Win32 such as fork(), mmap() or ioctl() will need to be reimplemented into Win32 equivalents for the application to function properly.
Simon
  • 14,631
  • 4
  • 41
  • 101
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
7

I try to make my programs under Windows behave like a good Windows citizen, and under Linux like a good Linux citizen.

Walter Bright
  • 4,277
  • 1
  • 23
  • 28
  • 5
    As always, Walter, you're the man! Thanks for making a very easy-to-use compiler, on both platforms! NOTE: For others reading this comment, Walter is the author of the "dmd" compiler for the D Programming Language (which I referenced as a great example of cross-platform coding in my original post). – benjismith Nov 04 '08 at 19:25
  • MingW largely achieves this goal. – Brad Gilbert Dec 14 '08 at 18:10
  • @Brad, but without the help of the core GCC developers. – RBerteig Apr 19 '09 at 04:25
7

POSIX (Portable Operating System Interface) "is an evolving, growing document that is being produced by IEEE and standardized by ANSI and ISO. The goal of POSIX is the source-code portability of application" [1].

In practical terms, the goal is defined as the ability to write one source implementation and have it run on different (POSIX-compliant) systems with only recompilation.

GCC is a compiler capable of delivering that promise and as such, it needs a layer of code that brings a machine "up to" POSIX standards.

That is the core of the answer to your question.

To help see what I mean, I'll offer you this exercise:

  • Write a program with no OS-specific #ifdefs that takes as input from the user some directory path and writes to stdout a listing of its contents (one level).

I think you will find that it is very difficult to write code that uses only the native WIN32 API that compiles on any UNIX or LINUX system

It will be only slightly less difficult to write code that uses POSIX API - as you can on any LINUX box - and have it compile under Windows (DevStudio2005 has a surprising number of POSIX-compliant headers now...you might be able to get close).

Take you LINUX program from above and now compile it under GCC running under Cygwin or MinGW. I'll bet it compiles and runs.

How did GCC perform that bit of magic? The POSIX headers and implementations underlying them provided by Cygwin or MinGW.

Does GCC's reliance on Cygwin/MinGW under Windows make more sense now?

  1. POSIX.4: Programming for the Real World, Bill O. Gallmeister, O'Reilly & Associates, Inc., pg 2
AJ S.
  • 400
  • 1
  • 3
  • 7
3

Windows does not offer a standard POSIX library so cygwin provides one (cygwin1.dll). The gcc packages that comes with cygwin uses it.

mingw, on the other hand, does not necessarely provide a POSIX layer. The mingw installation that I use, for example, does not even have a pthread library.

Should I need it I would have to install it. Mingw-gcc produces Win32 native code (and in fact relies on MSVCRT.DLL).

EDIT: reading your edit I'm no longer sure if you're asking why gcc itself needs mingw/cygwin libraries or if the programs compiled with gcc on Win require those libraries

Remo.D
  • 16,122
  • 6
  • 43
  • 74
  • My question could be rephrased like this: Why, as a user of GCC, should I be required to first install a POSIX emulation layer onto my machine? – benjismith Nov 04 '08 at 19:27
  • 1
    @benjismith: Or, why, as a user of Visual Studio, should I be required to first install a Windows emulation layer onto my Linux machine? Both questions seem to me to make about the same amount of sense. – David Thornley Feb 01 '10 at 15:08
  • @David, you don't have the source code for Visual Studio, so you have to provide whatever the binaries require. You have the source code of Gcc, so it should be able to be built without the requirement of an emulation layer. Which I assume is what MinGW does. – Dominique McDonnell Feb 21 '12 at 23:25
3

Why? Because when GCC was created, Windows 32 bit even hadn't exits...

To be more correct --- it was developed for UNIX/Posix OS. Later it was ported to windows.

Windows is not POSIX compient system. It even does not provide very basic functionality. Try to find readdir or stat under Windows compiler? And this is so extreamly basic functionality that you need to write compiler!

Just to be clear, GCC compiled programs usually required only one mingw32.dll to add missing functionality to be able to run.

So... You ask why GCC requires some POSIX layer? Because Windows OS is not POSIX operating system.

Artyom
  • 31,019
  • 21
  • 127
  • 215
  • 2
    Almost totally wrong. Windows is (minimally) POSIX compliant, it's been issued a certification of compliance. And `readdir` and `stat` are among the functions *least* likely to be used by a compiler. – Ben Voigt Nov 07 '10 at 02:47
  • @Ben Voigt - Windows (mininal) POSIX compatibility is a joke. Its ** minimality** makes it totally useless and does not help porting software. And operations of file system for C or C++ compiler are very critical and important. – Artyom Nov 07 '10 at 05:28
  • @Artoym: The truth is, the reason some apps compile across a broad spectrum of Unix-like OSes but not Win32 is because they require the SUS (Single Unix Specification) API, not just the POSIX API. And the only file system operations needed for a compiler are open file, read, write, close, and maybe seek, furthermore those ARE part of POSIX and fully supported on Windows. No need for directory listings, no need to check file timestamps (the shell takes care of globbing, the `make` utility takes care of checking for changed dependencies, neither of these are part of the compiler). – Ben Voigt Nov 07 '10 at 05:39
  • There is the native [POSIX subsystem in Windows](https://en.wikipedia.org/wiki/Microsoft_POSIX_subsystem) (which was replaced by [Windows Services for UNIX](https://en.wikipedia.org/wiki/Windows_Services_for_UNIX) in newer Windows versions) http://superuser.com/q/19834/241386 http://superuser.com/a/881813/241386 – phuclv Jul 20 '15 at 09:07
3

I'm not a C++ developer, but I've always been interested in compilers, and I'm interested in tinkering with some of the GCC stuff (particularly LLVM)

Note that LLVM and GCC are not related. LLVM is largely the result of the research done by Chris Lattner (http://llvm.org/developers.cgi) about modern optimization. His papers are available on http://llvm.org. Nowadays, it is heavily sponsored by Apple. GCC's C/C++/Obj-C frontend is used for llvm-gcc, which emits LLVM machine code (and after a ton of optimizations in llvm, a final executable comes out); llvm-gcc is a kind of hack to couple some ready C/C++/Obj-C-frontend to LLVM.

Note anyways that the LLVM crew also builds an own, complete C/C++/Obj-C compiler, called clang. It's C implementation is near complete, C++ support is getting better and better, dunno about Obj-C though.

So, if someone says "compiler llvm", he really either means llvm-gcc, or clang. LLVM itself is just the Low Level Virtual Machine, with only a handful of instructions in Static Single Assignment form (approx. 32 instructions, afair), but a megaton of optimization passes upon thy syntax tree.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
2

Much of that software that is compiled for different platforms is compiled... in MinGW. The only difference with gcc is that it is a compiler itself, which means it needs all the headers that normally get compiled in with the program, and normally which one does not need to run the resulting program.

Dark Shikari
  • 7,941
  • 4
  • 26
  • 38
0

Because the people behing GCC hate Windows with a passion (read Stallman some day). So when porting GCC to Windows, they do their best to pretend it's just another Unix.

That, and they probably don't want to spend time on removing POSIX dependencies from the code.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 3
    Rather than assuming hatred, consider that a lot of folks behind GCC are not GNU folks. Two known, explicitly not Unix centric companies are google and Oracle. Rather than assuming hatred, consider that GCC *is* a compiler for Unix platforms. MinGW is "just" a port to one of the few non-unix platforms on the world. *Rather that demaning proofs, bring your own proofs in the first place.* – Sebastian Mach Dec 01 '10 at 14:18
  • 1
    And btw, most Open Source and Free Software people I know (including myself) don't hate windows (the loudest != the majority). We have no feelings for it, at max we have feelings for its users. – Sebastian Mach Dec 01 '10 at 14:21
  • Well, the loudest ones were allowed to be the voice of the movement so far. As for RMS, you don't have to Google much to find an invective against proprietary software (Windows included) - try this one on, for example: http://www.fsf.org/blogs/rms/can-we-rescue-olpc-from-windows – Seva Alekseyev Dec 01 '10 at 18:33
  • 1
    And yes, it was RMS who started GCC. AFAIK he's not involved now. – Seva Alekseyev Dec 01 '10 at 18:34
  • 1
    RMS is not the one voice of the one movement, because there is simply not the one movement. And whether or not RMS started GCC is irrelevant to your bullshit post. You don't know the FLOSS-[meta-]movement, otherwise you'd know that not even RMS is undisputed among the FLOSS-folks. Also, I wrote **most ... don't hate** it, which is absolutely different from **all**. Any good programmer should better know the difference between none, some, many, most, all, <, <=, ==, !=, =>, >, and he should be precise on that. – Sebastian Mach Jun 08 '11 at 12:23
0

The MinGW-w64 port of gcc creates native code for 32- and 64-bit Windows without any additional dependencies. See for example http://mstenberg.com/blog/2010/06/13/gcc-for-windows/ for a quick getting-started guide.

thecoshman
  • 8,394
  • 8
  • 55
  • 77
0

You have an option when building gcc to specify enable-threads options other than posix, if you don't want to support pthreads or OpenMP. Needless to say, those aren't as well tested. There is some 3rd party closed source support for OpenMP with Windows threads, but its use with gcc appears to violate the license. As the Windows pthreads library is a higher level interface to Windows threads functionality, it's perhaps not surprising when it doesn't perform as well or encounters Microsoft's rejection of affinity support. Only recently has Microsoft begun to tolerate gcc on Windows. There was a time when they actually said they would not work on bugs reported by gcc users, even if they could be reproduced with exclusively Microsoft tools.

tim18
  • 580
  • 1
  • 4
  • 8