From a technical POV, what is it that MinGW does that makes gcc possible on windows?
Asked Answered
W

2

8

Several questions:

  1. Is it correct to understand that MinGW is essentially taking the gcc source and compiling it into an executable for windows, i.e gcc.exe?

  2. MinGW wiki says "MinGW on the other hand, provides functions supplied by the Win32 API.". This confuses me. Doesn't a Windows installation by default already provide the Win32 API headers that exposes the Win32 API, which would make MinGW providing it again redundant? Are the headers MinGW provides different to the ones that come with a Windows installation?

  3. When I have an object file compiled by MinGW gcc, can I just link it to the C runtime library on windows? I'd think not because the object file generated by MinGW gcc may not be compatible(like uses the same calling conventions) as the C runtime library on windows.

Wyant answered 10/7, 2014 at 4:13 Comment(6)
It's more like they provide Linux-like wrappers around Win32. The headers, then, would need to be more like Linux headers, while the implementations would actually call into the Win32 API.Mayweed
(1) Yes. (2) MinGW provides only the functions supplied by the Win32 API, as opposed to Cygwin, which provides a Unix-like emulation layer on top of Windows. (3) MinGW supports the calling conventions used by the Windows built-in C runtime, so you can link to it; note, however, that Microsoft don't support third-party use of the built-in runtime, so there's no guarantee that it will work reliably (though in practice it mostly does).Fatwitted
Q: Doesn't a Windows installation by default already provide the Win32 API headers. A: No. The windows headers and libraries are installed separately. With the Windows SDK. Or with a Windows compiler like MSVS ... or MinGW.Raviv
WRT 3, there is no standard "C runtime library" on Windows. The MSVC runtime is installed separately from the operating system!Overskirt
@duskwulf: MinGW uses a "C runtime library" MSVCRT.DLL which is distributed with recent versions of Windows as an OS component.Refund
Yeah, Ben Voigt is right, says so on the front page of mingw.orgDietetic
P
4
  1. GCC is a multi-platform compiler so there is a Linux version, a MacOS version, a Windows version of this compiler. The "MinGW GCC" is one of at least two Windows versions existing. The "MinGW system" is nothing but a collection of the Windows versions of some GNU tools.

  2. I just read the MinGW Wiki entry and the "supplied by the Win32 API" seems to clarify the difference between "Cygwin" and "MinGW" - not between "MinGW" and the Microsoft C compiler:

    For many GNU tools there are two different versions available for Windows: "Cygwin" and "MinGW".

    "Cygwin" uses a special emulation environment to emulate a Unix-like file system. A special library will be linked to the programs where functions like "fopen" will convert file names in the form "/home/mydir/myfile.txt" to file names like "c:\programs\cygwin\home\mydir\myfile.txt".

    Using the "Cygwin" compiler both the "gcc" command line and the command lines of the programs created (more exact: linked) by it require Unix-like file names.

    The "MinGW" tools however behave like other Windows programs and use the normal Windows libraries where functions like "fopen" expect "normal" Windows-like file names like "c:\somedir\somefile". Programs built by the "MinGW" GCC compiler behave like programs built by the Microsoft compiler.

    Unlike Linux Windows does NOT come with any header files but they come with the Win32 API that has to be downloaded (> 1GiB) from Microsoft. MinGW and Cygwin provide some own header files that are nearly compatible with the Microsoft ones so it is not necessary to download the Win32 API.

  3. Most development tools in Windows use the same object and static library file format. (The "Watcom" compiler is one of the few exceptions.) This means you can mix object and static library files compiled with different compilers. (The format of .lib/.a stub libraries used for dynamic linking against DLLs differs between gcc and Microsoft so you cannot mix them!)

About the comment to the other answer:

  • MinGW typically links to "msvcrt.dll" which comes with Windows. This file contains standard C functions like "printf()".
  • Microsoft Visual C++ sometimes links to "msvcrt.dll", sometimes to some DLLs like "msvcr100.dll". "msvcr100.dll" also contains standard C functions however some of them have enhanced functionality (e.g. Unicode...). "msvcr100.dll" has to be post-installed because it does not come with Windows.
  • Cygwin links to files like "cygwin1.dll" containing the Cygwin variant of the standard C functions (that differ in file name handling). Needless to say that this file does not come with Windows but has to be post-installed.

Unlike "libc" in Linux all of these DLLs do not directly call the operating system but they call "kernel32.dll" which contains lower-level functions (like "WriteFile()") that will call the operating system.

Peppi answered 10/7, 2014 at 9:4 Comment(1)
Note that Microsoft Visual C++ hasn't used msvcrt.dll since VS6, which is long out of support. All current versions of VS ship with their own C runtimes, and don't support the use of msvcrt.dll. (That doesn't necessarily mean you can't do it, it just isn't supported.) MinGW's use of msvcrt.dll isn't supported either AFAIK, though in practice it seems to mostly work.Fatwitted
D
2
  1. What do you mean by "gcc source"? When you say "gcc source" you might mean "the source code of the GCC compiler". MinGW is a windows version of GCC, so it takes any C source code and produces executables, and it is GCC so it has the features of that compiler.

  2. I think the header files provided by Microsoft (e.g. windows.h and winusb.h) are not actually compatible with GCC. The MinGW project includes header files that are compatible with GCC so you can call Windows functions like ReadFile from your program. The last time I checked, MinGW only had some of the Microsoft header files; it was missing winusb.h.

  3. Why do you want to link with a Windows runtime library? I know that cross-compiler interop is possible because I once wrote a DLL with the Microsoft C compiler and called it from a MinGW (Qt) program.

Dietetic answered 10/7, 2014 at 4:22 Comment(3)
1. I meant the source code of GCC, which when compiled gives gcc.exe. 2. Why aren't they compatible with GCC? Are they non-conformant C code that only the VC compiler can consume? If so, then that'd make it an untrivial task to rewrite the windows api headers so they become feedable to gcc.Wyant
3. Because the windows runtime library provides printf, process and stuff which are essential to any non-trivial program. Where else am I gonna get printf?Wyant
@igbgotiz: the headers provided with the Windows SDK support a variety of compilers, but my understanding is that MinGW is not one of them. These headers have to be able to declare types of a specific size (which requires knowledge of the compiler) and also have to be able to declare the calling conventions for a function (which requires compiler-specific extensions). You can compare the MinGW headers to the MS headers yourself if you are curious as to the exact differences.Fatwitted

© 2022 - 2024 — McMap. All rights reserved.