Using open source GNU software on Unix
We will create lab activities for 3 to 4 weeks of work to learn how to use the emacs editor, the gcc compiler, the make utility, the CVS or RCS version control software, the GNU dbg debugger, and GNU timer and profiler. The labs could be fully used in CSCI170 or partially in CSCI 110, CSCI 140 and CSCI220-230
1. Login on your account on the Solaris Ultra 5 workstations. Ask your
instructor what is your username and your password.
Start an X terminal. Your instructor will show you
how to do this.
Inside this new window you will type commands just
like in DOS.
Change your passwd
if needed using the command passwd
Verify you are in your home directory with pwd (print working directory)
Create your own directory:
%mkdir myname (
myname will be your name)
Change your working directory:
%cd myname
%pwd
the % sign could actally be $ or > , it stands for
the prompt in a Unix shell
2. Start the emacs editor and create a new file named first.c
At prompt type :
%/opt/sfw/bin/emacs
first.c [enter]
Start typing some text (your first C program from
R&K):
#include<stdio.h>
main(){
printf(“
Hello World \n”);
return
0;
}
Hit [ Ctrl-x
Ctrl-s] (save)
then [ Ctrl-x Ctrl-c] to quit the emacs editor.
3. Now you willl compile and run this program.
First make sure you have the file first.c in your working directory.
Type ls or ls –l. You will get a listing of files in your
current directory, just like dir in DOS. You’ll see that first.c is there.
To compile using the GNU compiler gcc type:
%
/opt/sfw/bin/gcc first.c
This will create an executable a.out. If you are unhappy with this name and want to create the executable “myfirst”
type now
%
/opt/sfw/bin/gcc -o myfirst first.c
Check you
got all these files with ls –l.
4. Let us run the executable now.
Type ./a.out. Did it work?
Type now ./myfirst. If everything was done right this will work
too.
5. Start the emacs editor and create a new file named cppFirst.cpp (or cppFirst.C)
Note that, depending on compiler, the C++
file extension is either .cpp or .C
At prompt type :
%/opt/sfw/bin/emacs
first.cpp
[enter]
Start typing some text (your first C program from
R&K):
#include<iostream.h>
main(){
cout<<
“ Hello World ”<<endl;
return
0;
}
Hit [ Ctrl-x
Ctrl-s] (save)
then [ Ctrl-x Ctrl-c] to quit the emacs editor.
6. Now you will compile and run this program.
First make sure you have the file cppFirst.cpp in your working directory.
Type ls or ls –l. You will get a listing of files in your
current directory, just like dir in DOS. You’ll see that cppFirst.cpp is there.
To compile using the GNU compiler gcc type:
%
/opt/sfw/bin/g++ cppFirst.cpp
This will create an executable a.out. If you are unhappy with this name and want to create the executable myname type now
%
/opt/sfw/bin/g++ -o myCppFirst cppFirst.cpp
Check you
got all these files with ls –l.
7. Let us run the executable now.
Type ./a.out. Did it work?
Type now ./myCppFirst. If everything was done right this will work
too.
Using the “emacs” editor
You could proceed now with emacs. You start by typing in “emacs filename” .
Start,
save, exit and get help
Start -
emacs or emacs filename (or xemacs)
Start on line n – emacs +n filename
Save – ctrl-x ctrl-s ( or in short C-x C-s)
( we shall write C-x for ctrl-x ie control and x
pressed together)
Save as C-x
C-w
Insert file at point C-x i
Exit C-x C-c
Cursor
movements, undo:
Forward C-f
Backward C-b
Previous line C-p
Next line C-n
Beginning of line C-a
End of line C-e
Beginning of file M-<
( M is the meta key - marked with one of: Alt, esc or diamond)
End of file
M ->
To END a
command C-g
To undo C-x u or C-_ (underscore)
3.
Screen up/down, search, replace, mark region, position
Next screen
C-v
Previous screen M-v
Recenter C-l
Search forward C-s
to exit at that point ESC
To continue search C-s
Search backward C-r
Query-replace M-%
(respond interactively old/new)
Replace all answer ! for yes esc for no and exit
Mark region C-@
What cursor position C-x =
4.
Delete,
yank, copy and paste
Delete next char C-d
Backward delete char Del
Kill word M-d
Kill line C-k
Kill region =mark
beginning with C-@ then at end of region with C-w
Yank C-y inserts at point what was most recently
killed
M-w places region in kill ring(copy) without
deleting it
Linux notice: we may use emacs with Linux (RedHat/Fedora/Ubuntu/Suse etc) OS on the Dell PCs. You will log on the account : student. The Linux account is a local account, that means you won’t be able to access your home directory. The reason is that the Linux installation configured DHCP the NIS
( Name Information Services = global accounts, i.e. your individual accounts across the LAN ) works only with static IP’s. You could though use the typical Unix commands and get familiarized with Unix. Try the commands from the Unix handout, check out if they work?
Sun notice/ ftp :You may need to copy some files you may use ftp: mget , copies the files in the directory catalog from your account in your home directory. You may ftp on any SUN try: 140.144.46.212 (laplace) 140.144.46.205 ( newton), 140.144.46.206(einstein) or 140.144.46.211 (euler). Type ftp and use your username and your password. Check first you are in the right directory on both sides. Use pwd, !pwd, cd, lcd if needed. Set transfer type to int or bin. Get your files using the mget *. When done type “bye” to quit ftp. You can use the same two files created in the directory named “catalog”. “supply” and “word_by_word” to practice the basic operations using the emacs editor. On the Ultra 5 SUN workstation you need to run either /opt/sfw/bin/emacs or /opt/sfw/bin/xemacs.
8.Start the vi and create a new file named “first”
At prompt type :
vi first [enter]\
Hit the I key [insert mode]
Start typing some text
Hit [esc] key
Hit “:”
Type wq!
Now you are out.
See your file with:
More
first (or less first)
9. Create now a second file with:
vi second
Practice
changing modes using
I,
a, [esq], :
Look
at page 71 in the textbook or the class handout for vi
Try
to do the following:
Corrections with
- delete character
-
delete
line
-
delete
word
search
with
/ string
10.Start the vi and create an new directory named “catalog”. CD to catalog
and create a new file named “supply”
that look like this:
_ ( blank line)
writing
-- markers.
writing -- pencils.
writing -- pens.
_
_
_
correction
fluid
correction
pens
correction tape]
_
_
_
staplers – desktop
staplers – electric
staplers – specialty
…
11. Create a second file the following way:
cat >word_by_word
type in: Move (the) cursor; word-by-word not
word_by_word.
Hit <enter> twice
Hit <ctrl-d>
Use ls and
more to check on your files
12. Use the above files to practice the following vi commands about cursor movements:
h
move cursor right
5h move cursor right 5
times
l move cursor left
j
or + move cursor down
2j
move cursor down twice
k
move cursor up
$
move cursor to the end of the line
ctrl-h or < backspace>
overwrite previous character
13. Practice more on similar
things:
0 move cursor to the beginning of the
line
G move cursor to the end of file
1G move cursor to the first line (
beginning of file)
3G move cursor to line 3
J join current line with the next line
14. w / W move forward a small/big word
b / B moves one small/big word backwards
{
move forward a paragraph
}
move backward a paragraph
)
move forward a sentence
(
move backward a sentence
15.
Control screen:
ctrl-g display status line
ctrl –l clear status line
ctrl-u scroll half screen up
ctrl-d scroll half screen down
ctrl-f scroll full screen forward
ctrl-u scroll full screen backward
ctrl-e one line up
ctrl-y one line down
H (high)
–move cursor at the top of the screen
M (middle) –move cursor at the middle of the screen
L (low) –move cursor at the bottom of the screen
16. Try now : the 4 arrows:
up/down/left/right
pg-up/pg-down/ctrl-home/ctrl-end
17.
More:
o create a new blank line after the
cursor
O create a new blank line before the
cursor
18.
More commands to try:
u
undo last command
U undo changes on last line
x delete char at cursor (then use “u”)
dd delete line (then use “u”)
2dd delete 2 more lines (then use “u”)
dw delete word
D delete to the end of line
d0 delete to the beginning of line
dG delete to the end of file
19.
Searching and replacing:
/string search string forward
n continue search forward
? string search string backwards
N continue search backward
Y or yy yank a copy of a line
p put the yanked line below cursor
P put the yanked line above cursor
:s/old/new substitute new for old
once
:%s/old/new substitute new for old all
:s /old/new/g substitute on a line
:1,5 s/old/new/g substitute on lines 1
through 5
: w filename – save as filename
: 3,5d delete lines 3 to 8
:2,4 m 6 move lines 2-4 to line 6
:2,4 t 6 copy lines 2-4 at line 6
20.
More usefull commands:
: set nu show line numbers
: showmode show the command or input
mode
:!ls get to the shell command ls
:!command execute the shell command
vi –r filename
recover file after system interruption
vi +5 supply
- start on line 5 in file
“supply”
vi +pens
supply - start at first occurrence of string “pens”
ZZ to exit
set number
set showmode
Hit
[esc] :wq and check using more “filename”
su – Password: ( ask for help from your
instructor)
You
are going to create two files needed for the Internet connection to work:
/etc/defaultrouter containing:
140.144.46.1
/etc/resolv.conf content:
domain
mtsac.edu
nameserver
140.144.32.202
nameserver
140.144.32.205
search
mtsac.edu
Check
to see if Netscape works on Suns under Unix
Download elm laplace 140.144.46.212 root directory in .tar.gz
form from ftp.virginia.edu
)
Uncompress
with gunzip filename
Untar
with tar xvf filename
Read
the Readme file for instruction on how to build the elm application on your
computer, you still need to be the super-user ”root”.
You need to modify the Make file in order to use the gcc rather than cc compiler.
Lab 3 Compilers gcc/g++ and
the makefile utility
1.
Login on your account on a SUN Ultra 5 Workstation. Verify you are in your home
directory with pwd . Check you PATH with “ echo $PATH”.
Along several tasks you need
to type long “absolute PATH “ for commands like emacs, vi, cb, cc, gcc, CC, g++, make. At the appropriate time
modify the PATH variable in your ~/.profile shell configuration file so that you will not need any more to
type in the long absolute path.
Use the find command to find
the PATH for the above mentioned external shell commands.
Create a directory ctest1,
change directory to the newly created directory. Use a text editor ( vi or emacs) to enter the C code for the 2
programs power.c and compute .c from page 570 in textbook.
Compile now in the following
ways:
a) cc power.c compute.c
b) cc –c power.c
cc
-c compute.c
cc power.o compute.o –o power
A message error will occur in the linking phase .
Link with the math libray using the option –lm
cc power.o compute.o –lm –o power
Test the result by running “power”. In Bourne shell
use ./power
The makefile utility
Create a makefile like this:
power:
power.o compute.o
cc power.o
compute.o –o power -lm
power.o:
power.c
cc
-c power.c
compute.o
: compute.c
cc -c
compute.c
Try to run now the /usr/ccs/make with this makefile
( from the current directory). Touch now the source file power.c ( $ touch power.c ) and run make again. What
is the difference?
Use $cd .. , then create now a new directory ctest2, inside
create the 3 header files main.h,
input.h, compute.h, and the 3 C source files main.c,
compute.c and input.c from page 573.
a) Compile to tie together
all these 6 files in a single command to create the executable. Use a command
similar to 5a.
b) Compile now by first creating the object files and later linking like in 5b)
Write now a new makefile to be used by the make utility to perform
like in 8b).
Rename now the old makefile and
create a new one like the one on
the top of page 576. Read the make
rules carefully and make sure you
understand each rule used. Be ready to
explain where the predefined rules
came into to produce simplifications.
Use the make utility now. Update a
few files using the touch command to be able to watch make working.
Rename now the old makefile
and create a new one like the one on
the top of page 577. Read the make rules
carefully and make sure you
understand how each rule has changed due to the macros used. Be
ready
to explain everything. Use the make
utility now. Update a few files using
the touch command to be able to watch make working again.
We shall
examine the Makefile used to compile the learn
program downloaded from Brian Kernighan’s web page on Bell Lab’s site. Check you PATH with “ echo $PATH”. Examine the following
Makefile used for learn and try to
understand how it works. Try to use it, make a temporary copy of files in the learn directory and see what modifications are
required to make it work:
LESSONS = files editor morefiles
macros eqn C
CFLAGS = -g
LLIB =
../lib
cp: all
cp
learn tee lcount $(LLIB)
@echo
"Do 'make lessons' if you need to extract the lesson archives"
@echo
"Do 'make play; make log' to make playpen and log directories"
all: learn
tee lcount
learn: learn.c learn.h
cc
-o learn $(CFLAGS) '-DLLIB="$(LLIB)"' learn.c
lcount tee:
cc
$(CFLAGS) $@.c -o $@
lessons: $(LESSONS)
$(LESSONS):
-rm
-rf $(LLIB)/$@
mkdir
$(LLIB)/$@
(cd
$(LLIB)/$@; /usr/ccs/bin/ar -x ../$@.a)
play log:
-rm
-rf $(LLIB)/$@; mkdir $(LLIB)/$@; chmod +w $(LLIB)/$@
check:
-@test
-r $(LLIB)/tee || echo 'tee not present; make tee'
-@test
-r $(LLIB)/lcount || echo 'lcount not present; make lcount'
-@test
-r $(LLIB)/play || echo 'play directory not present; make play'
-@test
-r $(LLIB)/log || echo 'log directory not present; make log'
-@for
i in $(LESSONS); do test -r $(LLIB)/$$i/L0 || echo $$i not unarchived, make
$$i; done
clean:
rm
-rf lcount learn tee $(LLIB)/play $(LLIB)/log
Now we want to learn working with libraries and SCCS ( the Source
Code Control System. Find the binaries
for the Code Control System in a directory named ccs. Find the PATH for the ar
command to work with libraries. Modify
the PATH variable in your ~/.profile shell
configuration file so that you will not need any more to type in the long
absolute path for ar or ccs commands. Use the find command to find the PATH for the above mentioned commands.
Create a directory ctest3 to
be used for experiments today. Change directory to the newly created directory.
Use ftp to get the file dir10a and dir10 from the root directory in laplace (140.144.46.212) . Those files were created
as output for ls command ( like
ls>dir10), now read those 2 files to find what files
you nedd to get from laplace in your ctest3. (If I am corect these are: hello,
l2learn.tar, print_date.c, print_date.c, trim.c Makefile – forget about SCCS and RCS
those are directories and all.tar, s.hello.c, s.print_date.c,
s.print_date.h, s.trim.c – some more that you do not care about) . Use ftp on your username and passwd to get these files. Do the ftp in binary
mode (bin or int ) not in ASCII (txt).
Change directory for a short time to ctest2 where you still have
the object files main.o
input.o and power.o.
First create the mathlib.a library by executing the
makefile from page 581(from the previous lab). Verify its content with ar t mathlib.a
Now you can use the library and link it to main like
cc
main.c mathlib.a –o power2
17. Use now the librarian ar to create a new
library libmmath.a from the 2
object code files input.o and power.o
. To learn about ar read 20.5.5
pag 578-585 in the textbook or
better use the man command: man ar.
ar
r libmmath.a input.o compute.o
cc
main.c libmmath.a –o power3
18. Now use the
q option to append main.o to the libmmath.a
Use ranlib to create a symbol table for the libray.
Then use nm to display library
information. Then use ar t to look at the table of content and extract main.o and
delete input.o from the library. Here
are the steps:
ar q
libmmath.o main.o ( appends)
ar t libmmath.o
ranlib libmmath.a (creates library symbol
table - like ar s )
nm
-V
nm libmmath.a
ar x libmmath.a main.o
ar d libmmath.a input.o
nm libmmath.a
ar t libmmath.a
1. We are going to continue
work with the C programs you developed in the previous labs: main.c,
power.c input.c
and the corresponding header files main.h, input.h and power.h. create a new directory
ctest4 and
inside it create two subdirectories:
SCCS and RCS. Make copies of the above
mentioned C files in the two directories.
You need to add to your PATH the directories /usr/ccs/bin for sccs and /usr/local/bin for rcs
In the
SCCS subdirectory create SCCS history files for each of the C programs.
Use admin –i file.c s.file.c
Use
the get command to check out the mainobject files main.c input.o and power.o. First use
get –p
s.filename for read only mode. Use ls –al
* to see what you got.
Use get –e s.filename in edit mode.
Try to
open the same file again. What message do you get? Again look at the directory
with ls –al * and look at your files with
cat or more.
Use sact to check on
files opened with SCCS. Use
unget to
return file main.c.Use sact and ls
–al now.
Create
new versions 1.2 and 1.3 and a new release 2.1 of main.c now.
Make some modification each time like inserting a new printf or a new comment. For each modification make sure to add your comment to the modification when SCCS asks you to do so. Use delta to create the new versions. See also what the delta –n does.
9. We shall edit multiple versions of a file now. Check out for editing the 1.1 version with get –e r1.1 s.main.c . Then try to check out version 1.2. What happens. Rename the 1.1 copy to main2.c using the mv shell command and try again. Remember to move it back when you are done with the 1.2 version. Use sact s.main.c and ls –al * to see what is going on. Create now new branches and sequence 1.1.1.1 , 1.2.1.1. Use delta –r1.2 s.main.c and then mv main2.c main.c and delta main.c ( no need for version number now. Why?) Return the files checked out and open 1.2 again to create 1.2.1.2 by checking out in edit mode.
10. We shall create a new release with get –e r2 s.main.c
11. Use prs
s.main.c to
display the history of the SCCS file.
12. Create a new release: get –e –r2
s.main.c
delta s.main.c
13. Remove versions: rmdel –r
1.2.1.2
ls –al *
14. Combine SCCS files to produce the latest version
having all the
modifications. Start with prs
s.main.c to see the SCCS history.
The comb will create a shell script and you will use the
script to delete
the unnecessary versions of a file and compress it
comb –o s.main.c > comb.sh
ls –al *
chmod +x comb.sh
./comb.sh
prs s.main.c
15. Use the RCS utility in the RCS directory now.
There are two basic commands ci for check-in and co for check out . Create an
RCS file
ci
–input.c
ls –al
co –l input.c
co –l input.c (??)
ci –u input.c
co –l input.c
ci –r2 input.c
rlog input.c
16. Use the power.c file and the SCCS and RCS
files from ftp from laplace to practice more.
RCS and CVS
We are going to continue work with the C programs you developed in the previous labs in the directory ctest4. Create 2 new subfolders in your home directory: ctest5 and ctest6. Move the RCS subdirectory in ctest5 and create a CVS subdirectory in ctest5. Make copies of the above mentioned C files in the two new directories
18. Use the RCS utility ( set /usr/local/bin – where rcs resides on your PATH) in the RCS directory now. To register a file with RCS use the check-in :
ci input.c
ls
–al
19. To get an read only copy :
co input.c
Now to get a locked copy that you can edit:
co
–l input.c
Do a minor modification and check in again
ci
input.c
This is going to create a new version 1.2.
Repeat checking in and out and add modifications to create 1.3, 1.4,
release 2.1 and branches 1.3.1.1, 1.3.1.2. To force a version number for
an unmodified file use –f like
ci –f –r1.4 input.c
You also need the –r to create a new release:
ci
–r2 input.c
If you want to retain a read only copy of your working file at check-in
time use ci –u input.c. Otherwise the copy is removed, use ls
to see this.
To see the log of modifications ( revision log file) use:
rlog input.c
To remove unwanted versions :
rcs -oRANGE input.c
RANGE could be 1.2:2.1 or :1.3 ( beginning to 1.3) or 1.4: ( from 1.4 on)
To create an access list and to delete names
rcs –anames-lis1 filename
rcs –enames-list2 filename
Change a file descriptor
rcs –t
filename (or)
ci –t
filename
To associate a name :
rcs –nbetatest:2.1 filename
ci –n working filename
To asociate a state
( like Exp(merimental), Rel(ease), Stab(le), Obs(olete) to a version)
rcs –sExp:1.3 filename
We are going work with the C programs fibi.c, maxsort.c, qsort2.c qsort2d.c, qsort2f in the folder named lab5 in your home directory.
1. For all our sessions using gdb you need first to compile using the –g option . This causes the compiler to generate an augmented symbol table needed for debugging. Make sure you have /opt/sfw/bin/gcc set up in your PATH in your .profile , (or create a symbolic link to /opt/sfw/bin/gcc in /bin or /usr/bin.
% gcc
–g –o fibi fibi.c
% gcc
–g -o maxsort maxsort.c …etc…
2. Create a new C source file testp.c , compile it with the –g option:
/* testp.c to be used with the gdb
debug session */
#define
big 32000;
int
first(int);
int
second(int*);
main(){
int
a=1,b;
b=first(a);
}
int
first(int x){
int
y=2;
return
second(&y);
}
int second(int *y){
int
i,j=2,h=1,l[100]={1},*p, *q=y;
p=&j;
for(
i=0;i<BIG;i++){
h|=*p;
p++;
}
return
1;
}
3. Compile with debug options and run the executable. Use gdb to find the line generating the error.
%
gcc –o testp –g testp.c
%
testp
%
ls (notice the existence of
core)
%
gdb testp core
%
(gdb)list
% (gdb)where
% (gdb)quit
4. Learn more about gdb, use the commands at the gdb prompt:
help breakpoints; help delete; help
frame, help
files; help info; help internals; help aliases;
help obscure; help running; help step; help
stack; help status; help support; help user.
For all the above try abbreviations like: h b; h d; h f; ...
5. If your program terminates abnormally control returns to gdb, gdb will tell you why the program terminated. The backtrace gives a stack backtrace showing exactly what the program was doing when it crashed.
(You may also use where) . backtrace produces a list of all active
procedures and the arguments with which they were called starting with
the most recent.
gdb
testp
(gdb)
run
(gdb)
backtrace
(gdb)
frame
(gdb) up
(gdb) down
(gdb)
list
(gdb)
whatis p
type= int *
(gdb)
print p
$1=(int *) 0xffbf0000
(gdb)
print *p
cannot
access memory at address 0xffbf0000
(gdb)
print $1-1
$2=(int *) 0xffbefffc
(gdb) print *$2
$3= 0 (no more a memory violation)
6. Artificial arrays provide a way to print chunks of memory- array sections or dynamically allocatedarea of storrage.
(gdb) print h@10
$4={-1, 2,848,0,0,0,0,134720,-4262584,-1}
(gdb) print $4[8]@5
$5={-4262584,-1,8192,-12845056,0}
7. Running under gdb you may do indirection <, > and pass arguments like :
gdb qsort2f
(gdb) run <infile>outfile
for the file version of qsort2. We shall modify fibi to a pfibi
/*
pfibi is the command argument version of fibi */
main(int argc, char* argv){
int
n; . . .
if
(argc!=2) { printf( “ usage fibi n \n”);
exit(1);}
n=atoi(argv[1]);...(the rest of fibi code) }
gdb – q pfibi
(gdb)
run 5
(gdb)
show args
(gdb)
set args 10
(gdb)
r
(gdb)
q
To end running use CTRL-c(abreviated C-c) or(gdb)quit to quit .
You may try now to see what is the largest Fibonacci we may compute, my gues is 46.First you need to silence out the printf’s (all but one in main( ))
8. Type %gdb
–q maxsort (q means quiet).
To see your source file type:
(gdb)list or
(gdb)list 10,20 for lines 10 through 20 or
9. Now we are going to set breakpoints and watchpoints, and execute up to the first, then step one instruction at a time with either step ( does step I functions) or next steps over functions. After break stops you can use continue and finish ( inside a function) .
You could delete ( takes
breakpoints numbers) or clear ( this takes line #’s) breakpoints, also you
can disable or enable breakpoints. You
can use info b to get info on active breakpoints.
(gdb) breakpoint 24
(gdb) breakpoint 26
(gdb)list maxsort
(gdb) breakpoint maxsort
(gdb) breakpoint 36
(gdb) info breakpoints
(gdb) run
Breakpoint 1
(gdb) continue
Breakpoint 2
(gdb) step
(gdb) print i
(gdb) next
(gdb) print i
(gdb) clear 36
(gdb) set variable a[5]=a[1]+22
(gdb) do different runs to see differences
examine varibles with print and contrast step vs next. The next “defines a
script”
(gdb)
define mystep
step
step
print a[2]
continue
end
You may now take some time to use gdb on the other C programs in this directory.
1. We are going to use the profiling utilities on UNIX. We shall use the C programs fibi.c, maxsort.c, qsort2.c bsearch.c and gnu-1.2.tar.gz. Get them using ftp from the /lab5 directory on laplace (140.144.46.212) Create a new folder named lab6 in your home directory. Make copies of the above filers in ~/lab6.
2. Modify qsort2.c to get random data input - you can borrow the
random number array generation from maxsort.c. Create two
versions qsort2f using files and qsort2d, getting random input and no
files. You need to do some adjustment to bsearch2.c too, so that it
takes input that is sorted up. Make two versions of it bserch2f.c to
use an input from a file provided by qsort2f.c and bsearch2d.c
that uses random number generation and a call to qsort2d() as a
function to sort the array and avoid the file.
3. Use the time shell command ( try which time). You need first to compile, in this lab use gcc ( either use /opt/sfw/bin/gcc or set up your PATH in your .profile , or create a symbolic link to /opt/sfw/bin/gcc in /bin or /usr/bin, try all of these).
Then use the time command under each shell sh, bash, ksh, csh, for each of your executable created like ( except gnugo-1.2):
$ gcc
–o fibi fibi.c
$ time
fibi
$ gcc
–o maxsort maxsort.c;
$ time
maxsort and so on.
4. Recompile now all your files with the –pg option to be able to do the
profiling. For gnugo-1.2 you need to take some extra steps:
$ gunzip gnu-1.2.tar.gz
$ tar xvf gnu-1.2.tar
$ cd gnugo
$ emacs Makefile add CC= /opt/sfw/bin/gcc at beginning,
and
change complilation from $(CC) to $(CC) –pg
5. Run all the file compiled with –pg option , create the stats file gmon.out
$ ./fibi
$ gprof –a fibi gmon.out> fibiprof
$ more fibiprof
Remark. Under Windows you may use
Macromedia vtune as a profiler
Java, Pearl, Assembly
3. Explain how would you use the time shell command if you want to see how good is your fibi.c program and what do you learn by using it.
Do you need to compile with -g or –DDEBUG or not ( and why)?
4.What are the –o and –xO options for the compiler (both cc and gcc)
5. Give the compiler commands to create an executable called myprog from the source files prog1.c prog2.c the object prog3.o and use some math library functions in the prog2.c? What is the purpose for each command?
6. What are the three steps of the program development process? What are the main tasks performed at each step? Name the Unix tools that can be used for these tasks.
7. Show the syntax of a make rule
8. Exemplify the two most important predefined rules for make show how the make rules get simplified?
9. Write the advantages and disadvantages of automating the recompilation and re-linking process using the make utility as opposed to manually doing this task.
10.Compile now in the following ways:
a)
cc power.c compute.c
b) cc –c power.c
cc
-c compute.c
cc power.o compute.o –o power
A message error will occur in the linking phase . Link with the math libray using the option –lm
cc
power.o compute.o –lm –o power
Test the result by running power. In Bourne shell use ./power
11.Create
a makefile like this:
power:
power.o compute.o
cc power.o
compute.o –o power -lm
power.o:
power.c
cc
-c power.c
compute.o
: compute.c
cc -c
compute.c
Try to run now the /usr/ccs/make with this makefile ( from the current directory). Touch now the source file power.c ( $ touch power.c ) and run make again. What is the difference?
12.Consider
the following makefile:
OPTIONS -g –o
OBJECTS=
main.o helper.o compute.o
SOURCES=
main.c helper.c compute.c
HEADERS=
main.h compute.h
power:
main.c $(OBJECTS)
$(CC) $(OPTIONS) power $(OBJECTS) –lm
main.o:
main.c main.h compute.h
cc –c main.c
helper.o:
helper.c
cc –c helper.c
compute.o:compute.c
compute.h
cc –c compute.c
mathliba:
helper.o compute.o
ar rv mathlib.a helper.o compute.o
all.tar
: $(SOURCES) $(HEADERS) makefile
tar cvf -$(SOURCES) $(HEADERS)
makefile>all.tar
clean:
rm –i *.o
a)
List
the names of macros
b)
Names
of targets
c)
Files
that each target depends on
d)
Commands
for constructing the targets named in part b0
e)
Simplify
the make rules using predefined rules
f)
Expain
the rules for the targets mathlib, clean and all.tar