Systems Programming/ C and UNIX

Similar documents
11.1 Modular Organization and makefiles.

CPSC 427: Object-Oriented Programming

COSC345 Software Engineering. Make

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

2 Compiling a C program

Java Programming Lecture 10

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

SmartHeap for Multi-Core

Programming in C S c o t t S c h r e m m e r

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

Programming Assignment 0

Topic 6: A Quick Intro To C

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

PRINCIPLES OF OPERATING SYSTEMS

15213 Recitation Section C

Data Management, Spring 2015: Project #1

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

CMSC 216 Introduction to Computer Systems Lecture 23 Libraries

CS240: Programming in C

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Basic Compilation Control with Make

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS143 Handout 05 Summer 2011 June 22, 2011 Programming Project 1: Lexical Analysis

CSC 1600 Memory Layout for Unix Processes"

o Code, executable, and process o Main memory vs. virtual memory

GNU make... Martin Ohlerich, Parallel Programming of High Performance Systems

Short Notes of CS201

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Lecture 2: C Programming Basic

CS201 - Introduction to Programming Glossary By

Introduction to Linux

CS240: Programming in C. Lecture 2: Overview

Workspace for '3-ctips' Page 1 (row 1, column 1)

Lectures 5-6: Introduction to C

CSE 333 SECTION 3. POSIX I/O Functions

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Cosc 242 Assignment. Due: 4pm Friday September 15 th 2017

CSE 333 Midterm Exam July 24, Name UW ID#

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

CPSC 427: Object-Oriented Programming

OPERATING SYSTEMS ASSIGNMENT 4 XV6 file system

CS11 Advanced C++ Fall Lecture 4

Introduction to Software Security Hash Functions (Chapter 5)

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

CSI33 Data Structures

make and makefile CS 211

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

ENERGY 211 / CME 211. Evolution

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

19 File Structure, Disk Scheduling

printf( Please enter another number: ); scanf( %d, &num2);

cget Documentation Release Paul Fultz II

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

Lectures 5-6: Introduction to C

CSE 333 SECTION 3. POSIX I/O Functions

22 File Structure, Disk Scheduling

Getting Started. Project 1

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CPSC 427a: Object-Oriented Programming

EE 355 Lab 4 - Party Like A Char Star

Tutorial 1 C Tutorial: Pointers, Strings, Exec

CPSC 427: Object-Oriented Programming

Programming in C. What is C?... What is C?

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

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

UNIX input and output

Week 5 Lecture 3. Compiler Errors

15-323/ Spring 2019 Project 4. Real-Time Audio Processing Due: April 2 Last updated: 6 March 2019

Classes in C++98 and C++11

Lab 2: More Advanced C

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 2

Topics Introduction to Microprocessors

CSE 303, Spring 2006, Final Examination 6 June Please do not turn the page until everyone is ready.

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

CSCI 2212: Intermediate Programming Introduction to C++ Classes

Should you know scanf and printf?

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

CSE 333 Lecture 6 - data structures

Recitation 2/18/2012

Lesson 9 Programming Arduino Hash, Encryption and Decryption Functions Usages (Examples 9.8 and 9.9)

Lecture 8: Structs & File I/O

Object-Oriented Principles and Practice / C++

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Project #1 Exceptions and Simple System Calls

Section 3: File I/O, JSON, Generics. Meghan Cowan

CSCI 171 Chapter Outlines

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

ASSIGNMENT TWO: PHONE BOOK

CpSc 1111 Lab 9 2-D Arrays

CSCI-UA /002, Fall 2012 RK Lab: Approximiate document matching Due: Sep 26 11:59PM

Transcription:

Systems Programming/ C and UNIX Alice E. Fischer Lecture 5 Makefiles October 2, 2017 Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 1/14 October 2, 2017 1 / 14

Outline 1 Modules and Makefiles Modular Construction Makefiles 2 Fingerprints: A SHA1 demo program Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 2/14 October 2, 2017 2 / 14

Modules and Makefiles Modular Construction Compiling and Linking A Makefile Defines the Project Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 3/14 October 2, 2017 3 / 14

Modular Construction Modular Programming: Keep It Short and Simple In the old days, people wrote entire programs in one file, and most of the code was inside main(). Such programs are easy to write and difficult to debug. Today, professionals use a highly modular coding style. A project comprises several code files, each with its own definable purpose and its own header file. Functions, including main() are kept short one screen at most. Complexity is handled by writing many short functions. In C++, many short functions are defined as inline functions to improve performance. Within a function, code is restricted to one idea per line. Highly nested or convoluted code is avoided. Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 4/14 October 2, 2017 4 / 14

Modular Construction How to Compile and Link Modules We use IDE s to manage our multi-module programs. However, when working on a remote machine, that does not work. For this situation, you need to know how to create and use a makefile. To compile and link a multi-module program, list all the source files in the g++ command: g++ -o p4 -Wall *.cpp../tools.cpp Or, use a makefile (explained later in this lecture). Each module you list will be compiled, in order. After all modules compile correctly, the system will attempt to link all the object files together with the library functions. Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 5/14 October 2, 2017 5 / 14

