Compilation & Linking Computer Organization I 1 November 2009 ©2006-09 McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.

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
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
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.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Creating your first C++ program
Programming With C.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Computer Programming I Hour 2 - Writing Your First C Program.
C Basics Computer Organization I 1 May 2010 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell Labs.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
Chapter 2 part #1 C++ Program Structure
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
CMSC 1041 Introduction to C Creating your first C program.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Introduction to GCC Department of Radio Physics and Electronics, Calcutta University.
Introduction to C Topics Compilation Using the gcc Compiler
Week 3-4 Control flow (review) Function definition Program Structures
What Is? function predefined, programmer-defined
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Executable program (p1)
The Three Attributes of an Identifier
Functions Separate Compilation
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
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.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions, Part 1 of 3 Topics Using Predefined Functions
C Preprocessor(CPP).
Separating Definition & Implementation
Introduction to Classes and Objects
Comments, Prototypes, Headers & Multiple Source Files
Creating your first C program
Programming Fundamentals Lecture #3 Overview of Computer Programming
C++ Compilation Model C++ is a compiled language
Your first C and C++ programs
Writing Large Programs
Functions, Part 1 of 3 Topics Using Predefined Functions
What Is? function predefined, programmer-defined
Compiler vs linker The compiler translates one .c file into a .o file
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Executable program (p1)
Chapter 2 part #1 C++ Program Structure
SPL – PS1 Introduction to C++.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the first thing that happens is that the code is parsed and modified by a preprocessor. The preprocessor handles a collection of commands (commonly called directives), which are denoted by the character '#'. #include directives specify an external file (for now a C library file); the preprocessor essentially copies the contents of the specified file in place of the directive. We will see more interesting preprocessor directives later. #include... int main() { printf("Hello, world!\n"); return 0; } Contents of file stdio.h are copied here.

Compilation & Linking Computer Organization I 2 November 2009 © McQuain, Feng & Ribbens Typical C Program Organization // single file // a.h // a.c // b.h // b.c // z.h // z.c... // main.c For very small programs, code is often organized in a single source file; the most common convention uses the extension c for C source files. For more interesting C programs, the code is typically organized into a collection of header files (extension h ) and source files. In most cases, the header files contain only type declarations and function prototypes, while the c files contain the corresponding implementations. #include directives are used within both h and c files to “import” declarations as necessary.

Compilation & Linking Computer Organization I 3 November 2009 © McQuain, Feng & Ribbens Using Inclusion // main.c #include directives are used within both h and cpp files to “import” declarations as necessary: #include "A.h" A(); // call // A.h void A(); // A.c void A() { // do stuff } // B.h int B(int x); // B.c #include "B.h" int B(int x) { // do stuff } #include "B.h" int x = B(42);

Compilation & Linking Computer Organization I 4 November 2009 © McQuain, Feng & Ribbens Controlling Access Often, a.c file includes some functions (and other things) that are needed only in that file: // B.h int B(int x); // B.c #include "B.h" int B(int x) { // do stuff Helper(); } void Helper() {... } Here, B() calls Helper(), but no other functions outside of the file B.c do so… So, we put the declaration of Helper() inside B.c, before any calls to it… … and we do NOT declare Helper() in B.h : void Helper();

Compilation & Linking Computer Organization I 5 November 2009 © McQuain, Feng & Ribbens The Multiple Inclusion Problem // main.c #include "A.h" #include "B.h" It is possible to create some unfortunate situations: Here, the preprocessor will copy the text of B.h into main.c twice, once due to the inclusion in A.h and once due to the explicit inclusion in main.c. This scenario is difficult to avoid in large projects involving many individuals or teams. However, there is a simple fix using preprocessor directives: // A.h #include "B.h" // B.h void foo(); // B.h #ifndef B_H #define B_H void foo();... #endif

Compilation & Linking Computer Organization I 6 November 2009 © McQuain, Feng & Ribbens #include #include "rFibonacci.h" int main() { for (int i = 0; i < 50; i++) { printf("Fibo(%d) = %lld\n", i, rFibonacci(i)); } return 0; } Example: Fibonacci Here's a short program with a test driver, a Fibonacci function in a second file, and a header file containing the declarations needed for the Fibonacci function: #ifndef RFIBONACCI_H #define RFIBONACCI_H #include uint64_t rFibonacci(uint64_t n); #endif #include "rFibonacci.h" uint64_t rFibonacci(uint64_t n) { if ( n < 2 ) return 1; return rFibonacci(n - 1) + rFibonacci(n - 2); }

Compilation & Linking Computer Organization I 7 November 2009 © McQuain, Feng & Ribbens #include #include "rFibonacci.h" int main() { for (int i = 0; i < 50; i++) { printf("Fibo(%d) = %lld\n", i, rFibonacci(i)); } return 0; } Compilation We can compile either c file without the other (but not produce an executable): OK: the function call matches this declaration; compiler doesn't need to see function definition. The compiler notes that this call "binds" to this declaration.

Compilation & Linking Computer Organization I 8 November 2009 © McQuain, Feng & Ribbens Compilation -c switch tells gcc to compile and exit If compilation is successful, an object file is created; this is a partial translation of the C source file into machine code.

Compilation & Linking Computer Organization I 9 November 2009 © McQuain, Feng & Ribbens Linking The final step in producing an executable is to invoke the linker. The linker resolves the "notes" left by the compiler for external references (like the function name noted earlier), and writes the final executable file. With gcc, the simplest way to do this is to just invoke gcc on the object files…

Compilation & Linking Computer Organization I 10 November 2009 © McQuain, Feng & Ribbens Linking -o switch tells gcc what to call executable can invoke gcc with object files to link them and produce executable You'd want to use gcc-4

Compilation & Linking Computer Organization I 11 November 2009 © McQuain, Feng & Ribbens #include //#include "rFibonacci.h" int main() { for (int i = 0; i < 50; i++) { printf("Fibo(%d) = %lld\n", i, rFibonacci(i));... Compilation Errors The compiler will generate error messages if it cannot resolve the references it encounters: Omit this include directive and the compiler cannot find a declaration that matches the call to rFibonacci().

Compilation & Linking Computer Organization I 12 November 2009 © McQuain, Feng & Ribbens Compilation Errors The compiler will generate error messages if it cannot resolve the references it encounters: #ifndef RFIBONACCI_H #define RFIBONACCI_H #include uint64_t Fibonacci(uint64_t n); #endif Or, if the declaration doesn't match the call, the compiler will generate an error message.

Compilation & Linking Computer Organization I 13 November 2009 © McQuain, Feng & Ribbens #include #include "rFibonacci.h" int main() { for (int i = 0; i < 50; i++) { printf("Fibo(%d) = %lld\n", i, rFibonacci(i)); } return 0; } Linker Errors The linker may also produce error messages: #ifndef RFIBONACCI_H #define RFIBONACCI_H #include uint64_t rFibonacci(uint64_t n); #endif #include "rFibonacci.h" uint64_t Fibonacci(uint64_t n) { if ( n < 2 ) return 1; return rFibonacci(n - 1) + rFibonacci(n - 2); } OK Now, linker cannot find a definition (implementation) that matches the function declaration the compiler matched the call to… that's an error.

Compilation & Linking Computer Organization I 14 November 2009 © McQuain, Feng & Ribbens Linker Errors Unfortunately, linker error messages are less direct than compiler error messages: The message does not refer to a specific line of source code. But, it does name a specific function and it implies there's a difference between the declaration of that function and the definition (implementation) of that function.