CSC 253 Lecture 6.

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

CSE 303 Lecture 16 Multi-file (larger) programs
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
1 Building. 2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs make, a popular.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
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.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
UNIT 13 Separate Compilation.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Advanced Shell Tricks Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
Computer Engineering Rabie A. Ramadan Lecture 5.
CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
1 Building Jennifer Rexford. 2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
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.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
THE DOUBLE VARIABLE The double variable can hold very large (or small) numbers. The maximum and minimum values are 17 followed by 307 zero’s.
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
1 CS101 Fall 2001 Lecture 1 In order to write a program, you must first telnet to your pegasus account and login either from a Rutgers computer in a lab,
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 18 – Linking and Libraries.
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Introduction to C Topics Compilation Using the gcc Compiler
ENEE150 Discussion 04 Section 0101 Adam Wang.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
Executable program (p1)
Computer Science 210 Computer Organization
Compilation and Debugging
Compilation and Debugging
Functions Separate Compilation
GCC: the GNU Compiler Collection
CS1010 Programming Methodology
Computer Science 210 Computer Organization
CS1010 Programming Methodology
Lecture 07 More Repetition Richard Gesick.
Larger Projects, Object Files, Prototyping, Header Files, Make Files
Lecture 4B More Repetition Richard Gesick
CSC 253 Lecture 8.
Separate Compilation and Namespaces
CSC 253 Lecture 8.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
CSC 253 Lecture 6.
Defining methods and more arrays
CSC 253 Lecture 7.
Domain and Range.
CSC 253 Lecture 14.
CSE 303 Concepts and Tools for Software Development
In C Programming Language
Module 6 Working with Files and Directories
CS1100 Computational Engineering
CSC 253 Lecture 15.
Executable program (p1)
GCC: the GNU Compiler Collection
SPL – PS1 Introduction to C++.
Presentation transcript:

CSC 253 Lecture 6

Declarations vs. Definitions Why do we separate declarations from definitions? You can’t call a function unless it’s been previously declared; suppose f calls g and g calls f … You can compile with the declarations even if the code has been previously compiled. You might not want them to unless they bought a source license. What is the rule for placing declarations and definitions? What is “module-based programming” in C? Dividing functionality of the program into set of paired .h and .c files.

Our int_math module Let’s define a file that contains two functions pow(int base, int exp) fact(int n) The first function is essentially the same as we wrote in the 3rd week. Let’s start with the header file.

Our int_math.h What idiom do we need to use at the beginning of the file? Let’s write the rest of the code …

Out int_math.c Let’s check parameters for validity … Let’s code fact() recursively. Suppose we compile int_math.c; what will happen?

Separate Compilation We can compile int_math.c even without writing a main program. We just use the -c switch on our call to gcc This produces an object file that can be used later.

The Main Program Now let’s write the main program … (exercise) We can run the program by listing our source file, along with the precompiled object file. We can also compile both files together …

Here’s the shell commands we invoked # Suppose we compile int_math.c without a flag that says not to link ... lec6% gcc int_math.c Undefined first referenced symbol in file main /afs/bp.ncsu.edu/contrib/gcc281/lib/gcc-lib/sparc-sun-\ solaris2.8/2.8.1/crt1.o ld: fatal: Symbol referencing errors. No output written to a.out # To avoid this error, we need to use a -c flag on the compile; that means "compile-only" lec6% gcc -c int_math.c # Let's see what file this created for us .. lec6% ls -lt | head -5 total 16 -rw-r--r-- 1 efg ncsu 944 Sep 21 11:17 int_math.o -rw-r--r-- 1 efg ncsu 300 Sep 21 11:15 int_math.c -rw-r--r-- 1 efg ncsu 595 Sep 21 11:03 int_math.h # Suppose we now compile (only) our main program lec6% gcc our_main.c pow /var/tmp/cchBaadV1.o fact /var/tmp/cchBaadV1.o # Oh, we need to tell the compiler about the code we already compiled ... lec6% gcc our_main.c int_math.o lec6% a.out 7 to the 7th power is 823543. 7! = 5040 # Now lets "rm int_math.o" and see what happens ... lec6% rm int_math.o

Here’s the last part of the commands … lec6% gcc -pedantic-errors our_main.c int_math.o gcc: int_math.o: No such file or directory # So if the .o file isn't there, we need to compile the source again ... lec6% gcc -pedantic-errors our_main.c int_math.c lec6% a.out 7 to the 7th power is 823543. 7! = 5040 # Or, we can name the binary file that the compile produces ... lec6% gcc -pedantic-errors -o our_main our_main.c int_math.c lec6% our_main lec6%