Intermediate Programming, Spring 2017*

Similar documents
2 Compiling a C program

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring Misha Kazhdan

Intermediate Programming, Spring 2017*

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

CS240: Programming in C. Lecture 2: Overview

[Software Development] Makefiles. Davide Balzarotti. Eurecom Sophia Antipolis, France

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

Makefiles SE 2XA3. Term I, 2018/19

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

PRINCIPLES OF OPERATING SYSTEMS

GNU Make 1. 1 material adapted from GNU Make by Richard Stallman

Reviewing gcc, make, gdb, and Linux Editors 1

Make! CSC230: C and Software Tools. N.C. State Department of Computer Science. Some examples adapted from

Intermediate Programming, Spring 2017*

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

make and makefile CS 211

independent compilation and Make

Intermediate Programming, Spring 2017*

15213 Recitation Section C

The makeutility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

Intermediate Programming, Spring 2017*

C introduction: part 1

CS Programming In C

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

Intermediate Programming, Spring 2017*

GCC: the GNU Compiler Collection

GCC: the GNU Compiler Collection

Introduction to C An overview of the programming language C, syntax, data types and input/output

The make Utility in Unix SEEM

Intermediate Programming, Spring 2017*

Reliable C++ development - session 1: From C to C++ (and some C++ features)

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Software Development With Emacs: The Edit-Compile-Debug Cycle

Lab 2: More Advanced C

cc is really a front-end program to a number of passes or phases of the whole activity of "converting" our C source files to executable programs:

Intermediate Programming, Spring 2017*

Systems Programming. laboratory. Compilation automation. make ceedling

CS201 - Lecture 1 The C Programming Language

Week 5 Lecture 3. Compiler Errors

Introduction: The Unix shell and C programming

CS 247: Software Engineering Principles. Modules

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

CSE 333 Lecture 6 - data structures

C - Basics, Bitwise Operator. Zhaoguo Wang

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Lab 2.2. Out: 9 February 2005

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

A Fast Review of C Essentials Part II

The make Utility in Unix SEEM

COSC345 Software Engineering. Make

Compilation & linkage. Compilation & linkage. Make. Compilation & linkage. Explicit rules. What makefile contains

Chapter 11 Introduction to Programming in C

CSE 333 Midterm Exam 7/27/15 Sample Solution

Exercise Session 2 Systems Programming and Computer Architecture

CSC 2500: Unix Lab Fall 2016

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Chapter 11 Introduction to Programming in C

CS240: Programming in C

Creating, Compiling and Executing

Follow us on Twitter for important news and Compiling Programs

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Data and File Structures Laboratory

CS2141 Software Development using C/C++ Compiling a C++ Program

The make utility. Alark Joshi COMPSCI 253

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Chris' Makefile Tutorial

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSE 390 Lecture 8. Large Program Management: Make; Ant

(T) x. Casts. A cast converts the value held in variable x to type T

And You Thought There Couldn t be More C++ Fundamentals of Computer Science

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Chapter 11 Introduction to Programming in C

Tutorial 1 C Tutorial: Pointers, Strings, Exec

PROGRAMMAZIONE I A.A. 2017/2018

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

IEEE 754 Floating-Point Format

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Topic 6: A Quick Intro To C

BİL200 TUTORIAL-EXERCISES Objective:

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Computers Programming Course 5. Iulian Năstac

Deep C (and C++) by Olve Maudal

Chapter 11 Introduction to Programming in C

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Programming in C week 1 meeting Tiina Niklander

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Chapter 11 Introduction to Programming in C

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Introduction to Supercomputing

Problem 1 (Void * generics) (24 points) (suggested time: 30 minutes)

Fundamentals of Programming Session 12

Transcription:

600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general and you should not follow my lead on this.

Outline Floats From code to executables Header files Independent compilation and linking make and Makefiles

