Problem Session 4 Header Files and Unix. Where are they? You may need to examine the header file to determine what the data structures and functions provided.

Slides:



Advertisements
Similar presentations
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
CSE 303 Lecture 16 Multi-file (larger) programs
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Object-Oriented Programming in C++
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time.
C Hints and Tips The preprocessor and other fun toys.
UNIT 13 Separate Compilation.
Engineering Computing I Chapter 4 Functions and Program Structure.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
What Is? function predefined, programmer-defined
INC 161 , CPE 100 Computer Programming
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Functions Separate Compilation
CS1010 Programming Methodology
CS1010 Programming Methodology
Pre-processor Directives
Preprocessor C program → Modified C program → Object Code
Separate Compilation and Namespaces
Structures putting data together.
Separate Compilation.
C Preprocessor(CPP).
Separating Definition & Implementation
Comments, Prototypes, Headers & Multiple Source Files
CSc 352 An Introduction to the C Preprocessor
Code Organization CSCE 121 J. Michael Moore.
C++ Compilation Model C++ is a compiled language
Writing Large Programs
C Preprocessor Seema Chandak.
C Programming Language
What Is? function predefined, programmer-defined
Conditional Compilation
SPL – PS1 Introduction to C++.
Presentation transcript:

Problem Session 4 Header Files and Unix. Where are they? You may need to examine the header file to determine what the data structures and functions provided are. Sometimes having a book like P.J. Plauger: The Standard C Library is not quite enough - different versions of Unix may have subtly different versions of the libraries, and you may or may not be able to determine what you are working with, without looking "under the hood".. Since the files are invoked by #include you should be able to find them in the "sensible place": /usr/include...

Problem Session 4 Let's look at one - the one. It contains lots of information that we have to navigate through. There are function prototypes - without any explanation or documentation: you are expected to have a text or some other separate documentation to make any sense of what's there. Some of the function names "make sense", some others appear to be unpronounceable collections of letters.

Problem Session 4 When your program contains #include this is the header file it will "textually include" in the source code to be compiled. The presence of the <> symbols indicates this is a system file, and will be found in a directory set up by the system administrators, and known to our compiler. The system will maintain information about the need to link - and some intelligent linkers will link only the functions actually used, thus minimizing the amount of space your program uses.

Problem Session 4 What about the beginning lines? #ifndef _STRING_H_ #define _STRING_H_ #ifdef __cplusplus extern "C" { #endif They are certainly not like regular program code. Furthermore, if you look this up in Plauger's book in the hope of getting some clarification, you will find: #ifndef _STRING #define _STRING #ifndef _YVALS #include #endif

Problem Session 4 Then What? Plauger's book doesn't say much about the "C preprocessor" - you have to go to something like Harbison&Steele: C - A Reference Manual; or Kernighan&Ritchie: The C Programming Language. Essentially, every line beginning with # is going to be interpreted as providing some kind of directive or information to the compiler. We are familiar with #include ; we may even be familiar with something like #define MAX_VAL 100 which would introduce a "symbolic constant" with a value of this would simply mean that every occurrence of MAX_VAL in our source code will be replaced by 100 before compilation.

What about: #ifndef _STRING_H_ #define _STRING_H_ This would introduce a symbolic constant _STRING_H _, but without any associated value. Notice that the first line is a conditional: #ifndef (same as #if ! defined ) checks whether the compiler is aware of the existence of that symbolic constant. If it is, it will skip to the #endif. If not, it will declare it and perform (in this case) some other actions. Problem Session 4

Why would you do this? Symbolic constants can be defined in several different header files. At times you will need to include two or more files defining the same symbolic constant. To avoid possible conflicts, you check and do not repeat actions already taken. There is another problem: your main program and one of the modules you need, have to have access to common information. You put the information into a file, but you will have TWO ways this file is read into your source code at compilation. The system will object to having multiple definitions of functions and data structures.

Problem Session 4 You avoid the problem by having a symbolic constant associated with every header file you write, and you perform a check for compiler awareness of the symbolic constant: if the check fails, the header file is safe to be read in; if the check succeeds, it has already been read in, so you skip it.

#ifndef GRAPH_H #define GRAPH_H typedef struct vertex{ int Number; bool Visited; } Vertex; typedef struct edge{ Vertex From; Vertex To; } Edge; typedef struct graph{ int N_Vertices; /* maximum of 20 */ int N_Edges; /* maximum of 30 */ Vertex Vertices[20]; Edge Edges[30]; } Graph; #endif // GRAPH_H Problem Session 4 Example of #ifndef