oct 2001ansi c under unix (v1.0)1 unix c programming under unix written and presented by...

28
Oct 2001 ANSI C Under Unix (v1.0) 1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Upload: job-bruce

Post on 17-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 1

UNIX

C Programming under Unix

written and presented byM.T.Stanhope

Page 2: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 2

Overview

A look at the differences between C and C++ A review of C’s programming features

• Program structure• Loops• Decisions• Arrays• Functions• Structures

The compilation process:• Source, object and executable files.

Libraries:• Archive and Shared.

Examples Laboratory exercises

Page 3: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 3

Example 1

/*Filename: prog1.c */

/*Author: M.T. Stanhope */

#include <stdio.h>

main()

{

int x=0, y=0, sum=0;

printf(“\nPlease enter an integer value”);

scanf(“%d” , &x);

printf(“\nPlease enter an integer value”);

scanf(“%d” , &y);

sum = x + y;

printf(”\nThe sum of %d and %d = %d” , x, y, sum);

}

Page 4: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 4

Compiler - High level view

CompilerMain source file

(prog1.c)

Executable (output) file

a.out

$ cc -Aa prog1.c (-Aa forces the compiler to obey ANSI C rules)

$ ./a.out

Page 5: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 5

Example 2

/*Filename: prog2.c */

#include <stdio.h>

int calcsum(int, int); /* Function prototype */

main()

{

int x=0, y=0, sum=0;

printf(“\nPlease enter an integer value”);

scanf(“%d” , &x);

printf(“\nPlease enter an integer value”);

scanf(“%d” , &y);

sum = calcsum(x,y); /* Invoke subfunction */

printf(”\nThe sum of %d and %d = %d” , x, y, sum);

}

/*Function to add 2 numbers and return the result */

int calcsum(int a, int b)

{

int s = 0;

s = a + b;

return s;

}

The program is written as two functions held in the same file. The program is compiled and executed in the same way as before.

Page 6: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 6

Notes

Page 7: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 7

Example 3

/*Filename: prog3.c */

#include <stdio.h>

int calcsum(int, int); /* Function prototype */

main()

{

int x=0, y=0, sum=0;

printf(“\nPlease enter an integer value”);

scanf(“%d” , &x);

printf(“\nPlease enter an integer value”);

scanf(“%d” , &y);

sum = calcsum(x,y); /* Invoke subfunction */

printf(”\nThe sum of %d and %d = %d” , x, y, sum);

}

/*Filename: sum.c */

/*Function to add 2 numbers and return the result */

int calcsum(int a, int b)

{

int s = 0;

s = a + b;

return s;

}

The program is now shown made up of two separate files. One holding the main function and the other a subfunction.

Page 8: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 8

Compiler - High level view

Compiler

Main source file

(prog3.c)

Subprogram source file(s)

(sum.c)

Executable (output) file

a.out

$ cc -Aa prog3.c sum.c

$ ./a.out

Page 9: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 9

Compiler - Detailed view

Main source file

(prog3.c)

Subprogram source file(s)

(sum.c)

Executable (output) file

a.outCom

pile

r

Lin

ker

(ld)

object file

(prog3.o)

object file(s)

(sum.o)

Compiler driver

Page 10: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 10

C compilation commands

Command Description

cc –Aa prog1.c Compile one complete program file to createan executable named a.out

cc –Aa prog1.c –o prog1 Compile one complete program file to createan executable (output) named prog1

cc –Aa –v prog1.c Compile one complete program file and turnon verbose mode so that the user can seethat the compiler driver (/opt/ansic/bin/cc)automatically invokes the following:C Preprosesor: /opt/langtools/lbin/cpp.ansiC compiler: /opt/ansic/lbin/ccomLinker: /usr/ccs/bin/ld

cc –Aa prog3.c sum.c Compile two program files to create anexecutable file named a.out

cc –Aa prog3.c sum.c –o prog3 Compile two program files to create anoutput (executable) file named prog3

Page 11: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 11

What is an object file?

An object file is a file containing machine language instructions and data in a form that the linker can use to create executable program code.

We cannot read the object file using a text editor as its contents are not held as ASCII characters.

Page 12: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 12

Linking with libraries

A library is a file containing object code for subroutines and data that can be used by other programs.

The library named libc is required for input/output functions to work and is automatically linked into your programs by the linker.• Other libraries must be linked in by you when needed, the

example below shows how the maths library named libm is linked in using the -lm option.cc -Aa mathsdemo.c -lm

By default, the linker program (ld) searches for libraries whose paths are stated in the environment variable named LPATH. LPATH is currently set to:/usr/lib/pa1.1:/usr/lib:/opt/langtools/lib

Page 13: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 13

Example 4

/*Filename: mathsdemo1.c */

#include <stdio.h> /* Required for prototypes of scanf() and printf() */

#include <math.h> /* Required for prototypes of pow() and sqrt() functions */

main()

{

float x=0, y=0, z=0;

printf(“\nPlease enter length of triangle side”);

scanf(“%f” , &x);

printf(“\nPlease enter length of triangle side”);

scanf(“%f” , &y);

z = sqrt( pow(x,2) + pow(y,2) );

printf(”\nA right angled triangle with sides %f and %f has hypotenuse of %f” , x, y, z);

}

This program is compiled by:

$ cc -Aa mathsdemo1.c -lm

Page 14: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 14

Finding help on library routines

Use the man pages:• man printf• man scanf• man pow

Page 15: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 15

Archive and Shared Libraries

Archive Libraries When linking a program object file with an archive library, the linker copies object code from the library into the resulting executable file (a.out). This makes the executable large but a self contained program.

