Search code examples
c++windowsmingwlibstdc++msvcrt

How did MinGW implement C++ Library support?


According to my observation, MinGW use MSVCRT for C, and libstdc++ for C++.

If so, how could they work together? And, why not uniform C and C++ support, no matter MSVCRT + MSVCPRT or glib + libstdc++.

I think the mixin between MSVCRT and libstdc++ sounds horrible. So why did MinGW still choose this?

Links:


Below is my observation, just skip it if you could answer the question.

In order to compile code for native Windows(using only the Win32 API),
MinGW use MSVCRT as the underlying C runtime library(provide Win32 API),
and wirte a bridge layer from scratch in order to connect standard C calls and Win32 API calls.

I check C header files in MinGW, stdio.h for example, it has a banner like this.

/**
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is part of the mingw-w64 runtime package.
 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
 */

And inside the file, you will find a lot of _CRTIMP, which means actually it would convert to a Win32 API calls.

But for C++ parts, it is quite wired.
It seems that MinGW use libstdc++ to implement C++ support.

I check the C++ header files like iostream, it has a banner like this

// Standard iostream objects -*- C++ -*-

// Copyright (C) 1997-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

Of course no more _CRTIMP or any MS style symbol.
So, MinGW has make libstdc++ works based on MSVCRT!


Solution

  • Here is my understanding, please fix me.

    C Library is means a lot more than C++ Library.

    C Library is the bridge from platform dependent system calls to platform independent C calls.

    C++ Librarys starts from platform independent C calls, then add object-oriented features and make it much easier to use.

    So the reason why not use glibc is much more than the GPL license issue, it is because C Library needs to make system calls and communicate with OS. So in most case, it comes along with the OS, and will be the only C Library available on the platform.

    Therefore, because C++ library is based on C library, so it is platform independent. So just use the code from libstdc++ and spontaneously it works on Windows. It also explains why libstdc++ could run based on MSVCRT.

    Now, things become much easier, because libstdc++ provide better support to latest C++ standard, MinGW choose that.