Removing msvcrt dependencies

Even when msvcr71.dll is explicitly named on the command line as a place to link gcc still has an annoying tendency to link to msvcrt.dll for a few functions such as abort and fprintf. It may be that gcc falls back on the default configuration for functions called from compiler generated boilerplate code. Fortunately the defaults are read from an external text file.

Warning: the following change will make MingW link to msvcr71.dll by default. It is suggested that a backup of the configuration file be made before editing. In this way MingW can be easily switched back to msvcrt.dll linkage.

These instructions are for gcc-core-3.4.5 and mingw-runtime-3.11 . gcc 3.4.5 can build DLLs using shared library command line options. The 3.11 runtime includes a library for linkage against msvcr71.dll. It should be expected that newer versions of MingW, as they become available, will also provide good msvcr71 support.

Under the main MingW directory the configuration file has the pathname lib\gcc\mingw32\3.4.5\specs . To make msvcr71 the default C library only line 54 needs editing: %{mthreads:-lmingwthrd} -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt Just replace -lmsvcrt with -lmsvcr71 and you are ready to go.

One other place with msvcrt dependencies is calls to non-ansi library functions, such as strdup. In newer versions of the Visual C library, such as msvcrt, the names of non-standard functions start with an underscore. But MingW provides backwards compatibility with earlier, non-underscored, names via library libmoldname.a. Unfortunately libmoldname.a links to msvcrt.dll. The solution is to build a new libmoldname that links to msvcr71.dll.

Download mingw-runtime-3.12-src.tar.gz (*) from here at SourceForge.NET and extract into its own directory. Copy lib\libmoldname.a from the MingW directory to the runtime directory. Open a MingW enabled prompt (**) and change to the runtime source directory. Now enter the following commands (taken from the MingW runtime make file): ar x libmoldname.a isascii.o iscsym.o iscsymf.o toascii.o strcasecmp.o strncasecmp.o wcscmpi.o gcc -DRUNTIME=msvcrt -D__FILENAME__=moldname-msvcrt.def -D__MSVCRT__ -C -E -P -xc-header >moldname-msvcrt.def dlltool --as as -k -U --dllname msvcr71.dll --def moldname-msvcrt.def --output-lib libmoldname71.a ar rc libmoldname71.a isascii.o iscsym.o iscsymf.o toascii.o strcasecmp.o strncasecmp.o wcscmpi.o ranlib libmoldname71.a

Copy libmoldname71.a to the lib subdirectory of MingW. Once again edit line 54 of lib\gcc\mingw32\3.4.5\specs, replacing -moldname with -lmoldname71. Now any references to non-standard library routines by their non-underscored names should link to msvcr71.dll instead of msvcrt.dll.

(*) A new version, 3.13, is available. For at least one person, it worked as described here.

(**) The MingW bin directory is in the program search path and environment variable MINGW_ROOT_DIRECTORY is set to the MingW directory path.

Adding msys's /usr/local to mingw search path

This is yet another hack to MingW's spec file. If you do not want to pollute the MingW directory, then this will allow files to be installed in the msys file structure instead. Library dependencies built with msys can be installed in /usr/local rather than /mingw. In lib\gcc\mingw32\3.4.5\specs of the MingW directory make the following two modifications. The first change adds /usr/local/include to the header search. On the line following *cpp: append -Iinclude-path where include-path is the absolute Windows path of the msys /usr/local/include directory. It is important to use forward slashes "/" instead of backslashes "\ in the path. Also be sure to leave a space between the -I option and what precedes it. And example is:

*cpp: %{posix:-D_POSIX_SOURCE} %{mthreads:-D_MT} -IC:/msys/1.0/local/include

The second modification adds /usr/local/lib to the library search. On the line following *link: append -Llibrary-path. Again library-path is a forward slashed absolute Windows path. Here are some examples:

*link: %{mwindows:--subsystem windows} -LC:/msys/1.0/local/lib