Shared LibrariesWhen linking a program object file with a shared library, the linker does not copy object code from the library into the a.out file, instead it places code that calls routines held in the external shared library. The a.out file is referred to as an ‘incomplete executable’. The a.out file is much smaller using shared libraries but requires access to the external shared library.

Page 16: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 16

Archive and Shared Libraries

Almost all system libraries are available both as a shared library and as an archive library in the directory /usr/lib.

Archive libraries are files ending in .a Shared libraries are files ending in .sl

• For example, the C standard library libc is available in an archive library at /usr/lib/libc.a or as a shared library at /usr/lib/libc.sl

If both versions of a library exist, the linker (ld) uses the one that it finds first in the default library search path (see LPATH). If both versions exist in the same directory, ld uses the shared version (although this behaviour can be overriden).

In addition to the system libraries, the user can create his/her own archive and shared libraries.

Page 17: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 17

Archive Library

Linker

(Match external referenceshown in main.o

to function printf definition in libc then

copy that portion of libcinto a.out)

Call to printf function resides here

Executable file a.out (includes code for printf routine)

/usr/lib/libc.a

main.o

printf object code resides here.

Call to printf

printf defined

Page 18: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 18

Shared Library

Linker

(Match external referenceshown in main.o

to function printf definition in libc then

put path to printf routinefile in a.out)

Call to printf function resides here

Executable file a.out (does not include code for printf)

/usr/lib/libc.sl

main.o

printf object code resides here.

Call to printf

Page 19: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 19

Creating an archive library

Compiler

file1.c

file2.c

file3.c

Archiver

file1.o

file2.o

file3.o

Symbol table

file1.o

file2.o

file3.o

Archive library (libtest.a)Source files Object files

cc -Aa -c file1.c file2.c file3.c Ar r libtest.a file1.o file2.o fileo.c

To create a library of 3 useful user-defined functions each written in a separate source file (file1.c, file2.c and file3.c)

Page 20: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 20

Using the Archive library

Linker

(main.c)

Archive Library

(libtest.a)

Executable (output) file

a.out

$ cc -Aa main.c libtest.a

$ ./a.out

Compiler(main.o)

Other libaries

(e.g. libc.a)

Page 21: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 21

Archive Library - Example

Create 3 function files (length.c, volume.c and mass.c) see coding on nextslide.

Compile to create 3 object files cc -Aa -c length.c mass.c volume.c

Archive the object files to create an archive library filear r libunits.a length.o mass.o volume.o

Check to see contents of archive (The t ‘key’ refers to table of contents)ar t libunits.a

Use the archive in creating a program (remember fn prototypes in main)cc -Aa convert.c libunits.a

Try running the program:./a.out

Page 22: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 22

Program coding - The functions

length.c

volume.c

mass.c

archiver libunits.a

float in_to_cm(float in)

{

return ( in * 2.54);

}

float gal_to_litres(float gal)

{

return ( gal * 3.79);

}

float oz_to_gr(float oz)

{

return ( oz * 28.35);

}

compiler

length.o

volume.o

mass.o

cc -Aa -c length.c volume.c mass.c

ar r libunits.a length.o volume.o mass.o

ar t libunits.a

Page 23: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 23

Program coding - The main function

LinkerArchive Library

(libunits.a)

Executable (output) file

a.out

$ cc -Aa convert.c libunits.a

$ ./a.out

Compilerconvert.o

Other libraries

(e.g. libc.a)

/* Convert.c */#include <stdio.h>

float in_to_cm(float);

float gal_to_litres(float);

float oz_to_gr(float);

main()

{

float test=1.0, ans=0.0;

ans = in_to_cm(test);

printf(“\n1.0 in = %f cm” , ans);

ans = gal_to_litres(test);

printf(“\n1.0 gal = %f litres”, ans);

ans = oz_to_gr(test);

printf(“\n1.0 oz = %f grams”, ans);

}

Page 24: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 24

Replacing, Adding and Deleting Object Modules

Replacing or Adding an object module• ar r libunits.a length.o

Deleting an object module • ar d libunits.a volume.o • ar t libunits.a

Give note on compiling only (-c option)

Page 25: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 25

Where are archive libraries kept?

Useful C library files need to be placed in a directory where other C programmers can access them. The 2 main choices are:

/usr/lib• The linker searches /usr/lib by default. Placing library files here

would allow a user to omit the library pathname when compiling.• System Admin privileges are required to access /usr/lib as

overwriting an existing library by mistake would be possible.

/usr/local/lib• This directory usually holds libraries created locally by

programmers on the system.

Page 26: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 26

Shared Library - Example

Create 3 function files (length.c, volume.c and mass.c)

Compile the source files using the +z compiler option to create 3 POSITION INDEPENDENT CODE (PIC) object files cc -Aa -c +z length.c volume.c mass.c

Create a shared library by using the linker with the -b option. By default the shared library will be called a.out so use the -o option to rename the library:ld -b -o libunits.sl length.o volume.o mass.o

Use the shared library in creating a program (remember fn prototypes in main)cc -Aa convert.c libunits.sl

Try running the program:./a.out

Page 27: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 27

ar Reference Info

When used to create and manage archive libraries, ar’s syntax is:

ar [-]keys archive [modules]

where:

archive is the name of the archive library

modules is an optional list of object modules or files

The following table defines some useful keys. For a full list see the man page for ar.

d - Delete the modules from the archive.

r - Replace or add the modules to the archive. If archive exists, ar replaces modules specified on the command line. If archive doesn’t exist, ar creates a new archive containing the modules.

t - Display a table of contents for the archive.

u - Used with the r, this modifier tells ar to replace only those modules with creation dates later than those in the archive.

v - Display verbose output.

x - Extracts object modules from the library. Extracted modules are placed in .o files in the current directory.

Page 28: Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope

Oct 2001 ANSI C Under Unix (v1.0) 28

Notes