Monday, May 12, 2008

Install GCC on Windows with MinGW and MSYS

You can get GCC on your Windows machine using a couple of different methods. I prefer using the packages provided by the MinGW (http://www.mingw.org) folks. They provide a set of installers that you can choose from according to your needs. They also provide software updates that you can unpack and copy on top of the original installation.

Visit http://www.mingw.org/download.shtml and download the following packages. Each file is named according to its version. If newer versions are available under the Current section of the web page, use those instead. For example, download MSYS-1.0.10.exe instead of MSYS-1.0.9.exe.

MSYS-1.0.10.exe
MinGW-3.1.0-1.exe
gcc-core-3.3.1-20030804-1.tar.gz
gcc-g++-3.3.1-20030804-1.tar.gz
First, install MinGW. Throughout this discussion, I'll assume you installed MinGW on the C: drive so that you ended up with C:\MinGW\.

Next, install MSYS. Throughout this discussion, I'll assume you installed MSYS on the C: drive so that you ended up with C:\msys\1.0\. The MSYS post-install script will configure MSYS to your environment. When it asks where MinGW is installed, tell it C:/MinGW (note the forward slash).

MSYS gives you many of the GNU (http://www.gnu.org) tools that are common on Linux systems, such as grep, less, and diff. MSYS also gives you a Bourne shell (a.k.a. command prompt) environment that makes a Linux user feel more at home. In fact, it creates a home directory for you; in my case it is C:\msys\1.0\home\Sid Steward\. When you run MSYS (Start Programs MinGW MSYS msys), a colorful command prompt opens, and it opens in your home directory by default. It is like a little slice of Linux, right there on your Windows machine. Run dir and it doesn't understand. Use ls instead. Run pwd and you'll see that even the filesystem looks different. Your current directory is /home/Sid Steward/, not C:\msys\1.0\home\Sid Steward\. You can access the traditional DOS drive names like so:

cd "/c/Program Files"


Test whether MSYS can find MinGW by running:

$ gcc --version


If it replies command not found, MSYS can't see MinGW. In that case, you will need to edit the text file C:\msys\1.0\etc\fstab so that it includes the line:

c:/MinGW /mingw


Note the forward slashes, and replace c:/MinGW with the location of MinGW on your machine.

To access the MSYS and MinGW tools from the Windows command prompt, you will need to add C:\msys\1.0\bin and C:\MinGW\bin to your Windows Path environment variable. Access environment variables by selecting Start Settings Control Panel System Advanced Environment Variables.

Finally, we'll apply the 3.3.1 updates to the installation. Copy the *.tar.gz files to your MinGW directory (e.g., C:\MinGW\). Open the MSYS shell (Start Programs MinGW MSYS msys) and then change into the /mingw directory (cd /mingw). Unpack the *.tar.gz archives like so:

Sid Steward@GIZMO /mingw

$ tar -xzf gcc-core-3.3.1-20030804-1.tar.gz

Sid Steward@GIZMO /mingw

$ tar -xzf gcc-g++-3.3.1-20030804-1.tar.gz

Now, test to make sure the upgrades worked by checking the versions. For example:

Sid Steward@GIZMO /mingw

$ gcc --version

gcc.exe (GCC) 3.3.1 (mingw special 20030804-1)

Success!

0 comments: