C/C++ compiling with GCC

back to Grand Files

This tutorial will show you how to compile C and C++ files with MinGW, and how to compile and run programs in Geany.

First of all, we will need to download and install MinGW. Download it from https://sourceforge.net/projects/mingw/files/. There is a nice online installer which I recommend you use (it's under "Automated MinGW Installer" on the SourceForge page). However if you need to install it on an offline computer, I'll show you how to do that now.

Download the .tar.gz files using the online installer or from https://sourceforge.net/projects/mingw/files/. Make a folder to put all of the MinGW files. For this tutorial I'll be using C:\MinGW.

Unzip the MinGW files to the directory you have chosen.

Next we will need to set the path to the MinGW files so Windows can access them from the command line. Hold the Windows key and press the Break key to open the System Properties window. Alternatively, open System in the Control Panel. Select the Advanced tab and click on Environment Variables.

Select the Path variable is System variables (or user variables if you prefer) and click Edit. End the last entry with a semi-colon (;) and type "C:\MinGW\bin" (or the "\bin" directory of whatever folder you made).

Let's see if it works. Open the command line by openning Run and typing in cmd.

Type in gcc -v to see the version information.

Now we will compile a C program in Geany. Geany can be downloaded here. To get Geany into C mode, you can either save the file as a .c file, open a .c file or select "C source file" from Document > Set Filetype > Programming Languages.

Open Build > Set Includes and Arguments. Copy the text from Build into Compile. You can do the same for C++ files as well.

Here it a very basic program in C which writes Hey!.

To compile a program, press the compile button (or press F8).

If all went well you will be able to see in the compiler tab at the bottom of the screen. If there were warnings or errors, they will also be displayed here.

To execute the last successful compilation of your program, press the execute button (or press F5).

Don't worry about the Press any key to continue..., this is made by Geany and isn't actually part of your compiled binary file, it just makes it easy for you to run your programs.

Here is a similar program to the one above, this time in C++.

So now you can compile C and C++ files!

One thing to note is that MinGW does come with make, however it has the file name mingw32-make.exe. If you don't know what that means, don't worry about it, but if you do, you can rename mingw32-make.exe to make.exe, or copy it and name the copy make.exe if you want.

If you want to learn C or C++, there are plenty of resources on the internet as well as plenty of books. For an introduction to computer science and C programming in lecture format, you can download videos from Harvard's CS-50 podcast.