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.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
CSE 303 Lecture 16 Multi-file (larger) programs
 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.
Compiling and Linking. Compiling is quite the same as creating an executable file! Instead, creating an executable is a multistage process divided into.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Object-Oriented Programming in C++
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.
Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.
CS 213 Fall 1998 Namespaces Inline functions Preprocessing Compiling Name mangling Linking.
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.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
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.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
ECE 103 Engineering Programming Chapter 7 Compiling C Programs Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
 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
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Object Oriented Programming COP3330 / CGS5409.  Class Templates  Bitwise Operators.
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.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Chapter 15: Writing Large Programs Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 15 Writing Large Programs.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Lecture 3 Translation.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
What Is? function predefined, programmer-defined
Executable program (p1)
INC 161 , CPE 100 Computer Programming
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Preprocessor C program → Modified C program → Object Code
Separate Compilation and Namespaces
Structures putting data together.
Separate Compilation.
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Separating Definition & Implementation
Comments, Prototypes, Headers & Multiple Source Files
Structures putting data together.
Code Organization CSCE 121 J. Michael Moore.
Classes.
Namespaces How Shall I Name Thee?.
C++ Compilation Model C++ is a compiled language
Writing Large Programs
C Preprocessor Seema Chandak.
Separate Compilation.
What Is? function predefined, programmer-defined
CSC 253 Lecture 15.
Executable program (p1)
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

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 files contain definitions of functions and variables. One source file must contain a definition of main (the program’s entry point).

Source Files Dividing a program into multiple files has several advantages: Related functions can be grouped together into a single file, making the structure of the program clearer. Each source file can be compiled separately. A single source file can be shared by several programs.

Header Files Header files contain information to be shared among several source files. Header files are a convenient place to put macro definitions. A header file named boolean.h might contain the following lines: #define BOOL int #define TRUE 1 #define FALSE 0 Other files can access these macro definitions by using the directive #include "boolean.h"

Header Files Putting macro definitions in a header file has several advantages: We don’t have to copy the definitions into each source file. Changing the definition of a macro requires only that we edit a single header file. We don’t have to worry about the possibility that different source files might contain different versions of the macro.

Protecting Header Files If a source file includes the same header file twice, compilation errors may result, depending on the contents of the header file. This problem is common when header files include other header files: header1.h includes header3.h header2.h includes header3.h prog.c includes header1.h and header2.h Header files can be protected against multiple inclusion: /* header3.h */ #ifndef HEADER3_H #define HEADER3_H … #endif

Compilation and Linking Before a program can be executed, its source files (not its header files) must be compiled to produce object files. The object files must then be linked. Compilation leaves “gaps” that are filled in during linking. For example, a source file may contain calls of functions that are not defined in that file. The linker checks that all necessary functions are present (either in a source file or in the library). Calls of functions in the C library are resolved by the linker; the compiler knows nothing about these functions.