MinGW for First Time Users HOWTO

For those who are just starting out, this will explain how to use MinGW to compile an executable, from C or C++ source, in a command prompt environment.

First you must make sure you have installed MinGW and set the PATH environment variable include the location of MinGW;
this is explained Here.

Now, assuming your directory for MinGW is the default "C:\MinGW", and your PATH environment variable is set to include "C:\MinGW\bin", it is easy to start compiling an executable:

Open a command prompt window, and set the current directory to wherever your *.c or *.cpp file is.

Example:
For the file helloworld.cpp in the folder C:\sources\hello enter the commands

cd c:\sources\hello

Now type the compile command

g++ helloworld.cpp -o helloworld.exe

The -o switch specifies the name of the output file, without it the output file would be given a default name of "a.exe".

If there are any errors in the source file the command line will notify you and direct you to the line where the error is
Say you had forgotten to add an extra line at the end of your source file (an easy mistake when moving from an environment such as Microsoft Visual C++) and you missed the ; at the end of the last statement. You will get a message similar to this:

helloworld.cpp: In function 'int main()':
helloworld.cpp:4: error: expected ';' before '}' token
helloworld.cpp:5:2: warning: no new line at end of file

Which describes both the positions of errors/warnings and the problem. I.e, for the missing ";" the error is on line 4, so check your source file at line 4 for the error.

When compiling more than one cpp file, list the names of files after the g++ command and before the -o switch.

g++ helloworld.cpp foo.cpp -o helloworld.exe

Note: if your source is in C, rather than C++, your source files will be "helloworld.c", "foo.c", etc., and you would use the "gcc" command, rather than the "g++" command; otherwise the procedure is similar to the above.

Whilst still in the command prompt (and dircectory is still at the .exe's location) you can run your program by typing the name of the executable file:

helloworld.exe

This will run your executable in the command prompt window.

For complete instructions on how to use the MinGW compiler, refer to the GCC manual.
This contains the commands/switches and their usage which will allow much more control over MinGW. This is a great, albeit long, resource for anyone starting off.

592347 reads

Site Status

Site maintenance completed May 25th, 2012 at 12:38 UTC

Search this site:

Administrative Update

As of June 2013 we are accepting donations. Please see the Donate page for more information on how to Donate.