Integer value representation Recall: On most machines, ints are represented using 4 bytes (32 bits) These encode the integer in binary (mod 2 32 ), using 32 digits of precision

Floating point value representation ±b 2 e On most machines, floats are represented using 4 bytes (32 bits) These are (roughly) used to encode: The sign (±): 1 bit The signed (integer) exponent (e): 8 bits * The unsigned (integer) base (b): 23 bits * This is what makes the point float

Floating point value representation [WARNING]: Adding floating point values requires aligning their precisions first* b 1 2 e 1 + b 2 2 e 2 b 1 2 e 1 e 2 2 e 2 + b 2 2 e 2 b 1 /2 e 2 e 1 + b 2 2 e 2 If b 1 is less than 2 e 2 e 1, then b 1 /2 e 2 e 1 will become zero Addition of floating points may not associative: a + b + c! = a + (b + c) *Assume e 2 > e 1

Floating point value representation [WARNING]: Adding floating point values requires aligning their precisions first* #include <stdio.h> b 1 2 e 1 + b 2 2 e 2 int main( void ) { b 1 2 e 1 e 2 2 e 2 + b 2 2 e 2 float a = 1e-4f, b = 1e+4f, c = -b; printf( "%.3e %.3e b 1 2 e %.3e\n" 1 e 2, a, b, c + b 2 2 e ); 2 printf( "%.3e %.3e\n", ( a + b ) + c, a + ( b + c ) ); If b 1 2 e 1 e 2 return is too 0; small to be represented it will become zero } Addition of floating points >>./a.out may not associative: 1.000e-04 1.000e+04-1.000e+04 a + b + c! = a + (b + c) 0.000e+00 1.000e-04 >> *Assume e 2 > e 1

Floating point value representation ±b 2 e On most machines, doubles are represented using 8 bytes (64 bits) These are (roughly) used to encode: The sign (±): 1 bit The signed (integer) exponent (e): 11 bits The unsigned (integer) base (b): 52 bits

Floating point value representation On most machines, ints are represented using 4 bytes (32 bits) On most machines, floats are represented using 4 bytes (32 bits) In order to interpret the 4 bytes as a number, we need to know the type! #include <stdio.h> int main( void ) { float f = 1.f; float *f_ptr_as_float_ptr = &f; int *f_ptr_as_int_ptr = (int*)f_ptr_as_float_ptr; int i = *f_ptr_as_int_ptr; printf( "%.2f %d\n", f, i ); return 0; } >>./a.out 1.00 1065353216 >>

Outline Floats From code to executables Header files Independent compilation and linking make and Makefiles

Source code Separate source files Big software projects are typically split among multiple files Code accomplishing related tasks is often grouped together (forming a library of functions) Different developers may create/edit/test different pieces

Header files Q: How do different files in a software package communicate? A: When compiling functions in one file, we need the declarations of functions in the other file We could include the declarations at the beginning of the file This causes code bloat and makes it hard to see what the code is doing We gather declarations in header (.h) files and then #include the header files A separate source (.c) file will contain definitions for functions declared in the header file Typically, functions defined in file-name.c are declared in a function named file-name.h We use #include <file-name.h> when the header file is part of the general library We use #include file-name.h when the header file is ours

Header files #include "func.h float mult2add( int x, float y ) { return mult2( x ) + y; } int mult2( int a ) { return 2*a; } func.c #include <stdio.h> #include "func.h int main( void ) { printf( "%.2f\t, mult2add( 2, 3.f ) ); printf( %d\n, mult2( 7 ) ); return 0; } main.c >> gcc -std=c99 -pedantic -Wall -Wextra main.c func.c >>./a.out 7.00 14 >> float mult2add( int x, float y ); int mult2( int a ); func.h

