12

I have C program I compile with mingw on Windows. It works fine but requires MSVCRT.DLL. I want to link that statically (like I can do in Visual Studio). Is this possible?

I tried -static flag to gcc and it didn't make any change.

What about C++ program using also standard C++ library?

zaharpopov
  • 16,882
  • 23
  • 75
  • 93
  • about your last question... if you are talking about STL, it's header-only. – Armen Tsirunyan Oct 15 '10 at 14:59
  • @Armen: but isnt there also DLL for C++ MSVCPT or something like that? – zaharpopov Oct 15 '10 at 15:12
  • 3
    Do note that MSVCRT.DLL is included as a system component in pretty much every version of Windows you are typically likely to encounter. In particular, you don't need to distribute it with your application because your end users will already have it. A natural question is then why do you need to avoid dynamic linkage to it? – RBerteig Oct 15 '10 at 18:31
  • 4
    Also, remember that you can confirm what DLLs are actually in use with [Dependency Walker](http://www.dependencywalker.com/). – RBerteig Oct 15 '10 at 18:35
  • 1
    The latest mingw, using gcc 4.5, has additional library dependencies, including libgcc_s_dw2-1.dll and libstdc++-6.dll. I haven't been able to remove these, and so I'm distributing them with my application. – Neil Mayhew Jun 17 '11 at 22:49

3 Answers3

13

I believe that MinGW doesn't use the static runtime library for copyright reasons.

You can maybe try to use newlib (http://sourceware.org/newlib/) to create an executable that doesn't link to msvcrt.dll

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 6
    +1 for copyright avoidance. That is a clever side effect of it already being on every Windows system. Programs built with MinGW require it, but never have to distribute it. Hence, no copyright issues. – RBerteig Oct 15 '10 at 18:33
  • 1
    Even if i Not use any std C functions in code it requires MSVCRT I think for CRT startup/cleanup codes that run before/after main. newlib provides that also? – zaharpopov Oct 16 '10 at 06:48
  • What about MinGW64? – Royi Oct 06 '19 at 21:51
1

Having spent a while searching for this myself, I came across this post introducing the in-progress Mingw32 Alternate C Runtime Library. However, it's not ready for use, and the developer appears to have abandoned it. Home page link here.

Posting this answer here in the hope that it'll come in useful to people googling at some point in future.

Riot
  • 15,723
  • 4
  • 60
  • 67
-1

This doesn't answer the question of "how to avoid linking with MSVCRT.DLL", but if you're here for a more general question of "How to link with MinGW statically"...:

Link with gcc -static -static-libgcc -static-libstdc++ and you won't need libgcc_s_dw2-1.dll and libstdc++-6.dll

rustyx
  • 80,671
  • 25
  • 200
  • 267
jefke
  • 27