Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 852
igbgotiz Avatar asked Jul 10 '14 04:07

igbgotiz


Video Answer


2 Answers

  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.

like image 68
David Grayson Avatar answered Oct 21 '22 06:10

David Grayson


  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.

like image 29
Martin Rosenau Avatar answered Oct 21 '22 08:10

Martin Rosenau