C Module System C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Data Structure & Abstract Data Type
C Module System C and Data Structures Baojian Hua
CSE 303 Lecture 16 Multi-file (larger) programs
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Program Development Tools The GNU (GNU’s Not Unix) Toolchain The GNU toolchain has played a vital role in the development of the Linux kernel, BSD, and.
Linking & Loading CS-502 Operating Systems
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
Compiler Construction by Muhammad Bilal Zafar (AP)
C Module System C and Data Structures Baojian Hua
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Functions C and Data Structures Baojian Hua
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
C and Data Structures Baojian Hua
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
CS 201 Functions Debzani Deb.
COP4020 Programming Languages
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Enabling the ARM Learning in INDIA ARM DEVELOPMENT TOOL SETUP.
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.
Lecture-1 Compilation process
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
1 ENERGY 211 / CME 211 Lecture 26 November 19, 2008.
UNIT 13 Separate Compilation.
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.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
(language, compilation and debugging) David 09/16/2011.
CS 261 – Recitation 2 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science.
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
Slide Advanced Programming 2004, based on LY Stefanus's slides Native Methods.
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.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
Chapter 1 Basic Concepts of Operating Systems Introduction Software A program is a sequence of instructions that enables the computer to carry.
Program Libraries 1. What is a program library? A library is a collection of implementations of behavior, written in terms of a language, that has a well-defined.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
Sung-Dong Kim Dept. of Computer Engineering, Hansung University Chapter 3 Programming Tools.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Operating Systems A Biswas, Dept. of Information Technology.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Introduction to GCC Department of Radio Physics and Electronics, Calcutta University.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Lecture 3 Translation.
Week 3-4 Control flow (review) Function definition Program Structures
Slides adapted from Bryant and O’Hallaron
Executable program (p1)
Separate Compilation and Namespaces
Linking & Loading.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
Program Execution in Linux
CS-3013 Operating Systems C-term 2008
CS1010 Programming Methodology
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.
CS1010 Programming Methodology
مباني كامپيوتر و برنامه سازي
Linking & Loading CS-502 Operating Systems
C++ Compilation Model C++ is a compiled language
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
Linking & Loading CS-502 Operating Systems
Programming Languages and Paradigms
Subtype Substitution Principle
Executable program (p1)
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

C Module System C and Data Structures Baojian Hua

Software Systems are Large Practical software systems tend to be large and complex: Linux kernel consists of ~1000K LOC So the general principals in designing large (even small) software systems are: dividing into manageable smaller ones separating specification (interface) from code (implementation)

Module System Module systems offer a systematic method to organize software Different parts can be separately developed, compiled, tested, debugged Different parts are finally linked together This process evolves compiling, linking and libraries, etc.

Module System Different styles of module systems in languages: ML signature and structure Java interface and class C (C++) header files (.h) and C files (.c) This slide shows how to manage C ’ s module system: source programs (separate) compiling, linking, loading and tools

Typical C Program Organization Program file1 function1functionm … … … filen function1functionn dec & stm exp dec & stm exp dec & stm exp dec & stm exp

General Process // General process from source files (.c) to // executables (.exe): 1.c 2.c n.c 1.i 2.i n.i 1.o 2.o n.o libraries a.exe preprocessingCompiling linking ………

Example Revisited double area (int r); double area (int r) { double pi = 3.14; return (pi*r*r); } int main() { double f; f = area(5); return 0; }

First Try // main.c int main () { double f; f = area (5); return 0; } // area.c double area (int r); #define PI 3.14 double area (int r) { double f = PI *r *r; return f; } Try this demo! Wrong result? Why?Or even worse? f = area(5, 6, 7, 8, 9);

Second Try // main.c double area (int r); int main () { double f; f = area (5); return 0; } // area.c double area (int r); #define PI 3.14 double area (int r) { double f = PI *r *r; return f; } Try this demo! Is this perfect? What about here is files contains “ area ” and “ area.c ” being changed?

Third Try // area.h double area (int r); // main.c #include “area.h” int main () … // area.c #include “area.h” #define PI 3.14 double area (int r) { double f = PI *r *r; return f; } Try this demo! Is this perfect!?

Pitfalls // area.h double area (int r); int i = 0; // main.c #include “area.h” int main () … // area.c #include “area.h” #define PI 3.14 double area (int r) { double f = PI *r *r; return f; } Try this demo!

Final Version // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c #include “area.h” … // area.c #include “area.h” #define PI 3.14 double area (int r) { double f = PI *r *r; return f; }

Preprocessing Take source files (.c.h), generate intermediate files file inclusion Macro substitution comments removal … afterwards, no header file needed any more So, what ’ s the role of “.h ” files?

Example // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c #include “area.h” int main () { area (5); } // area.c #include “area.h” #define PI 3.14 double area (int r) { double f = PI*r*r; return f; }

Example // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c #include “area.h” int main () { area (5); } // area.c double area (int r); #define PI 3.14 double area (int r) { double f = PI*r*r; return f; }

Example // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c #include “area.h” int main () { area (5); } // area.c double area (int r); #define PI 3.14 double area (int r) { double f = 3.14*r*r; return f; }

Example // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c double area(int r); int main () { area (5); } // area.c double area (int r); double area (int r) { double f = 3.14*r*r; return f; }

Example // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c double area(int r); int main () { area (5); } // area.c double area (int r); double area (int r) { double f = 3.14*r*r; return f; }

Compiling Generate binary object files (.obj) object files in assembly or binary may involve several intermediate phases analysis optimizations … See demo …

Example // main.c double area(int r); int main () { area (5); } // area.c double area (int r); double area (int r) { double f = 3.14*r*r; return f; } // area.o // main.o

Linking Object files often called relocatable they are incomplete function names, extern variables, etc. Linking the process of linking all object files together resolve reference to external entities See demo …

Linking // Object files are incomplete: // main.o area(…) printf(…)

Linking // Resolve external references: // main.o call area call printf // area.o area: … // printf.o printf: …

Example // main.c double area(int r); int main () { area (5); } // area.c double area (int r); double area (int r) { double f = 3.14*r*r; return f; } // area.o // main.o // a.exe ……………

Static vs Dynamic Linking Static: all object files must be available and link together the generated.exe files are complete what ’ s the pros and cons? Dynamic: some object files are absent the generated.exe files are incomplete then how these absent object files are referenced? what ’ s the pros and cons?

What are Libraries? Libraries just are pre-written pre-compiled object files Normally offered by the compiler company For user program linking purpose Header files are available Ex: stdio.h, stdlib.h, ctype.h, …, from C standard library Source code available (ex: gcc), or unavailable (ex: vc) Same linking technique, but …

How to Implement Libraries? In order to familiarize you with libraries implementation techniques and others, we next study carefully an example stdio.h Our goal is to study the “ printf ” function int printf (const char *format, …);

General Strategy User program call the library function printf () printf () internally makes operating system call to do the real work details vary on different OS OS calls hardware driver Offered by hardware company user program libraries: printf() OS routines hardware driver

Case Study: Linux vs Windows fwrite () user program write () int 0x80 sys_write () Kernel fwrite () user program write () NtWriteFile () int 0x2e IoWriteFile () Kernel

API & SDK Application programming interface (API) a set of routines that nicely wrap up operating system calls libraries are built on top of them standard C libraries, runtime, etc. Why API? Software Development Kit (SDK) A collection of APIs header, libraries, files, and tools Varies on different Windows version

Runtime Runtime is a special set of libraries For program startup and exit get system info libraries preparation, etc. Normally NOT for called by user program Again, vary between different systems