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

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
CSE 303 Lecture 16 Multi-file (larger) programs
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
 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.
Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 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.
Computer Science 210 Computer Organization Introduction to C.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
chap13 Chapter 13 Programming in the Large.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
C Hints and Tips The preprocessor and other fun toys.
UNIT 13 Separate Compilation.
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.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Department of Electronic & Electrical Engineering Introduction to C - The Development cycle. Why C? The development cycle. Using Visual Studio ? A simple.
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.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
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 PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
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.
1 Building a program in C: Preprocessor, Compilation and Linkage.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
13 C Preprocessor.
What Is? function predefined, programmer-defined
Computer Science 210 Computer Organization
C #include <stdio.h> int main() { printf ("hello class\n");
CISC105 – General Computer Science
Alberto Fassò Endre Futó
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
CS1010 Programming Methodology
C #include <stdio.h> int main() { printf ("hello class\n");
C Basics.
CS1010 Programming Methodology
Pre-processor Directives
Computer Science 210 Computer Organization
Introduction to Programming
Preprocessor C program → Modified C program → Object Code
C Preprocessor(CPP).
Preprocessor.
Govt. Polytechnic,Dhangar
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
C++ Compilation Model C++ is a compiled language
C Preprocessor Seema Chandak.
C Programming Language
What Is? function predefined, programmer-defined
Executable program (p1)
Conditional Compilation
Presentation transcript:

Compilation and Debugging 101

Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)

Preprocessor A single-pass program that Include header files Expands macros Control conditional compilation Remove comments

#include directive add exp.. #include foo.h Include the file foo.h, from current directory #include Include the file stdio.h from the standard library directory (part of compiler installation)

Modules & Header files Complex.c struct Complex { double _real, _imag; }; Complex addComplex(Complex, Complex); Complex subComplex(Complex, Complex);... complex.h #include #include complex.h // implementation Complex addComplex(Complex a, Complex b) { … complex.c #include complex.h int main() { Complex c; … MyProg.c

Header files Header file contain Definition of data types Declarations of functions & constants That are shared by multiple modules. #include directive allows several modules to share the same set of definitions/declarations

#define directive #define FOO 1 … int x = FOO; is equivalent to … int x = 1;

#define with arguments #define square(x) x*x b = square(a); is the same as b = a*a;

#define -- cautions #define square(x) x*x b = square(a+1); c = square(a++); Is it what we intended? b = a+1*a+1; // b = 2*a+1; c = a++*a++; // c = a*a; a+=2;

#define #define directive should be used with caution Alternative to macros: Constants enum { FOO = 1; }; or const int FOO = 1;

#if directive Allows to have conditional compilation #if defined(DEBUG) // compiled only when DEBUG exists printf(X = %d\n, X); #endif

#if – header safety Complex.h: struct Complex { … MyStuff.h: #include Complex.h Main.c #include MyStuff.h #include Complex.h Error: Complex.h:1: redefinition of `struct Complex'

#if – header safety Complex.h (revised): #if !defined(COMPLEX_H) #define COMPLEX_H struct Complex { … #endif Main.c: #include MyStuff.h #include Complex.h // no error this time

Preprocessor We can test what the preprocessor does > gcc –E hello.c will print the C code after running preprocess

Compilation Takes input C-code and produces machine code (object file) –gcc –c Main.c –Main.c Main.o The object file does not contain all external references –It leaves names, such as printf, addComplex, etc. as undefined references

Linking Combines several object files into an executable file –No unresolved references Main Preprocessor Compiler Complex.c Complex.o Main.c Main.o Linker libc.a

Link errors The following errors appear only at link time Missing implementation > gcc -o Main Main.o Main.o(.text+0x2c):Main.c: undefined reference to `foo' Duplicate implementation > gcc -o Main Main.o foo.o foo.o(.text+0x0):foo.c: multiple definition of `foo' Main.o(.text+0x38):Main.c: first defined here

assert.h #include // Sqrt(x) - compute square root of x // Assumption: x non-negative double Sqrt(double x ) { assert( x >= 0 ); // aborts if x < 0 … If the program violates the condition, then assertion "x >= 0" failed: file "Sqrt.c", line 7

assert.h Important coding practice Declare implicit assumptions Sanity checks in code Check for violations during debugging/testing Can we avoid overhead in production code?

assert.h #undef assert // procedure that actually prints error message void _assert(char* file, int line, char* test); #ifdef NDEBUG #define assert(e) ((void)0) #else #define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e)) #endif