Modular Construction Way is a Modular Style Important? My general rules: If I cannot look at a piece of code and understand it, the code is too complex. Maximize the amount of code you can see on one screen at one time. Use blank lines sparingly. In all cases, you should be able to see the variable declarations from all parts of the code that use them. Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 6/14 October 2, 2017 6 / 14

Makefiles A Makefile Defines the Project Every modern IDE gives you a way to specify a set of code files that are all part of a complete application. It lets you add code and header files to the project, compile, build, run, and clean out a project. IDE s are a great invention, but they don t work from a command line, when you are logged into a remote machine. So, even now, it is worth figuring out what a makefile does and how to write one. A makefile is a simple text file that contains shell commands, definitions, and comments. The tab character is NOT the same as a series of spaces; it has special meaning. (Yuk!) A makefile lists all the file dependencies, that is, which files are needed for each step of the build process. The make system will skip a recompiling or relinking step when no file in its list of dependencies has changed. Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 7/14 October 2, 2017 7 / 14

Makefiles Writing a Makefile Refer to Chapter 23.1 of Applied C (paper edition) or Chapter 20 of the online version: http://eliza.newhaven.edu/cprog2/chapters/chapter20.pdf. You find these kinds of lines in a makefile: Comments start with # and occupy the entire line. To define a symbol, write the name followed by = and the meaning. The newline ends the definition To use a symbol, write $(symbolname) A target is one compilation, linking or cleanup step. Defining a target takes two lines. The first gives the name of the target, followed by a colon and a list of files on which that target depends. The second line must start with a tab, and it gives the command to execute to create this target. Additional indented lines may follow, if more than one command is necessary. Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 8/14 October 2, 2017 8 / 14

Makefiles A Makefile for the SHA1 demo program. # Define flags for compiler. CXXFLAGS = -g -O1 -Wall # Use clang++ for OS-X Lion or Mountain Lion # Change this to g++ for Linux and earlier OS-X versions CXX = clang++ # The crypto library must be explicitly linked in. LIBS = -lcrypto # This program has two modules, shuffle (main) and tools. OBJ = shafile.o tools.o # Get any object file by compiling its source file. shafile: $(OBJ) $(CXX) -o $@ $(OBJ) $(LIBS) Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 9/14 October 2, 2017 9 / 14

Makefiles A C++ Makefile continued. To use this makefile, put it in the same directory as your source code and make that your current active directory. Then type in: make depend This command will scan your files, find the dependencies, create a list of dependency declarations, and append them to this file. To avoid duplication, delete the old dependencies first. On my test application, the dependencies generated were: shafile.o: shafile.cpp tools.hpp tools.o: tools.cpp tools.hpp To build your application, type in make or make game To clean out.o files and the executable type make clean Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 10/14 October 2, 2017 10 / 14

Fingerprints: A SHA1 demo program Fingerprints: A SHA1 demo program What is a fingerprint? Why are they useful? Creating a fingerprint Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 11/14 October 2, 2017 11 / 14

Fingerprints: A SHA1 demo program Fingerprinting a File - crypt A fingerprint (also called a hash ) is a string or integer that is calculated (like a checksum) from all the data in a file. The old, original way to calculate a fingerprint for a text file as follows: Allocate a buffer that is large enough for the whole file Read in the entire file. (Use fread or read, not scanf.) Call char * crypt (const char *data, const char *salt) where data is the buffer that stores your file and salt is the string A1 or any other 2-character string. The resulting fingerprinting can be stored and later used to identify whether the file has been changed without authorization. Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 12/14 October 2, 2017 12 / 14

Fingerprints: A SHA1 demo program Is it a Duplicate? One way to detect a file copy is to compare the two files, byte by byte. This is efficient and makes sense if the two files are attached to the same computer. For two files that are stored in different locations, one file would need to be transmitting across the network before it could be compared to the other. This would be much to slow to be practical as a way of detecting duplicates. The cryptographic algorithms used for fingerprinting make it very unlikely that the fingerprints of two different files will match. Thus, we can be fairly confident that if two files have the same length and the same fingerprint, they are copies of the same file. Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 13/14 October 2, 2017 13 / 14

Fingerprints: A SHA1 demo program Fingerprinting a File - SHA SHA256 (a 32-byte hash) and SHA3 are more modern encryption algorithms, but they have no advantage for our purposes and SHA3 may not be universally supported yet. Please use either SHA1 or SHA256. man 3 SHA1 #include <openssl/sha.hpp> unsigned char* SHA1( const unsigned char* d, unsigned long n, unsigned char* md); d is the buffer that stores your file, n is the file length, in bytes, and md is a buffer to hold the answer. It must have space for SHA_DIGEST_LENGTH == 20 bytes. The return value is a pointer to the array that contains the digest. If md is NULL, the digest is placed in a static array. See the demo code: Sha-c++: Alice E. Fischer Lecture 5 Makefiles Lecture 5 Makefiles... 14/14 October 2, 2017 14 / 14