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 ...
	command1
	command2
...

The target and prerequisites are separated by a colon (:). The commands must be preceded 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 terminal, type:

    touch Makefile
    

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

  3. You will need to edit the Makefile. You need to do this in a text editor that will NOT put spaces in when you type a tab. By default, many programming tools will convert a press of the tab key into spaces; avoid IDEs, or first research how to force that editor to 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 Makefile put the following:

    program.exe: main.cpp Answer.cpp Answer.h
    	g++ -g -Wall -o program.exe main.cpp Answer.cpp
    
    • This defines one make target called program.exe. A typical targets is named after the file it says how to make.
    • It has three pre-requisites: all of the files that, if they changed, you would want to remake the target.
    • The command builds the file program.exe using g++
      -g says to include debugging info. This helps if you use Valgrind on this project
      -Wall says to mention ‘all’ warnings
      -o program.exe gives the output file the correct name
      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 are not compiled, they are included into the .cpp files as appropriate. Any .h files you might edit should be listed in the prerequisites, but only there.

    Save the Makefile.

  5. Continuing in the terminal, type:

    make
    

    The default behavior for make is to run the first target in the file. In this, case that is the target program.exe. You can also run a particular rule by calling it explicitly.

    make program.exe
    
  6. Now type:

    ./program.exe
    

    To run the program program.exe which is in the current directory (.). In general, this is how you will want to run your programs.

  7. Usually, people add some additional rules to every makefile with some common behavior. (These are called ‘phony’ rules, because they are not named after a file they produce but just given convenient names. You can name a rule whatever you like, but if it doesn't make a file with that name, you should label it phony.)

    Add a rule to build all of the targets and to clean up, leaving the Makefile looking like this.

    .PHONY: all
    all: program.exe
    
    program.exe: main.cpp Answer.cpp Answer.h
    	g++ -g -Wall -o program.exe main.cpp Answer.cpp
    
    .PHONY: clean
    clean:                  
    	rm -f program.exe
    

    The all target should come first, so that running make with no arguments builds everything by default. The all target simply depends on all of the files you want to make sure are always up-to-date, and does not need a recipe. The clean target doesn't have any prerequisites, so it always runs, and it deletes files created by the build process. It uses rm to remove files, and the -f option means that it won't ask for confirmation or complain if the file(s) already don't exist.

  8. Now if you run make clean, you should end up with only your source files and not the compiled program.

  9. You can rebuild the program with make or make all. If you try to rebuild it after the program has already been built and there are no changes to the source files, make will detect that nothing needs to be done.