Wednesday, September 15, 2010

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:
Step1: Installation

> 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:

#include <stdio.h>
#include <conio.h>
int main(void)
{
      printf("My GCC is working with MinGW");
      getch();
      return 0;
}

> 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"

#include <windows.h>

int WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
  MessageBox(0,"Hello, Windows","MinGW Test Program",MB_OK);
  return 0;
} > 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"

#include <windows.h>

char *AppTitle="Win1";
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int nCmdShow)
{
  WNDCLASS wc;
  HWND hwnd;
  MSG msg;

  wc.style=CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc=WindowProc;
  wc.cbClsExtra=0;
  wc.cbWndExtra=0;
  wc.hInstance=hInst;
  wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  wc.hbrBackground=(HBRUSH)COLOR_WINDOWFRAME;
  wc.lpszMenuName=NULL;
  wc.lpszClassName=AppTitle;

  if (!RegisterClass(&wc))
    return 0;

  hwnd = CreateWindow(AppTitle,AppTitle,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,CW_USEDEFAULT,100,100,
    NULL,NULL,hInst,NULL);

  if (!hwnd)
    return 0;

  ShowWindow(hwnd,nCmdShow);
  UpdateWindow(hwnd);

  while (GetMessage(&msg,NULL,0,0) > 0)
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
  switch (msg)
  {
    case WM_PAINT:
    {
      PAINTSTRUCT ps;
      HDC dc;
      RECT r;
      GetClientRect(hwnd,&r);
      dc=BeginPaint(hwnd,&ps);
      DrawText(dc,"Hello World",-1,&r,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
      EndPaint(hwnd,&ps);
      break;
    }

    case WM_DESTROY:
      PostQuitMessage(0);
      break;

    default:
      return DefWindowProc(hwnd, msg, wparam, lparam);
  }
  return 0;
}
> 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:

#gcc -Wall -mwindows -O2 main2.cpp -o main.exe

###############################################
# COMPILER
###############################################
CC = gcc

###############################################
# EXE Environment
# -mwindows for Windows Environment
# blank for DOS environment
###############################################
EXE_ENV = -mwindows

###############################################
# File optimization level
# O0 - Level 0
# O1 - Level 1
# O2 - Level 2
# O3 - Level 3
# Os - Optimize for Size
###############################################
OPTIMISATION = Os

###############################################
# GCC Options that help in debudding
# -Wall to show all the warnings
###############################################
OPTIONS = -Wall

OUTPUT = TEST1.EXE

all: 
      $(CC) $(OPTIONS) $(OPTIMIZATION) $(EXE_ENV) main2.cpp -o $(OUTPUT)
     
clean:
      rm -f *.exe
      rm -f *.o

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.

Warm Regards,
Boseji

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to Boseji's Blog

Popular Post

Blogger templates

Welcome

Creation by Electronics Hobbyists from Bharat(India) to encourage & help people. This blog hosts the numerous innovations, discoveries and projects we provide.
Our Research Wing Adhar Labs
Contact Form | Email Address
We Support Open Source Hardware Definition v1.0
Creative Commons License
All works on this blog are licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at forbharat.wordpress.com and m8051.blogspot.com.

Permissions beyond the scope of this license may be available at http://forbharat.wordpress.com/permissions/.
Thank you for all your support.

© A.D.H.A.R Labs Research 2008-2013 . Powered by Blogger.

Follow by Email

Followers

PCB Unit Converter

mils
mm

- Copyright © Electronics for Bharat -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -