

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Objectives and contents
1
Linux is not Unix
Unix was developed at Bell laboratory, it be- comes a popular multiuser, multitasking operating system for a wide variety of dif- ferent hardware platforms.
Linux is a freely distributed implementation of a Unix-like kernel, the low level core of an operating system.
Linux contains no Unix code, it takes Unix as a model, it is a rewrite based on POSIX standard.
GNU project
Linux community supports the concepts of free software, i.e. software that is free 5
usr/local is a good point to compile pro- grams, to access required files, to up- grade the operating system.
usr/X11 is the X window system, which is commonly installed under this direc- tory.
type in this program, save it as hello.
#include<stdio.h>
int main()
{ printf(‘‘Hello World\n’’); exit(0); }
type in the two lines of commands, to see the r
gcc -o hello hello.c
./hello
A simple example:
cc -o fred fred.c /usr/lib/libm.a
or
cc -o fred fred.c -lm
Search for a specific library
cc -o x11fred -L/usr/opendwin/lib x11fred.c -lX
where -L indicates the location of libX11.
Library is a collection of object files kept together in a ready-to-use form.
The compiler and linker take care of com- bining the program code and the library into a single executable program. You must use the -l option to indicate which libraries.
Static Libraries : .a Shared Libraries: .so
When a program uses a shared library, it is linked in such a way that it doesn’t con- tain function code itself, but reference to shared code that will be made available at run time. When the program is executed, the function references are resolved and calls are made to the shared library, which will be loaded into memory if needed.
/* This is lib.h. It declares the function fred and bill*/ void bill(char *arg); void fred(int);
/* This is program.c */ #include ‘‘lib.h’’ int main() { bill(‘‘Hello World’’); exit(0); }
/* compile the function separately, test it */ cc -c program.c cc -o program program.o bill.o ./program
/* create our own library called libfoo.a */ ar crv libfoo.a bill.o fred.o
/* create a table contents for the library, make the library ready to use */ ranlib libfoo.a
/* use this library to link to program.o */ cc -o program program.o libfoo.a ./program
/* a shorthand */ cc -o program program.o -L. -lfoo
$ ls -l /bin/sh lrwxrwxrwx 1 root root 4 Sep 11 2001 /bin/sh -> bash
/check the version if you have bash/ $/bin/bash -version
GNU bash, version 2.04.21(1)-release (i386-redhat-linux-gnu) Copyright 1999 Free Software Foundation,Inc.
or /* invoke your bash command prompt */ $/bin/bash bash-2.04$ echo $BASH_VERSION 2.04.21(1)-release bash-2.04$ exit exit $
So, I have a Bourne Again Shell! What is yours? Probably the same. It is the Linux staple, from the GNU project .bash, has advantage that the source code is free available.
Type this command in an editor, we save it as a file called “first.sh”.
#!/bin/sh echo "Is it morning?Please answer yes/no" read timeofday
if [ $timeofday = "yes" ]; then echo ‘‘Good Morning’’ else echo ‘‘Good afternoon’’ fi exit 0
Execute this file as following:
$./first.sh Is it morning? Please answer yes or no yes Good Morning
$ ./first.sh Is it morning? Please answer yes or no
no Good afternoon
Type this command in an editor, we save it as a file called “foo”.
#!/bin/sh foo( ){ echo "Is it morning? Please answer yes or n read timeofday
if [ $timeofday = "yes" ]; then echo "Good Morning" else echo "Good afternoon" fi } echo "script starting" foo echo "script ended" exit 0
KISS (keep it small and simple)
Filter
Reusable-component
Flexibility
7
bill.c , fred.c
/*compile the functions */ cc -o bill.c fred.c /usr/lib/../crt1.o: In function ‘_start’: /usr/lib/../crt1.o: undefined reference to ‘main’ collect2: ld returned 1 exit status
error!!! the correct way: cc -c bill.c fred.c bill.o , fred.o
/* Create a header file */ lib.h = void bill(char *arg) ; void fred(int)
/* Create an executable file(which has main() function) / program.c = / This is program .c */
#include "lib.h" int main() { 8