Tut 02
Tut 02
Dr. A. Sahu
Dept of Comp. Sc. & Engg.
Indian Institute of Technology Guwahati 1
• Logging
• Compiling multiple file in C/C++
– Creating Library, Static/Dynamic
• Makefile:
– Introduction
– Example and Example
– Rules
– Dependency
2
Logging
Logging
● A Log file is a file that records events occurs in a
operating system or other system software or
the messages between the different user on a
communication software.
● Logging is a act of keeping log files.
Significance
System admin need to know what is
happening on the system.
Logging
● A general purpose facility called syslog is used.
● Programme send their log entry to syslog by
consulting two configuration files
/etc/syslogd.conf and /etc/syslog.
● Syslog contain four basic terms:
○ Facility: Describe the type of application. For
example: mail, kernel, ftp etc.
○ Priority or Levels: Describe the importance of log
message. For example: emerg, crit, alert etc.
○ Selector: Combination of facility and levels.
○ Action: Whenever there is a match of selector, a
action is performed eg: message to log file, echo the
message to console.
Logging (Contd.)
● The syslog.conf controls where message are
logged. A typical syslog.conf look like this:
Group Management
● Three type of account in the linux system:
○ Root account:Have complete control of system.
○ System account: Have some system specific control. For
example: sshd account and mail account.
○ User account: Have interactive access to the system and
provide very limited access to critical data.
● To manage user and groups, four type of
administration files are needed:
○ /etc/passwd - Keep user account and password
information.
○ /etc/shadow - Hold encrypted password.
○ /etc/group - contain group information.
○ /etc/gshadow - Hold secure information related to groups.
8
//foo.c
int foo3x(int x){ • $ gcc –c foo.c
return 3*x;
} • $ gcc –c bar.c
//bar.c • $ gcc foo.o bar.o
int main(){
int x; • $./a.out
x=foo3x(10);
printf(“%d”,x);
return 0;
} 9
• Compiler in Action…
$gcc foo.c bar.c –o a.out
foo.c bar.c
run compiler preprocessor
foo.s bar.s
run assembler
(as)
foo.o bar.o
linker
a.out a.out = fully linked
executable
• Combines multiple relocatable object files
• Produces fully linked executable – directly
loadable in memory
• How?
– Symbol resolution – associating one symbol
definition with each symbol reference
– Relocation – relocating different sections of
input relocatable files
• Types –
– Relocatable : Requires linking to create
executable
– Executable : Loaded directly into memory for
execution
– Shared Objects : Linked dynamically, at run
time or load time
• Collection of concatenated object files – stored on
disk in a particular format – archive
• An input to Linker
– Referenced object files copied to executable
bar.o libm.a
foo.o libc.a
printf.o & fopen.o
Linker (ld)
fully linked executable
a.out object file
//foo.c int main(){//bar.c
int foo3x(int x){ int x;
return 3*x; x=foo3x(10);
} printf(“%d”,x);
return 0;
}
• $ gcc -c foo.c
• $ ar rcs libfoo.a foo.o //it create libfoo.a
• $ gcc bar.c -L. -lfoo
• $ ./a.out
14
• Addresses disadvantages of static libraries
– Ensures one copy of text & data in memory
– Change in shared library does not require
executable to be built again
– Loaded at run-time by dynamic linker, at
arbitrary memory address, linked with programs
in memory
– On loading, dynamic linker relocates text & data
of shared object
• Linker creates libfoo.so (PIC) from a.o and b.o
• a.out – partially executable – depend on libfoo.so
• Dynamic linker maps shared library into program’s
address space a.o b.o
Linker -fPIC
bar.o
libfoo.so (position
independent shared object)
Linker
Partially linked a.out
executable Loader
– dependency on fully linked
libfoo.so executable in
Dynamic linker
memory
Creating Dynamic Library
int main(){//bar.c
//foo.c
int x;
int foo3x(int x){
x=foo3x(10);
return 3*x;
printf(“%d”,x);
}
return 0;
• $gcc -c -fPIC foo.c }
• $gcc -shared -Wl,-soname,libfoo.so.1 -o
libfoo.so.1 foo.o
• $ gcc bar.c -L. –lfoo
• $ export LD_LIBRARY_PATH=.
• $ ./a.out
17
18
Introduction
• “make” utility in Unix
– Is one of the original tools designed by S. I. Fieldman of
AT&T Bell labs 1977.
• What is “make”?
– The tool is designed to allow programmers to
– efficiently compile large complex programs with many
components easily.
• You can place the commands to compile a program
in a Unix script
– but this will cause ALL modules to be compiled every time.
• The “make” utility allows us to only compile those
– that have changed and the modules that depend upon
them.
Motivation
• Small programs single file