Session 2 : Operating System Programming Concepts : Lab 2 - Makefiles



Lab 2 : Makefiles

Height Of Facebook Addicti0n: 

A B0y's Fb Status 
"I'm Online On Fb During Lecture Hahaha" [:-)] 
Coment 4m His Professor: 
"Get Out Of The Class N0w" [:-P] 
Dean Liked Coment...! [;-)] 
Friend Comented: 
"come fast dude , there is funky situation  in cafe " [:-D] 
Gate Keeper's Comment: 
"sir,first come to gate and lock up ur BIKE " 
[B-)]
Mom Commented: "stupiD,useless, if thers no need to take class then
just take some vegetable for dinner and come straight back to home "
:-/;-):-DB-)


Q?What is a makefiles 
/>makefiles are special format of
files that uses the make utility and helps to automatically
build and manage our
projects. 
/>check out at manual of make 
/>When we have several source files it is very tedious to type the
compiling command every time whenever we want to so to solve the
problem we have makefiles. 

Q?How makefiles are organized 
/>The makefile has the following format: 
Target:<dependencies>
<tab>system command 

Q?How do we use dependencies 
/>sometimes when we use multiple targets, we can use the dependencies
to suggest that which target has the preference over which. 
Example:

compile:
 echo compiling my application
 gcc -Wall main.c -o myWC 
install:compilethe compile target will be executed before install
 echo copying executable in the deploy directory 
 ….
clean: 
 echo clean object files 
 …...

Q?How do we use varibles in makefiles 
/>using variables we can make the option of changeable 
/>ex: if we want to change the compiler or compiler options we just
declare the variables for it 

CC = gcc ← variable declaration and initialization 
CFLAGS = -Wall
ONAME = myWC 
compile:
 echo compiling my application
 $(CC)$(CFLAGS) main.c -o $(ONAME) ← variable usage 
 …..

Q?How do we use the make command 
/>The make command will look for a
file named makefile in our directory and then execute it. 
/> if you have several makefiles, then we can execute them with the
command: make -f MyMakefile 

Q?How does the build process works in gcc 
/>Compiler takes the source files and outputs object files (.o)
: gcc -c main.c-c(compile) option just creates the object files
/>Linker takes the object files and creates and executable 
-gcc main.o -o executable_name 

Some special function for copying files 
int fgetc(FILE *stream)
/>gets the next character(an unsigned char) from the specified stream
and advances the position indicator for the stream.
char *fgets(char *restrict s, int n, FILE *restrict stream)
/>reads bytes from stream into the array pointed to by s,until n-1 bytes are
read, or a <newline> is read and transferred to s, or an
end-of-file condition is encountered. The string is then terminated
with a null byte. 
int fscanf(FILE *restrict stream, const char *restrict format,...)
/>reads bytes, interprets them according to a format, and stores the results
in its arguments
printf(const char *restrict format,.......);
/>printf a formatted string on standard output 
size_t fread(void *ptr,size_t size,size_t nmemb, FILE *stream)
/>reads nmemb elements of data, each of size bytes long, from the stream
pointed to by stream, storing them at the location given by ptr 
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
/>writes nmemb elements of data, each size bytes long, to the stream pointed
to by stream, obtaining them from the location given by ptr. 
/>they return the number of items successfully read or written(not the
number of characters)
/>if an error occurs, or the end-of-file is reached then the return value
is a short item count(or zero)
/>fread() does not distinguish between end-of-file and error, and callers must
use feof() and ferror() to determine which occurred. 
ssize_t read(int fildes, void *buf, size_t nbyte)
/>fildes: file descriptor 
/>attempt to read nbyte bytes from the file associated with the open fildes,
into the buffer pointer to by buf 
/>on success, the number of bytes read is returned(zero indicates the end
of file), and the file position is advanced by this number 
ssize_t write(int fildes, const void *buf, size_t nbyte) 
/>attempt to write nbytes from the buffer pointed to by buf to the file
associated with the open fildes
/>on success, the number of bytes written is returned (zero indicates
nothing was returned). 
/>on error -1 is returned 

Q?What is redirection 
/> it is a process in which we can redirect
standard streams to user-specified locations. 
> : redirect standard output to a file 
>&: redirect both standard output and standard error 
< : redirect standard input to a file/location 
>!: redirect standard output; overwrite file if it exists 
>&!:redirect standard output and standard error and overwrite file if it exists 
| : redirect standard output to another command(pipe)
>>: append the standard output 
>>&:append standard output and standard error 

Q?what is the format for changing permission 
/> chmod [who] <operator> <type> filename
who : u→ user 
  g →group 
  o →other 
  a →all
operator: + → add permission 
   - →remove permission 
   = →set permission
type :r, w, x 



Lab 2 : Makefile 

Thought of today : 
Studying Has Two Types 
1- Hard Subjects Which Cannot Be Studied. 
2- Easy Subject That Doesn't Need To be Studied ... ”
 
Learning goals: in this laboratory activity you will learn the
difference between text and binary files. You will also learn how to
write simple Makefiles for compiling C programs. 
Finally you will be familiar with redirection, permissions and recursive
version of cp ,mv , rm and chmod. 

Exercise 1 
1. Write a C program that shows on standard output a text file by using I/O
ANSI C functions (fgetc, fscanf, printf, fgets). The source filename
is specified as command line parameter. 
2. Write a Makefile contains only one compilation target to generate the
executable file from the previous source file. Compare the output
result of your command with the 'cat' 
command 
3. Is it possible to use your command to copy a text file? 
4. What’s the behavior of the program if the input file is a binary
file? (For instance an executable). 
Exercise 2 
1. Modify the previous Makefile adding a new target named install. The
target should create a directory named bin in the parent directory
and copy the executable file there. 
2. Modify the previous Makefile adding a new target named clean which
removes binary files from the current directory. 
3. Modify the previous Makefile adding a new target named distclean
which does the same task as the clean target but which also removes
the bin directory created before. 
Exercise 3 
1. Make a C program able to copy binary files. Use ANSI C functions fread and
fwrite. Is it possible to use this program to copy text files too? 
2. Modify the program you developed in the step 1 so that it is able to
copy binary files using POSIX functions open, read, write and close. 
3. Verify that both programs work by copying a binary file and running
diff on them.
Exercise 4 
1. Modify the Makefile in such a way that it is able to generate all the
executables, each one with a different name. The install target has
to copy all of them into the bin directory. 
2. Verify that both programs work by copying a binary file and running
diff on them. 
Exercise 5 
Create two text files file1.txt and file2.txt using your favorite editor. 
1. Using redirection create file3.txt that is composed by file2.txt content
and file1.txt content (take care of the order of the two contents) 
2. Append the content of file1.txt in the file2.txt 
Exercise 6 
1. Starting from root directory(/) copy the whole content of so_ex01
directory, you created in the first exercise, in the backup_so_es1 
2. Remove so_ex01 directory 
3. Restore so_ex01 and delete backup_so_es1 (hint: use mv recursive
command) 
Exercise 7 
1. Check for each file and directory, in your home dir , which kind of
permissions do you have 
2. Using recursive version of chmod remove executable permission of all the
file, directory and subdirectory present in so_es1 directory. Try to
access to some directory that doesn't have the executable
permissions. 
3. Restore the executable permission of all the file, directory and
subdirectory present in so_es1 directory. 
Summary Review the code for showing and copying text and binary files understanding
the differences. Review your Makefile understanding what a target is and how compilation
using Makefiles works. Try to figure out where your colleagues home
directory are located and which kind of permissions you have.

Comments

Popular Posts