Revision of HOWTO Compile Programs with MinGW -- A Guide for First Time Users from 2009, January 19 - 12:27

Printer-friendly version

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

First you must make sure you have installed MinGW and set the System Variable for Path is set to express the location of MinGW.
This is explained Here.

Now, assuming your directory for MinGW is the default C:\MinGW, and your Environment Variable is set to C:\MinGW\bin, it is easy to start compiling a C++ executable.

Open up an elevated command prompt window (start as admin in Vista) and set the current directory to where your *.cpp file is.

For the file helloworld.cpp in the folder C:\docs\hello enter the command

         cd c:\docs\hello

Now type the compile command


         g++ helloworld.cpp -o helloworld.exe

The -o switch defines the name of the output file, without it the output file would be 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. Ie, 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

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:


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.

This wiki is not a forum for discussion of usage issues. Please use the list instead. We do not allow creation of comments by anonymous or untrusted users, on any page.
Site Status

Site maintenance performed successfully, site online Dec 13th 12:00 AM Eastern.