MinGW, a contraction of “Minimalist GNU for Windows”, is a minimalist development environment for native Microsoft Windows applications. I had know this for a long time. For many cross compilers I was using GNU gcc compiler. However my Win32 experience was mostly using the VC++ from Microsoft. I always wanted to do away the propitiatory stuff related to Visual Studio and all. The best way for hobbyists like me is to go for GNU alternatives.I tired my luck in installing and running my first program on the MinGW today.
So here goes what’s we need to do:
> First we need to download the MinGW suit from Sourceforge. Fortunately unlike other GNU tools this has a good Installer feature. I downloaded the recent mingw-get-inst-20100909.exe, or which ever gets updated.
However for the installer to work an Internet connection is needed for it to download the complete packages.
Note that installation user need to be a Administrator user, or at least have Administrator rights on the PC, this is being installed. This is important since we need to also configure the path for the executable.
> After the download is complete, execute the the installer. It would ask for creation of the Software icons etc. Click next to continue
> Next it would ask for the Repository Catalogs – Select the Use pre-package repository catalogs.
Since its always better to trust the tested packages than the beta ones.
> Accept the GNU License to continue
>It would prompt to create the MinGW directory. Possibly like C:\MinGW. This can be set to another location or left as it is. We would consider it left as it is for this tutorial.
> Next comes the selection of the Program File group.
> Finally the important step the selection of the packages: Select the C++ Compiler and MSYS system if you like. However its better to install MSYS in case you need the feel of linux under windows.
> After this it opens a command prompt window to download the packages. There may be a case that the internet connection drops out. Don’t worry, the install can be reinstated again and again it would start at the exact point of download it had stopped.
> If all the download is completed successfully you don’t get a error message before Finish.
Now an Important step is to set the Path:
>Go to Start Menu in WinXP right click on the MyComputer and select Properties to open the “System Properties Dialog”.
>Go to the Tab “Advanced” at the bottom would be button named “Environment Variables”. Click this to open the “Environment variables” dialog
> In the “System Variables” list scroll for “Path” and click on Edit.
> It will already have some data, that would be default path of the system.
Concatenate to it “;” and click ok to set the path.
Ensure that path is not accidentally over written or many things on the system would fail.
Best way it the copy it to notepad, add the required parameters and then paste it back to the Input box.
Now the PC is ready to execute the MinGW programs!!.
Step 2: First Dos Based (Terminal Based) Program
>First we need to open the Termial : Use “Start Menu” ==> “Run” Type “cmd” to open the command prompt.
> Go to the desired directory where the programs stored. For this tutorial its assumed to be at C:\MinGW\tut\ directory. So we give the command “cd c:\MinGW\tut” in the terminal
> Check the GCC version – give the command “gcc -v” it should display the version of the current GCC installed with MinGW.
> use Notepad to make you first program: For this tutorial we name it as “main.c” Below is the code:
> Now give the first compile command: “gcc -Wall -O2 main.c -o main.exe“
Here the “gcc” is the compiler , “-Wall” says that we need to see all the warnings, “-O2” is the Optimization directive, we have the file name to be compiled and then “-o ” specifies the final “exe” (on Windows) we want to generate.
> After this you can see the file has been created named “main.exe“. Double click on this to execute.
> If everything is fine then you get a small terminal window displaying “My GCC is working with MinGW”, press any key to continue.
This way the simple terminal based programs can easily be made using this excellent power open source compiler.
Step3: First Windows Program
> Here is the program named as “main1.cpp”
} > To compile type : “gcc -Wall -O2 -mwindows main1.cpp -o main.exe“
Here the additional “-mwindows” instructs the compiler that this is a Windows application.
> Double click on the “main.exe” created. If everything works fine we would see a small message box:
> This is the basic Message Box api for Win32 applications.
Now lets do some advanced stuff:
Step 4: Advanced Windows based Program on Win32
> Here is the code for this named as “main2.cpp”
> This program would be compiled using :”gcc -Wall -O2 -mwindows main2.cpp -o main.exe“
However you might see a depreciated warning since we have the “-Wall” option.
“main2.cpp:3:16: warning: deprecated conversion from string constant to ‘char*’“
Dont worry its alright to have this warning.
> Now Double click on the “main.exe” created, If every thing goes fine we see:
> That’s the first Windows program with callback implemented.
But we are still not doing the things in the GCC way, create the Makefile:
Now in the terminal just give the “make” command and the file magically compiles.
If you need to clean up the file just give “make clean“
I would be posting more advances soon.
Meanwhile let me know your feedback to make this article better.