***

Master of Science in Computer Science - Computer Science Introductory Course

Laboratory Work 4: Makefile

Translated from a French version designed by Bertrand Dupouy


1. Introduction to the program to be studied

We are going to study the program contained in starwars.c (To get a copy of it, click here.)
The comments in the program are  in French.
The program simulates a sword fight between SkyWalker (the good guy) and DarkVador (the bad one!) , heroes of the famous Star Wars saga.

Sword strikes are simulated by sending a signal to the opponent. Each fighter protects himself by ignoring the corresponding signal. When they attack their opponent, the fighters become sensitive to signals and can receive strikes. The program is divided into 3 functions:

Create a new directory (called tp_chprod, for example), copy starwars.c in it, and do the lab work in it.
mkdir tp_chprod
cd tp_chprod

1.1 Question 1

Reminder:

while (PointsDeVie[Moi] > 0)
{
signal (Sabre[Lui], SIG_IGN); /* Comment 30 */
sleep (random() % 3);
sleep (TempsDefense[Moi] - 1); /* Comment 35 */
signal (Sabre[Lui], Touche); /* Comment 40 */
sleep (TempsAttaque[Moi]);
if (kill(Pids[Lui], Sabre[Moi]) < 0) break; /* Comment 45 */
}

1.2 Question 2

We are now going to cut the file in several sub-files :
  1. Cut the file as specified.
  2. Include the required .h files in the appropriate .c files
  3. Recompile each file as indicated below. You should not get any  "warnings". If there are, go back to step 2.
To create an executable do :
gcc main.o foncs.o -o starwars

2. Makefile file

We are now going to write the corresponding Makefile using the template provided here.

2.1 Question 1

Edit the makefile and then type:

gmake all
starwars

to verify that it works fine.

2.2 Question 2

Create an include directory and move all .h files to it.
Do :
gmake all
Modify the Makefile in order for the Makefile to find all include files without having to modify the source code of any file.
Hint :

2.3 Question 3

In main.c, change line:
sleep (random() % 3);
to
sleep (sqrt (random() % 3) );
Do gmake starwars .
Which modifications can you perform to remove the errors ?
ALL warnings must disappear.

2.4 Question 4

Do  gmake starwars .
In utils.h, change line:
typedef short POINTS;
to
typedef float POINTS;
  1. Do again gmake starwars.
    If gmake recompiles good ! jump to next step.
    Otherwise, if nothing happens then something is missing as utils.h concerns foncs.c and main.c. Explain why nothing has been recompiled  ?
    Modify Makefile to solve this problem.
  2. Do again gmake starwars, there may be a warning that we are going to ignore for NOW.
    Execute gmake starwars
    The values displayed are relative to life points (points de vie) in main and in touche ( ... Pts DV : ... ) and are surprising.
    correct program, error in printf !!!!
  3. Correct the cause of  warning.
    Be careful when using printf to debug


3. Debugging

To debug, we advise you to use gdb or xxgdb that allow you to follow step by step execution and to inspect and modify program variables.
In order to use such a debugger you need to add the -g flag to both CFLAGS and LDFLAGS.
Do gmake all, and use gdb as indicated below.

If you want to use  gdb with starwars, do :

gdb starwars

We can now execute starwars under gdb control and try the following commands :


(gdb) break main
Breakpoint 1 at 0x10900: file main.c, line 36.
(gdb) run
Starting program: starwars
Breakpoint 1, main () at main.c:36
36 int TempsDefense[] = {5, 3};
(gdb) step
37 int TempsAttaque[] = {3, 1};
(gdb) step
41 printf ("PointsDeVie : SkyWalker %d DarkVador : %d\n",
(gdb) step
PointsDeVie : SkyWalker 1076101120 DarkVador : 0
44 pid = fork();
(gdb) print PointsDeVie[0]
$1 = 10
(gdb) print PointsDeVie[1]
$2 = 10
(gdb)

Note: xxgdb is a graphical version of gdb.

Licence de droits d'usage ©(Copyright) Bertrand Dupouy- Contact : Petr Kuznetsov (surname . name @ telecom-paristech.fr) - Last Modified, Sept 26, 2013.