Making A Simple Program

Instructions to build a simple makefile for a single program.

A makefile consists of a set of rules, each with 3 parts:

  • a target
  • a list of prerequisites
  • a list of commands.

The general syntax for a rule is:

target: prerequisite-1 prerequisite-2 ...

The target and prerequisites are separated by a colon (:). The commands must be receded by a tab (NOT spaces).

If a command is not preceded by a tab, you get an error message like this:
makefile:4: *** missing separator. Stop.
If you see this message it usually means you have spaces instead of tabs.

  1. From the terminal (use git bash on Windows), navigate to the folder that has the code you wish to make a Makefile for. The sample shown below uses the MakeBasics project from the CS260 code (should be cs260Code/Week01/MakeBasics/).

  2. In the dev environment, type:

    touch MyMake

    This makes an empty file with the name MyMake. You could make this file in your host OS, but need to make sure it does not have a .txt extension.

    The "normal" name for Makefiles is "Makefile". But QTCreator uses that name. To avoid confusion with its build product, we will always call our hand-made files "MyMake".

  3. You will need to edit the MyMake file. You need to do this in a text editor that will NOT put spaces in when you type a tab. By default, QTCreator and many other programming tools will convert a press of the tab key into spaces - do not use them. (Or if you want to, first research how to make your text editor use tabs).

    Open the file with a super-simple text editor:

    • Windows: Notepad
    • Mac: Textedit (see these instructions for switching to plain text mode)
  4. In the MyMake file put the following:

    	#make a directory to hold the build product
    	mkdir -p build
    	#build MyProgram.exe to that directory
    	g++ -g -o build/MyProgram.exe main.cpp Answer.cpp
    • This defines one make target called all. (There is nothing special about that name, it could be anything you want.)
    • It has no pre-requisites, meaning the rule all can happen no matter what.
    • The first command makes the directory build
      -p says to "don't report an error if it already exists"
    • The second command builds the file main.cpp using g++
      -g says to include debugging info. This helps if you use Valgrind on this project -o build/MyProgram.exe calls the output file MyProgram.exe and places it in the build directory main.cpp Answer.cpp are the list of files that need to be compiled

    Note that you should NOT list the .h files, in the list of files to be compiled. They will get included from whatever .cpp files you use.

    Save the file.

  5. In the dev environment, type:

    make -f MyMake

    To run make and tell it to use the file (-f) called MyMake. The default behavior for make is to run the first target in the file. In this, case that is the target all. This should build MyProgram.exe. If there are error messages, double check your makefile - make sure it has tabs and not spaces. You can also run the first rule by calling it explicitly:

    make -f MyMake all
  6. Now type:


    To run the program MyProgram.exe which is in the build folder within the project folder. The working directory will by the project folder (where the makefile and code are) that is where the program will try to read and write any files to/from. In general, this is how you will want to run your programs.

  7. Now add a second rule to the file. Make it look like:

    	#make a directory to hold the build product 
    	mkdir -p build
    	#build MyProgram.exe to that directory
    	g++ -g -o build/MyProgram.exe main.cpp Answer.cpp
    	rm -rf build   

    This defines a new rule called clean. The rule removes (rm) the folder called build -rf says to do so "recursively" (removing all contents from in the folder) and to "force" it to happen without asking for confirmation

  8. To run the new command, type:

    make -f MyMake clean

    This will run the rule with the target clean instead of the first rule. It should delete the build folder.

  9. To run the first rule again to build the program, you can either explicitly specify the all target:

    make -f MyMake all

    Or rely on the fact that it is the default target because it is first:

    make -f MyMake