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

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

N ATIONAL E NERGY R ESEARCH S CIENTIFIC C OMPUTING C ENTER 1 Mixed Language Programming on Seaborg Mark Durst NERSC User Services.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Introduction to C Programming
Introduction to C Programming
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Preparation for working in the CSIL Basic LINUX operations.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
Ar: Unix Archiver Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
CS Lecture 11 Outline Compiling C programs using gcc Archiving modules Using Makefiles Debugging using gdb Assignment 3 discussion Lecture 111CS.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
Computer Science 210 Computer Organization Introduction to C.
Introduction to Shell Script Programming
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
리눅스 : Lecture 5 UNIX 유틸리티 : text editor, compilation (make), …
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Programming With C.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
UNIT 13 Separate Compilation.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
cs33201 Chapter 12 C Programming Tools Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
Multiple File Compilation and linking By Bhumik Sapara.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
C P ROGRAMMING T OOLS. C OMPILING AND R UNNING S INGLE M ODULE P ROGRAM.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Ar: Unix Archiver Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
Computer Science 210 Computer Organization
C Programming Hardik H. Maheta.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
CS-3013 Operating Systems C-term 2008
CS1010 Programming Methodology
Introduction to C Topics Compilation Using the gcc Compiler
CS1010 Programming Methodology
Computer Science 210 Computer Organization
Lexical Elements, Operators, and the C Cystem
Loaders and Linkers: Features
Lexical Elements, Operators, and the C Cystem
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Appendix F C Programming Environment on UNIX Systems
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Makefiles, GDB, Valgrind
SPL – PS1 Introduction to C++.
Presentation transcript:

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

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

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); }

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

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.

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

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.

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

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

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

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.

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

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

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

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.

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.

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

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

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)

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)

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

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

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); }

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)

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.

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

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.

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