Monday, January 26, 2009

Installing MinGW on Windows

MinGW is a free software development system for Windows. Not only is MinGW useful for porting Linux software to Windows, but you can also use MinGW to write native Windows programs. The following is an excerpt from the MinGW web site:


MinGW: A collection of freely available and freely distributable Windows specific header files and import libraries, augmenting the GNU Compiler Collection (GCC) and its associated tools (GNU binutils). MinGW provides a complete Open Source programming tool set which is suitable for the development of native Windows programs that do not depend on any 3rd-party C runtime DLLs.

Now, let's install MinGW. Note that MinGW doesn't run smoothly on Windows Vista because of weird filesystem restrictions. The workaround is:




  • to install MinGW in your home folder (C:\Users\username) or a public folder (somewhere in C:\Users\Public, for example, C:\Users\Public\Desktop)
  • to run MSYS (rxvt) as administrator (Right-click and select Run as administrator)
  • to install MinGW on FAT filesystem,
  • to work in the safe mode, or
  • to use MinGW Portable which is said to be working well.


To Set up the Latest MinGW


To have a functional MinGW installation, just download and run the latest MinGW installer.





To Set up GCC toolchain 3.4.5


To install the outdated GCC toolchain 3.4.5, download the following installers and run them in the order:




After Installation


After successfully installing MSYS, check the file /etc/fstab inside the MSYS folder. Mine looks like:


C:/MinGW /mingw
C:/MinGW /usr/local


You can try MSYS by double-clicking the MSYS icon on the desktop. This will bring up a console window where you can type in some UNIX commands. For example, the following command will display the version of installed GCC compiler:


$ gcc --version
gcc.exe (GCC) 3.4.5 (mingw-vista special r3)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


The environment variable HOME can be used to set the user's working directory. If your username has a space or foreign character, it may confuse MinGW and hinder the compile process. In Windows Vista and Windows 7, access restriction is imposed outside C:\Users. In such cases, set $HOME to some other location. For example, set the HOME variable like this:




Don't forget to create ~/.profile. This file can be used to set environment variables, such as CC, CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS. These environment variables are used to customize the compiler settings. The following is my .profile contents:


CC=/mingw/bin/gcc.exe
CFLAGS='-march=pentium2 -mtune=i586 -mthreads -mms-bitfields -O2'
CXXFLAGS='-march=pentium2 -mtune=i586 -mthreads -mms-bitfields -O2'
LDFLAGS='-Wl,--enable-auto-image-base,--enable-auto-import,--enable-runtime-pseudo-reloc'
PKG_CONFIG_PATH=/mingw/lib/pkgconfig
TERM=cygwin
export CC CFLAGS CXXFLAGS LDFLAGS PKG_CONFIG_PATH TERM


Testing GCC with simple codes


Let's compile a very simple C program. Using a text editor, such as Notepad, create a text file called test.c:


#include <stdio.h>

int main(void)
{
printf("Greetings, ladies and gentlemen.\n");
return 0;
}


The following command will produce an executable code.


gcc -o test.exe test.c


Now, let's try a very simple C++ code:



#include <iostream>
using namespace std;

int main()
{
cout << "Hello, boys and girls.\n";
return 0;
}


To build it, type:


g++ -o test.exe test.cpp


Additonal Tools to Install


You can optionally install other tools from the MinGW download site, such as mingw-utils and bison. mingw-utils provides some useful tools, including dos2unix, a2dll, pexports and reimp. I find dos2unix particularly useful as it converts DOS-style line endings (CR+LF) to UNIX-style (LF) in text files. mingw-utils should be extracted into the MinGW folder and bison should be extracted into the MSYS folder.


Some tools are commonly used in the Linux development environment, such as pkg-config and msgfmt. I've compiled static versions of these programs in order to ease compiling processes. Download them from the links below and place them in /mingw/bin.




Integrated Development Environments for MinGW


If you prefer to use a convenient IDE to the command line, you have several choices:





Related Posts


2 comments:

  1. Hi, After the installation, I could not find fstab in the MSYS folder(only have fstab.sample). And when I entered the command gcc --version, it displays sh: /c/cygwin/bin/gcc: Bad file number. what went wrong? I need to use MSYS to compile gnutls. Please help, thanks!

    ReplyDelete
    Replies
    1. You should create your own fstab.

      You probably have both Cygwin and MinGW installed. When using MinGW, make sure Cygwin is not in the path.

      Delete

About This Blog

KBlog logo This blog is about current events and issues concerning general population. Thanks for visiting the blog and posting your comments.

© Contents by KBlog

© Blogger template by Emporium Digital 2008

Followers

Total Pageviews

icon
Powered By Blogger