[S4] make basics - calculator

Estimated time: 5 minutes

You have just built a simple calculator. When typing ls in your command line, the source folder has the following structure:

calc.c    calc.h    Makefile    operations.c    operations.h

calc.c contains the calculator which handles the 4 basic operations: addition, substraction, multiplication, and division. These 4 operations are implemented in operations.c.

As you might have noticed, there are also two other files: calc.h and operations.h. These are the corresponding header files of calc.c and operations.c. These headers contain the function prototypes and specifications, but not their definitions. They can also contain extern declarations of variables. For more on that, see this page or chapter 4 in [kernighan2006c].

When compiling this calculator, you need to specify in the Makefile rules the header files needed by calc.c, that is both calc.h and operations.h.


[kernighan2006c]Kernighan, B. W., & Ritchie, D. M. (2006). The C programming language.

Question 1: Linking object files

Unfortunately, for some obscure reasons the source folder (including the precious Makefile) was lost but you still have access to the object files.

Write a Makefile rule to build the executable calc from both object files calc.o and operations.o. That is, you want to link the object files into the executable.

Hints: You do not need to write the rules for operations.o and calc.o. Do not forget the prerequisites of your rule. You cannot use implicit rules, you must use explicit rules.

Question 2: Object files and prerequisites

Now, you just remembered you had created a git repository for this calculator on a remote server. You clone it on your computer, and verify each file with the command line editor nano. You notice the Makefile is incomplete. There are no recipes to build the object files!

Write Makefile rules to build the object files calc.o and operations.o from the source files.

Hints: Do not forget the prerequisites of your rule. You cannot use implicit rules, you must use explicit rules.

Information

Author(s) Pablo Gonzalez Alvarez
Deadline No deadline
Submission limit No limitation
Category Tags S2

Tags

make & Makefile

Sign in