Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope."— Presentation transcript:

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

2 Oct 2001ANSI C Under Unix (v1.0)2 Overview l A look at the differences between C and C++ l A review of C’s programming features Program structure Loops Decisions Arrays Functions Structures l The compilation process: Source, object and executable files. l Libraries: Archive and Shared. l Examples l Laboratory exercises

3 Oct 2001ANSI C Under Unix (v1.0)3 Example 1 /*Filename: prog1.c */ /*Author: M.T. Stanhope */ #include 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); }

4 Oct 2001ANSI C Under Unix (v1.0)4 Compiler - High level view Compiler Main source file (prog1.c) Executable (output) file a.out $ cc -Aa prog1.c (-Aa forces the compiler to obey ANSI C rules) $./a.out

5 Oct 2001ANSI C Under Unix (v1.0)5 Example 2 /*Filename: prog2.c */ #include 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.

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

7 Oct 2001ANSI C Under Unix (v1.0)7 Example 3 /*Filename: prog3.c */ #include 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.

8 Oct 2001ANSI 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

9 Oct 2001ANSI C Under Unix (v1.0)9 Compiler - Detailed view Main source file (prog3.c) Subprogram source file(s) (sum.c) Executable (output) file a.out CompilerLinker (ld) object file (prog3.o) object file(s) (sum.o) Compiler driver

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

11 Oct 2001ANSI 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.

12 Oct 2001ANSI C Under Unix (v1.0)12 Linking with libraries l A library is a file containing object code for subroutines and data that can be used by other programs. l 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 l 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

13 Oct 2001ANSI C Under Unix (v1.0)13 Example 4 /*Filename: mathsdemo1.c */ #include /* Required for prototypes of scanf() and printf() */ #include /* 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

14 Oct 2001ANSI C Under Unix (v1.0)14 Finding help on library routines l Use the man pages: man printf man scanf man pow

15 Oct 2001ANSI C Under Unix (v1.0)15 Archive and Shared Libraries l 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. l Shared Libraries When 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.

16 Oct 2001ANSI C Under Unix (v1.0)16 Archive and Shared Libraries l Almost all system libraries are available both as a shared library and as an archive library in the directory /usr/lib. l Archive libraries are files ending in.a l 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 l 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). l In addition to the system libraries, the user can create his/her own archive and shared libraries.

17 Oct 2001ANSI C Under Unix (v1.0)17 Archive Library Linker (Match external reference shown in main.o to function printf definition in libc then copy that portion of libc into 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

18 Oct 2001ANSI C Under Unix (v1.0)18 Shared Library Linker (Match external reference shown in main.o to function printf definition in libc then put path to printf routine file 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

19 Oct 2001ANSI 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 filesObject files cc -Aa -c file1.c file2.c file3.cAr 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)

20 Oct 2001ANSI 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)

21 Oct 2001ANSI C Under Unix (v1.0)21 Archive Library - Example l Create 3 function files (length.c, volume.c and mass.c) see coding on next slide. l Compile to create 3 object files cc -Aa -c length.c mass.c volume.c l Archive the object files to create an archive library file ar r libunits.a length.o mass.o volume.o l Check to see contents of archive (The t ‘key’ refers to table of contents) ar t libunits.a l Use the archive in creating a program (remember fn prototypes in main) cc -Aa convert.c libunits.a l Try running the program:./a.out

22 Oct 2001ANSI 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

23 Oct 2001ANSI C Under Unix (v1.0)23 Program coding - The main function Linker Archive Library (libunits.a) Executable (output) file a.out $ cc -Aa convert.c libunits.a $./a.out Compiler convert.o Other libraries (e.g. libc.a) /* Convert.c */ #include 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); }

24 Oct 2001ANSI C Under Unix (v1.0)24 Replacing, Adding and Deleting Object Modules l Replacing or Adding an object module ar r libunits.a length.o l Deleting an object module ar d libunits.a volume.o ar t libunits.a l Give note on compiling only (-c option)

25 Oct 2001ANSI C Under Unix (v1.0)25 Where are archive libraries kept? l Useful C library files need to be placed in a directory where other C programmers can access them. The 2 main choices are: l /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. l /usr/local/lib This directory usually holds libraries created locally by programmers on the system.

26 Oct 2001ANSI C Under Unix (v1.0)26 Shared Library - Example l Create 3 function files (length.c, volume.c and mass.c) l 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 l 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 l Use the shared library in creating a program (remember fn prototypes in main) cc -Aa convert.c libunits.sl l Try running the program:./a.out

27 Oct 2001ANSI 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.

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


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

Similar presentations


Ads by Google