Header files #include <stdio.h> // #include "func.h Note: If we do not include the.h file(s), the compiler can try to guess the declaration: It can try to guess the types of the function s input from the arguments passed It will assume the output is an int This is right for mult2 This is wrong for mult2add float mult2add( int x, float y ); int mult2( int a ); func.h int main( void ) { printf( "%.2f\t, mult2add( 2, 3.f ) ); printf( %d\n, mult2( 7 ) ); return 0; } main.c

Header files #include <stdio.h> // #include "func.h Note: If we do not include the.h file(s), the compiler can try to guess the declaration: It can try to guess the types of the function s input from the arguments ^~~~~ passed It will assume the output is an int float mult2add( ^~~~~ int x, float y ); int mult2( int a ); func.h int main( void ) { printf( "%.2f\t, mult2add( 2, 3.f ) ); printf( %d\n, mult2( 7 ) ); return 0; >> gcc -std=c99 -pedantic -Wall -Wextra main.c func.c mainfile.c: In function main: } mainfile.c mainfile.c:6:20: warning: implicit declaration of function mult2add [-Wimplicit-function-declaration] printf( "%.2f\t", mult2add( 2, 3.f ) ); mainfile.c:6:13: warning: format %f expects argument of type double, but argument 2 has type int [-Wformat=] printf( "%.2f\t", mult2add( 2, 3.f ) ); ^ mainfile.c:7:19: warning: implicit declaration of function mult2 [-Wimplicit-function-declaration] printf( "%d\n", mult2( 7 ) ); >>

Header files #include <stdio.h> // #include "func.h Note: If we do not include the.h file(s), the compiler can try to guess the declaration: int main( void ) { printf( "%.2f\t, mult2add( 2, 3.f ) ); printf( %d\n, mult2( 7 ) ); return 0; >> gcc -std=c99 -pedantic -Wall -Wextra main.c func.c mainfile.c: In function main: } mainfile.c mainfile.c:6:20: warning: implicit declaration of function mult2add [-Wimplicit-function-declaration] printf( "%.2f\t", mult2add( 2, 3.f ) ); It can try to guess the types of the function s input from the arguments ^~~~~ passed mainfile.c:6:13: warning: format %f expects argument of type double, but argument 2 has type int [-Wformat=] It will assume the output is an int printf( "%.2f\t", mult2add( 2, 3.f ) ); ^ mainfile.c:7:19: warning: implicit declaration of function mult2 [-Wimplicit-function-declaration] printf( "%d\n", mult2( 7 ) ); ^~~~~ >>./a.out 0.00 14 >> The answer is junk because the compiler is reading the 4 float bytes as 4 int bytes

Compiling and linking Until now, we ve used one gcc command for compilation and linking compiling translates source (.c) files into intermediate object (.o) files linking combines.o files into one executable file called a.out (Recall that we can optionally specify the executable name with the -o flag)

Compiling and linking.h file #1.h file #2.h file #3.c file #1.c file #2 Preproc. Preproc..h file #2.h file #2.h file #3.c file #1.h file #3.c file #2 Compiler Compiler.o file #1 Linker a.out source files temp. files.o file #2 object files executable

Using header files When we ve run gcc, we did all three steps at once: Pre-processing Compilation Linking #include <stdio.h> #include "func.h int main( void ) { printf( "%.2f\t, mult2add( 2, 3.f ) ); printf( %d\n, mult2( 7 ) ); return 0; } main.c >> gcc -std=c99 -pedantic -Wall -Wextra main.c func.c >>./a.out 7.00 14 >>

Using header files When we ve run gcc, we did all three steps at once: Pre-processing Compilation Linking If we modify one of the files, we need to recompile But we only need to generate new object files for the modified source files #include <stdio.h> #include "func.h int main( void ) { printf( hi\n ); printf( "%.2f\t, mult2add( 2, 3.f ) ); printf( %d\n, mult2( 7 ) ); return 0; } main.c >> gcc -std=c99 -pedantic -Wall -Wextra main.c func.c >>./a.out hi 7.00 14 >>

