Editing/Compiling Programs on Windows Using MinGW

So, you are new to C or C++ and you want to figure out how to get started? How to run your code? How to create executables? Well, here is all you need to know.

EDITORS: 
There are quite a few editors for C and C++, and some which even support multiple other languages. It's up to you to figure out which one works best for you. Some of the better ones that I am aware of are:

Notepad++: Fully featured editor which supports a wide variety of languages out of the box.
http://notepad-plus-plus.org/ 

SciTE: Fast and lightweight cross platform editor. Supports many languages and has a nice syntax highlighting scheme.
http://www.scintilla.org/SciTE.html 

Emerald Editor: Another free and easy to use, small editor. This one also supports a wide variety of programming languages and has macro capabilities. It is based on Crimson Editor, which is no longer in development.
http://www.emeraldeditor.com/ 

TextPad: Quite nice, but not completely free. If you want to keep this editor, you should eventually pay for it.
http://www.textpad.com 
You can get the proper syntax highlighting plugin from:
http://www.textpad.com/add-ons/syna2g.html 

Dev-C++: Included only for the reason that it is extremely popular, Dev C++ is not a stand alone editor, but an all out Integrated Development Environment which uses MinGW/GCC. For the purposes of this tutorial, we'll assume you're not using Dev-C++.
http://www.bloodshed.net/devcpp.html 

COMPILING: 

If you want to compile your source code, here is the "best", most straight-forward way. We will be working with GCC, through the Windows MinGW port, probably the most popular cross platform collection of compilers. It include compilers for C, C++, Objective C, Java as well as Ada.

First, what you need to do is download it from here:
http://sourceforge.net/projects/mingw/files/latest/download 

Next, just install it, no differently than you would install any other program. You can keep all the configurations and settings as their default; that should be good enough.

Alright, this is the part that you need to pay attention to. To use the compiler through command prompt, you will need to edit the Windows PATH environmental variable. If you are using Windows 2000 or Windows XP then you have to:

  1. Right click on My Computer from the desktop. 
  2. Select properties. 
  3. Go to advanced. 
  4. Click environmental variables. 
  5. Under system variables select Path 
  6. Click edit. 
  7. Add ; and then where you installed MinGW to. If it was C:/Program Files/MinGW then you would add ;C/Program Files/MinGW/bin to the end of what is already there, do not overwrite what is already there!
Now you're done! To test to see if it worked, and compile programs, open command prompt. If you don't already have a shortcut then it should be somewhere in the accessories folder in the start menu.

When you are already there, you will see the directory which you are already in. Now what you need to do is browse to where the source file is. You will be using DOS style commands, if you are not already familiar with them then:

  • type cd to open a folder and browse it. 
  • type cd .. to go up, 'back' into a more general directory.
  • type dir to see what folders and files are already in the directory


Now, once you are at the location of your source file, to compile it you have to type:


1.    For C++: 
g++ .cpp -o .exe 

2.    For C: 
gcc .c -o .exe 

3.    For Java: 
gcj .java -o .exe


This just specifies which compiler you are using, the source file, and what the resulting executable file will be called.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © 2013 Taqi Shah Blogspot -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -