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

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

CSE 403 Lecture 11. Static Code Analysis. Reading: IEEE Xplore, "Using Static Analysis to Find Bugs"

2 Compiling a C program

CSE 333 Interlude - make and build tools

CSE 374 Programming Concepts & Tools

Software Development. COMP220/COMP285 Seb Coope Ant: Structured Build

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

Intermediate Programming, Spring 2017*

Makefiles SE 2XA3. Term I, 2018/19

Multiple file project management & Makefile

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

CS11 Advanced C++ Fall Lecture 4

independent compilation and Make

Introduction to Supercomputing

CS240: Programming in C. Lecture 2: Overview

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

Make: a build automation tool

Make: a build automation tool 1/23

Build Tools. Software Engineering SS 2007

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

CSE 390a Lecture 1. introduction to Linux/Unix environment

CS240: Programming in C

Introduction to Software Engineering: Tools and Environments. Session 9. Oded Lachish

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

COSC345 Software Engineering. Make

Maemo Diablo GNU Make and makefiles Training Material

Build Tools. Software Engineering SS A tool was needed. Agenda for today. Build tools. Software complexity. Build tools

Lab 2.2. Out: 9 February 2005

CSE 333 Lecture 6 - data structures

Ant. Originally ANT = Another Neat Tool. Created by James Duncan Davidson Now an Apache open-source project

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

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

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

Using GNU make C HAPTER 4

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

Program Development Tools

Build. System building

Makefile Brief Reference

... Fisheye Crucible Bamboo

Basic Unix Commands. CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

CSE 390a Lecture 5. Intro to shell scripting

The make utility. Alark Joshi COMPSCI 253

CS Basics 15) Compiling a C prog.

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

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

CSE 391 Lecture 1. introduction to Linux/Unix environment

CSE 391 Lecture 1. introduction to Linux/Unix environment

Systems Programming. laboratory. Compilation automation. make ceedling

Intro to Linux & Command Line

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

Getting It Right COMS W4115. Prof. Stephen A. Edwards Spring 2007 Columbia University Department of Computer Science

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

Chapter 2 Author Notes

Basic Compilation Control with Make

Reversing. Time to get with the program

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

CS Students Linux User's Guide

Chris' Makefile Tutorial

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

The Linux Programming Environment. Computer Science Department Texas State University

Pattern Matching, set-up. Pattern Matching, example. Pattern Matching, set-up, cont. Lecture 9: Make Pattern Matching & Conceptual Integrity

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

Topic 6: A Quick Intro To C

Software Engineering - Development Infrastructure 2. Kristjan Talvar Nortal

CSE 303 Lecture 8. Intro to C programming

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

CSE 391 Lecture 5. Intro to shell scripting

Compiling Code using Makefiles and GNUstep. 2501ICT Nathan

CSE 303, Spring 2009 Final Exam Wednesday, June 10, 2009

Using the Unix system. UNIX Introduction

Outline. Configuration management. Main Phases MOTIVATION

CS11 Intro C++ Spring 2018 Lecture 4

Compiling Software on UNIX. System Administration Decal Spring 2009 Lecture #4 George Wu Slides prepared by Joshua Kwan

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

C / C++ Coding Rules

Dalhousie University CSCI 2132 Software Development Winter 2018 Lab 8, March 22

Build automation. CSE260, Computer Science B: Honors Stony Brook University

6.170 Laboratory in Software Engineering Eclipse Reference for 6.170

Lecture 4: Build Systems, Tar, Character Strings

Software Building (Sestavování aplikací)

CSE 390a Lecture 7. Regular expressions, egrep, and sed

Exercise 1: Basic Tools

Introduction to System Programming : makefile

Build Automation Kurt Christensen

CS Programming In C

These steps may include:

CS 261 Recitation 1 Compiling C on UNIX

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

CSE 15L Winter Midterm :) Review

Deployment Tools and Techniques

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

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

Lab 2: More Advanced C

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

Personal SE. C Struct & Typedef Make

Introduction to Supercomputing

CSC 2500: Unix Lab Fall 2016

COMP 2400 UNIX Tools

Projects and Make Files

Transcription:

CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson http://www.cs.washington.edu/390a/ 1

Motivation single-file programs do not work well when code gets large compilation can be slow hard to collaborate between multiple programmers more cumbersome to edit larger programs are split into multiple files each file represents a partial program or module modules can be compiled separately or together a module can be shared between multiple programs but now we have to deal with all these files just to build our program 2

3 Compiling: Java What happens when you compile a Java program? $ javac Example.java Example.class produces Answer: It produces a.class file. Example.java is compiled to create Example.class How do you run this Java program? $ java Example

Compiling: C command gcc description GNU C compiler To compile a C program called source.c, type: gcc -o target source.c target (where target is the name of the executable program to build) the compiler builds an actual executable file (not a.class like Java) Example: gcc -o hi hello.c Compiles the file hello.c into an executable called hi To run your program, just execute that file: Example:./hi produces 4

5 Object files (.o) A.c file can also be compiled into an object (.o) file with -c : $ gcc -c part1.c part1.o produces $ ls part1.c part1.o part2.c a.o file is a binary blob of compiled C code that cannot be directly executed, but can be directly linked into a larger executable later You can compile and link a mixture of.c and.o files: $ gcc -o myprogram part1.o part2.c myprogram produces Avoids recompilation of unchanged partial program files (e.g. part1.o)

6 Header files (.h) header : A C file whose only purpose is to be #included (#include is like java import statement) generally a filename with the.h extension holds shared variables, types, and function declarations similar to a java interface: contains function declarations but not implementations key ideas: every name.c intended to be a module (not a stand alone program) has a name.h name.h declares all global functions/data of the module other.c files that want to use the module will #include name.h

Compiling large programs Compiling multi-file programs repeatedly is cumbersome: $ gcc -o myprogram file1.c file2.c file3.c Retyping the above command is wasteful: for the developer (so much typing) for the compiler (may not need to recompile all; save them as.o) Improvements: use up-arrow or history to re-type compilation command for you use an alias or shell script to recompile everything use a system for compilation/build management, such as make 7

make make : A utility for automatically compiling ("building") executables and libraries from source code. a very basic compilation manager often used for C programs, but not language-specific primitive, but still widely used due to familiarity, simplicity similar programs: ant, maven, IDEs (Eclipse),... Makefile : A script file that defines rules for what must be compiled and how to compile it. Makefiles describe which files depend on which others, and how to create / compile / build / update each file in the system as needed. 8

9 Dependencies dependency : When a file relies on the contents of another. can be displayed as a dependency graph to build main.o, we need data.h, main.c, and io.h if any of those files is updated, we must rebuild main.o if main.o is updated, we must update project1

make Exercise figlet : program for displaying large ASCII text (like banner). http://freecode.com/projects/figlet Download a piece of software and compile it with make: download.tar.gz file un-tar it look at README file to see how to compile it (sometimes) run./configure for cross-platform programs; sets up make for our operating system run make to compile the program execute the program 10

Makefile rule syntax target : source1 source2... sourcen command command... source1 through sourcen are the dependencies for building target Make will execute the commands in order Example: myprogram : file1.c file2.c file3.c gcc -o myprogram file1.c file2.c file3.c this is a tab THIS IS NOT spaces!! The command line must be indented by a single tab not by spaces; NOT BY SPACES! SPACES WILL NOT WORK! 11

Running make $ make target uses the file named Makefile in current directory Finds a rule in Makefile for building target and follows it if the target file does not exist, or if it is older than any of its sources, its commands will be executed variations: $ make builds the first target in the Makefile $ make -f makefilename $ make -f makefilename target uses a makefile other than Makefile 12

Making a Makefile Exercise: Create a basic Makefile to build {hello.c, file2.c, file3.c} Basic works, but is wasteful. What happens if we change file2.c? everything is recompiled. On a large project, this could be a huge waste 13

14 Making a Makefile courtesy XKCD

Making a Makefile Exercise: Create a basic Makefile to build {hello.c, file2.c, file3.c} Basic works, but is wasteful. What happens if we change file2.c? everything is recompiled. On a large project, this could be a huge waste Augment the makefile to make use of precompiled object files and dependencies by adding additional targets, we can avoid unnecessary re-compilation 15