Using header files #include <stdio.h> #include "func.h When we ve run gcc, we did all three steps at once: Pre-processing Compilation Linking If we modify one of the files, we need to recompile. But we only need to generate new object files for the modified source files int main( void ) { printf( hi\n ); printf( "%.2f\t, mult2add( 2, 3.f ) ); printf( %d\n, mult2( 7 ) ); return 0; } main.c >> gcc -std=c99 -pedantic -Wall -Wextra c main.c >> gcc main.o func.o >>./a.out 7.00 14 >> We can separately invoke the compiler (with preprocessor) and the linker

make and Makefiles Separately invoking the compiler and linker can be a pain: We need to track dependencies We need to track which files changed since the last time we compiled / linked

make and Makefiles make is a tool that helps keep track of which files need to be reprocessed so that those, and only those, are recompiled It takes a file containing a list of rules for generating specific files / targets: Prerequisites: What targets does this target depend on? Recipes: What should be done to generate this target?

make and Makefiles make is a tool that helps keep track of which files need to be reprocessed so that those, and only those, are recompiled It takes a file containing a list of rules for generating specific files / targets Simplest to name the file Makefile or makefile, otherwise need to run the make command with extra flags (specifying the name of the configuration file) There are strict rules about structure of Makefile, so it s easiest to follow a template and modify Note that tabs and spaces are not equivalent in a Makefile!

Makefiles Lines in a makefile consist of # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Makefiles Lines in a makefile consist of: Comments, start with a # sign # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Makefiles Lines in a makefile consist of: Comments, start with a # sign Definitions, assigned as: constant-name=... # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Makefiles Lines in a makefile consist of: Comments, start with a # sign Definitions, assigned as: constant-name=... and later referenced as: $(constant-name) # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Makefiles Lines in a makefile consist of: Comments, start with a # sign Definitions, assigned as: constant-name=... and later referenced as: $(constant-name) Rules for generating the targets # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Makefile rules Format of a Makefile rule target-name: {dependencies} # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Makefile rules Format of a Makefile rule target-name: {dependencies} a set of lines with a tab followed by a command-line instructions # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Invoking Makefiles Invoke the main tool with the name of the target to build If no target is given, the first is used # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Invoking Makefiles Invoke the main tool with the name of the target to build If no target is given, the first is used Examples: >> make clean Delete all object files and executable # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Invoking Makefiles Invoke the main tool with the name of the target to build If no target is given, the first is used Examples: >> make func.o Has func.c or func.h changed since the last creation of func.o? Yes: Compile func.c func.o No: Do nothing # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Invoking Makefiles Invoke the main tool with the name of the target to build If no target is given, the first is used Examples: >> make main.o Has main.c or func.h changed since the last creation of main.o? Yes: Compile main.c main.o No: Do nothing # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Invoking Makefiles Invoke the main tool with the name of the target to build If no target is given, the first is used Examples: >> make >> make main Does main.o or func.o need to be (re-)generated? Yes: make those first Has main.o or func.o changed since the last creation of main? Yes: Link main.o + func.o main No: Do nothing # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main

Invoking Makefiles # Define the compiler and flags CC=gcc CFLAGS=-std=c99 -pedantic -Wall Wextra Invoke the main tool with the name of the target to build If no target is given, the first is used Examples: >> make >> make main Does main.o or func.o need to be (re-)generated? Yes: make those first Has main.o or func.o changed since the last creation of main? Yes: link main.o + func.o main No: Do nothing # (Default) rule for making the main file main: main.o func.o $(CC) -o main main.o func.o # Rule for making the main object file main.o: main.c func.h $(CC) $(CFLAGS) -c main.c # Rule for making the functions object file func.o: func.c func.h $(CC) $(CFLAGS) -c func.c # Rule for clean-up clean: rm -f *.o rm -f main Note: This means that make can have a cascading effect, with the makeing of one target requiring the makeing of another.

Piazza Resources section Resources tab Exercise 3-2