CSE 333 Interlude - make and build tools

Similar documents
CSE 374 Programming Concepts & Tools

Final C Details, Build Tools

Final C Details, Build Tools (make)

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

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

Makefile Tutorial. Eric S. Missimer. December 6, 2013

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

Make: a build automation tool

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

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

Multiple file project management & Makefile

Makefiles SE 2XA3. Term I, 2018/19

The make utility. Alark Joshi COMPSCI 253

Starting to Program in C++ (Basics & I/O)

Introduction to System Programming : makefile

Make: a build automation tool 1/23

Makefile Brief Reference

CSE 374 Programming Concepts & Tools

CSE 333 Lecture 6 - data structures

COSC345 Software Engineering. Make

CS11 Advanced C++ Fall Lecture 4

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

CS11 Intro C++ Spring 2018 Lecture 4

CSE 333 Midterm Exam 5/9/14 Sample Solution

Kurt Schmidt. May 23, 2018

Using GNU make C HAPTER 4

GNU Make. A Program for Directing Recompilation GNU make Version 3.79 April Richard M. Stallman and Roland McGrath

Reviewing gcc, make, gdb, and Linux Editors 1

COMP Lecture Notes The Compiler

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

CSE 303: Concepts and Tools for Software Development

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

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

GNU make. Michal Koutný. Software development and monitoring tools (NSWI126)

An Introduction to Ant

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

2 Compiling a C program

CSE 374 Final Exam Sample Solution 3/15/12

Maemo Diablo GNU Make and makefiles Training Material

Basic Compilation Control with Make

Chris' Makefile Tutorial

CSE 333 Midterm Exam 7/27/15 Sample Solution

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

Lab 2.2. Out: 9 February 2005

Concurrent Variants, Isolation, and Feature Models in Software Deployment

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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

Program Development Tools

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

UNIX Makefile. C Project Library Distribution and Installation.

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

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

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

CS Basics 15) Compiling a C prog.

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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:

@shorttitlepage GNU Make Copyright (C) 1988, '89, '90, '91, '92, '93 Free Software Foundation, Inc.