Rules with no dependencies myprog: file1.o file2.o file3.o gcc -o myprog file1.o file2.o file3.o clean: rm file1.o file2.o file3.o myprog make assumes that a rule's command will build/create its target but if your rule does not actually create its target, the target will still not exist the next time, so the rule will always execute its commands (e.g. clean above) make clean is a convention for removing all compiled files 16

Rules with no commands all: myprog myprog2 myprog: file1.o file2.o file3.o gcc -o myprog file1.o file2.o file3.o myprog2: file4.c gcc -o myprog2 file4.c... all rule has no commands, but depends on myprog and myprog2 typing make all will ensure that myprog, myprog2 are up to date all rule often put first, so that typing make will build everything Exercise: add clean and all rules to our hello Makefile 17

18 Variables NAME = value $(NAME) (declare) (use) Example Makefile: OBJFILES = file1.o file2.o file3.o PROGRAM = myprog $(PROGRAM): $(OBJFILES) gcc -o $(PROGRAM) $(OBJFILES) clean: rm $(OBJFILES) $(PROGRAM) variables make it easier to change one option throughout the file also makes the makefile more reusable for another project

Example Makefile: More variables OBJFILES = file1.o file2.o file3.o PROGRAM = myprog CC = gcc CCFLAGS = -g -Wall $(PROGRAM): $(OBJFILES) $(CC) $(CCFLAGS) -o $(PROGRAM) $(OBJFILES) many makefiles create variables for the compiler, flags, etc. this can be overkill, but you will see it "out there" 19

Special variables $@ the current target file $^ all sources listed for the current target $< the first (left-most) source for the current target (there are other special variables*) Example Makefile: myprog: file1.o file2.o file3.o gcc $(CCFLAGS) -o $@ $^ file1.o: file1.c file1.h file2.h gcc $(CCFLAGS) -c $< Exercise: change our hello Makefile to use variables for the object files and the name of the program *http://www.gnu.org/software/make/manual/html_node/automatic-variables.html#automatic-variables 20

Auto-conversions Rather than specifying individually how to convert every.c file into its corresponding.o file, you can set up an implicit target: # conversion from.c to.o Makefile comments!.c.o: gcc $(CCFLAGS) -c $< "To create filename.o from filename.c, run gcc -g -Wall -c filename.c" For making an executable (no extension), simply write.c :.c: gcc $(CCFLAGS) -o $@ $< Exercise: simplify our hello Makefile with a single.c.o conversion 21

What about Java? Create Example.java that uses a class MyValue in MyValue.java Compile Example.java and run it javac automatically found and compiled MyValue.java Now, alter MyValue.java Re-compile Example.java does the change we made to MyValue propagate? Yep! javac follows similar timestamping rules as the makefile dependencies. If it can find both a.java and a.class file, and the.java is newer than the.class, it will automatically recompile But be careful about the depth of the search... But, this is still a simplistic feature. Ant is a commonly used build tool for Java programs giving many more build options. 22

Ant Similar idea to Make Ant uses a build.xml file instead of a Makefile <project> <target name= name > tasks </target> <target name= name > tasks </target> </project> Tasks can be things like: <javac /> <mkdir /> <delete /> A whole lot more http://ant.apache.org/manual/tasksoverview.html 23

Ant Example Create an Ant file to compile our Example.java program To run ant (assuming build.xml is in the current directory): $ ant targetname For example, if you have targets called clean and compile: $ ant clean $ ant compile Refer to: http://ant.apache.org/manual/tasksoverview.html for more information on Ant tasks and their attributes. 24

Example build.xml file <!-- Example build.xml file --> <!-- Homer Simpson, cse390a --> <project> <target name="clean"> <delete dir="build"/> </target> <target name="compile"> <mkdir dir="build/classes"/> <javac srcdir="src" destdir="build/classes"/> </target> </project> 25

Automated Build Systems Fairly essential for any large programming project Why? Shell scripts instead? What are these tools aiming to do? Is timestamping the right approach for determining recompile? What about dependency determination? What features would you want from an automated build tool? Should building your program also involve non-syntactic checking? Ant can run JUnit tests 26