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:
- main contains global
variables, a few #define and typedef
statements and simulates the fight.
- Touche processes a received
sword strike.
- Fin is called when a child
process terminates
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:
- signal(sig, fonc) specifies
that the fonc function should be called if
a signal number sig is received
- kill(pid, sig) sends signal
number sig to the process identified by pid,
- Read the comments at the beginning of starwars.c
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 */
}
- What does a process do when it executes instructions between
comment
30 and comment 35 ? What behaviour is simulated by these instructions ?
- What does a process do when it executes instructions between
comment
40 and comment 45 ? What behaviour is simulated by these instructions ?
- What happens for each process when signal Sabre[Lui]
is processed by function Touche ?
1.2 Question
2
We are now going to cut the file in several sub-files :
- - main.c that contains only
main, the global variables and their
initialization
- - foncs.c that contains
only Touche and Fin,
- - foncs.h
that contains the prototypes of functions Touche
and Fin,
- - utils.h that contains only
typdef and #define statements
- - globals.h that contains
only the extern declarations relative to the
global variables,
- Cut the file as specified.
- Include the required .h
files in the appropriate .c files
- Recompile each file as indicated below. You should
not get any "warnings".
If there are, go back to step 2.
- gcc -Wall -c main.c
- gcc -Wall -c foncs.c
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 :
- use the -I option to tell the
compiler where to find the include files,
- for the make command to find
these files, use a variable that gives the name of the directory where
the include files are stored.
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;
- 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.
- 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 !!!!
-
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)
-
break sets a breakpoint
- run restarts execution until
another breakpoint is encountered.
- step
executes the next instruction and stops.
- print visualizes the variable values.
- variable values can be modified using the set
command, etc.
Note: xxgdb is a graphical version of gdb.
©(Copyright)
Bertrand Dupouy- Contact : Petr Kuznetsov (surname . name @ telecom-paristech.fr)
- Last Modified, Sept 26, 2013.