The Makefile utility. (Extract from the slides by Terrance E. Boult

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

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

independent compilation and Make

Intermediate Programming, Spring 2017*

Software Building (Sestavování aplikací)

CSC 2500: Unix Lab Fall 2016

CSE 374 Programming Concepts & Tools. Hal Perkins Winter 2012 Lecture 16 Version control and svn

Separate Compilation. EECS 211 Winter 2019

Practicum 5 Maps and Closures

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

CS Students Linux User's Guide

Makefile Practice: Part 1 Making a Single Program without Templated.h Files: Get the MakeBasics folder from the CS260 repository.

1 Compilation and Interpretation

CSE 333 Midterm Exam 7/29/13

GNU Make. A Program for Directing Recompilation. Edition 0.51, for make Version 3.75 Beta. May Richard M. Stallman and Roland McGrath

Projects and Make Files

CSCi 4061: Intro to Operating Systems Spring 2017 Instructor: Jon Weissman Assignment 1: Simple Make Due: Feb. 15, 11:55 pm

Personal SE. C Struct & Typedef Make

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

COMP26912: Algorithms and Imperative Programming

Generating Programs and Linking. Professor Rick Han Department of Computer Science University of Colorado at Boulder

GNU Make. A Program for Directing Recompilation GNU make Version 3.82 July Richard M. Stallman, Roland McGrath, Paul D.

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

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

CSE 333 Midterm Exam Sample Solution 7/29/13

CSC209H Lecture 5. Dan Zingaro. February 4, 2015

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CSE 303: Concepts and Tools for Software Development

CS354 gdb Tutorial Written by Chris Feilbach

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

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

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

The Linux Programming Environment. Computer Science Department Texas State University

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

C/C++ Programming. Session 10

Software Engineering /48

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

CS 247: Software Engineering Principles. Modules

Using the EE 109 Tools. Allan Weber Mark Redekopp

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

area.o perimeter.o formatting.o geometry.c gcc -o geometry area.o perimeter.o formatting.o geometry.c

CS 107 Lecture 18: GCC and Make

Transcription:

CSE 333 Interlude - make and build tools Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

make make is a classic program for controlling what gets (re)compiled and how. Many other such programs exist (e.g., ant, maven, projects in IDEs,...) make has tons of fancy features, but only two basic ideas: 1. Scripts for executing commands 2. Dependencies for avoiding unnecessary work To avoid just teaching make features (boring and narrow), let s focus more on the concepts...

Building software Programmers spend a lot of time building (creating programs from source code) - Programs they write - Programs other people write Programmers automate repetitive tasks. Trivial example: If you: gcc -Wall -g -std=c11 -o widget foo.c bar.c baz.c - Retype this every time: shame, shame - Use up-arrow or history: shame (retype after logout) - Have an alias or bash script: good-thinkin - Have a Makefile: you re ahead of us

Real build process On larger projects, you can t or don t want to have one big (set of) command(s) that redoes everything every time you change anything - If gcc didn t combine steps behind your back, you d need to preprocess and compile each file, then run the linker - If another program (e.g., sed) created some C files, you would need an earlier step - If you have other outputs for the same source files (e.g., javadoc), it s unpleasant to type the source file names multiple times - If you want to distribute source code to be built by other users, you don t want to explain the build logic to them - If you have 105 to 107 files of source code, you don t want to recompile them all every time you change something A simple script handles 1 4 (use a variable for filenames for 3), but 5 is trickier

Recompilation management The theory behind avoiding unnecessary compilation is a dependency dag (directed, acyclic graph): To create a target t, you need sources s 1,s 2,,s n and a command c (that directly or indirectly uses the sources) If t is newer than every source (file-modification times), assume there is no reason to rebuild it Recursive building: If some source s i is itself a target for some other sources, see if it needs to be rebuilt Cycles make no sense

Theory applied to C Compiling a.c creates a.o the.o depends on the.c and all included files (.h files, recursively/transitively) An archive (library,.a) depends on included.o files Creating an executable ( linking ) depends on.o files and archives (-L. -lfoo to get libfoo.a from current dir) So if one.c file changes, just need to recreate one.o file, maybe a library, and relink If a header file changes, may need to rebuild more And there are many more possibilities

make basics A makefile contains a bunch of triples target: sources command Example: foo.o: foo.c foo.h bar.h TAB gcc -Wall -o foo.o -c foo.c Syntax gotchas: The colon after the target is required Command lines must start with a TAB NOT SPACES You can actually have multiple commands (executed in order) If one command spans lines you must end the previous line with \ Which shell-language interprets the commands? (Typically bash; to be sure, set the SHELL variable in your makefile.)

Using make At the prompt: prompt% make -f nameofmakefile atarget Defaults: If no -f specified, use a file named Makefile If not target specified, use the first one in the file Open source usage: You can download a tarball, extract it, type make (four characters) and everything should work Actually, there s typically a configure step too, for finding things like where is the compiler that generates the Makefile (but we won t get into that) - The mantra:./configure; make; make install

Precise review A Makefile has a bunch of these: target: source_1... source_n shell_command Running make target does this: For each source, if it is a target in the Makefile, process it recursively Then: - If some source does not exist, error - If some source is newer than the target (or target does not exist), run shell_command (presumably updates target, but that is up to you; shell_command can do anything)

make variables You can define variables in a Makefile. Example: CC = gcc CFLAGS = -Wall -std=c11 foo.o: foo.c foo.h bar.h $(CC) $(CFLAGS) -c foo.c -o foo.o Why? Easy to change things Can change on make command line (CFLAGS=g)

More variables It s also common to use variables to hold list of filenames: OBJFILES = foo.o bar.o baz.o widget: $(OBJFILES) gcc -o widget $(OBJFILES) clean: rm $(OBJFILES) widget *~ clean is a convention: remove any generated files, to start over and have just the source It s funny because the target doesn t exist and there are no sources, but that s okay: - If target doesn t exist, it must be remade so run the commands - These phony targets have several uses, another is an all target...

all example all: prog B.class somelib.a # notice no commands this time prog: foo.o bar.o main.o gcc -o prog foo.o bar.o main.o B.class: B.java javac B.java somelib.a: foo.o baz.o ar r foo.o baz.o foo.o: foo.c foo.h header1.h header2.h gcc -c -Wall foo.c...(similar targets for bar.o, main.o, baz.o)...

Revenge of the funny characters Lots - see the documentation - $@ for target - $^ for all sources - $< for left-most source - Examples: widget: foo.o bar.o $(CC) $(CFLAGS) -o $@ $^ foo.o: foo.c foo.h bar.h $(CC) $(CFLAGS) -c $<

And more There are a lot of built-in rules. E.g., make just knows to create foo.o by calling $(CC) $(CFLAGS) on foo.c. (Opinion: may be more confusing than helpful. YMMV) There are suffix rules and pattern rules. Example: %.class: %.java javac $< # Note we need $< here Remember you can put any shell command on the command-line, even whole scripts You can repeat target names to add more dependencies (useful with automatic dependency generation) Often this stuff is more useful for reading makefiles than writing your own (until some day )

See you on Friday!