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

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

2 Compiling a C program

CSE 333 Interlude - make and build tools

Intermediate Programming, Spring 2017*

CSE 374 Programming Concepts & Tools

independent compilation and Make

CS240: Programming in C

CS Basics 15) Compiling a C prog.

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

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

Maemo Diablo GNU Make and makefiles Training Material

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

CS240: Programming in C. Lecture 2: Overview

CSE 333 Lecture 6 - data structures

CSC 2500: Unix Lab Fall 2016

Topic 6: A Quick Intro To C

Introduction to Supercomputing

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Multiple file project management & Makefile

Reviewing gcc, make, gdb, and Linux Editors 1

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

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

Make: a build automation tool

Lab 2.2. Out: 9 February 2005

Make: a build automation tool 1/23

Makefiles SE 2XA3. Term I, 2018/19

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

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

CS Students Linux User's Guide

CS11 Advanced C++ Fall Lecture 4

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

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

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

COMP26120 Academic Session: Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling

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

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

Exercise 1: Basic Tools

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

Reversing. Time to get with the program

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

The make utility. Alark Joshi COMPSCI 253

Introduction to Supercomputing

COMP 2400 UNIX Tools

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

UNIX Makefile. C Project Library Distribution and Installation.

PRINCIPLES OF OPERATING SYSTEMS

A Fast Review of C Essentials Part II

COMP s1 Lecture 1

PROGRAMMAZIONE I A.A. 2017/2018

make and makefile CS 211

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

Getting started with UNIX/Linux for G51PRG and G51CSA

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

CS Programming In C

CSE 303 Lecture 8. Intro to C programming

Using GNU make C HAPTER 4

C / C++ Coding Rules

RVDS 4.0 Introductory Tutorial

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CSSE 332 Standard Library, Storage classes, and Make

Tutorial: Compiling, Makefile, Parallel jobs

Basic Compilation Control with Make

CSE 333 Lecture 6 - data structures

RVDS 3.0 Introductory Tutorial

Systems Programming. laboratory. Compilation automation. make ceedling

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

Workshop Agenda Feb 25 th 2015

Lab 2: More Advanced C

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

COMP26912: Algorithms and Imperative Programming

Lecture 4: Build Systems, Tar, Character Strings

Introduction: The Unix shell and C programming

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

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

Program Development Tools

COSC345 Software Engineering. Make

Chris' Makefile Tutorial

CSE 333 Lecture 5 - data structures & modules

Follow us on Twitter for important news and Compiling Programs

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

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

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

CSE 390 Lecture 9. Version control and Subversion (svn)

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

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

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

CS50 Supersection (for those less comfortable)

Makefile Brief Reference

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

C introduction: part 1

COMP 2718: The OS, Shell, Terminal, and Text

EL2310 Scientific Programming

Final C Details, Build Tools

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

CS201 - Lecture 1 The C Programming Language

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

Transcription:

Outline COMP 2718: Software Development Tools: gcc and make Slides adapted by Andrew Vardy ( Memorial University) Originally created by Marty Stepp, Jessica Miller, and Ruth Anderson (University of Washington) http://www.cs.washington.edu/390a/ We will look at the following software development tools: The gcc compiler The make build tool (usually used for C/C++) (later) The ant built tool (usually used for Java) Along the way we ll introduce some additional concepts: Compiling and linking Object files We will not focus on the actual programming language concepts, but on the tools themselves. So don t worry if you don t know or remember either Java or C/C++. 1 2 C and Java We will discuss tools for software development, mostly with respect to C and Java C is one of the most well-used programming languages C++ is the object-oriented successor to C Yet C continues to be used, particularly for embedded systems C/C++ are compiled languages. The compiler generates a machine language executable program. Java is also extremely popular and shares many of its objectoriented features with C++ Java programs are compiled to Java bytecode (not machine language) Java bytecode runs on a Java Virtual Machine (JVM) which can be implemented on a wide array of platforms according to the Java slogan: "Write once, run anywhere" 3 4 1

Compiling: Java Compiling: C What happens when you compile a Java program? $ javac Example.java Example.class produces command gcc description GNU C compiler Answer: It produces a.class file. Example.java is compiled to create Example.class How do you run this Java program? $ java Example 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 5 6 Multiple Files 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 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 inserted into an executable later You can compile a mixture of.c and.o files: $ gcc -o combined part1.o part2.c combined produces Avoids recompilation of unchanged partial program files (e.g. part1.o) 7 8 2

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 commands like this is wasteful: often the required compile command is much longer for larger projects even if one file is changed (e.g. file3.c) all files need to be recompiled; again this becomes really time consuming for larger projects We ll look at this through the following Running Example... 9 10 Running Example --- hello.c /* Classic "hello world" program, split into three.c files. */ #include "file2.h" #include "file3.h" #include <stdio.h> int main(void) { print_hello(); printf(" "); print_world(); printf("\n"); } --- file2.h /* Print "hello". */ void print_hello(void); --- file2.c #include "file2.h" #include <stdio.h> void print_hello(void) { printf("hello"); } --- file3.h /* Print "world". */ void print_world(void); --- file3.c #include "file3.h" #include <stdio.h> void print_world(void) { printf("world"); } Running Example We have three modules: hello, file2, file3 Two of the modules have both.h and.c files: file2, file3 These are intended to be used externally The third module is the main program: hello No.h file because this module is the main one, it pulls in file2 and file3 with include statements Simplest solution to compile is the following one-liner: gcc -o hello hello.c file2.c file3.c Using this one-liner we would have to re-compile if any file changes No intelligence built into this process; easy for mistakes to be made (e.g. file2.h is changed, but we forget to re-compile) 11 12 3

Running Example The following solution is a bit better: gcc -c hello.c Generates hello.o gcc -c file2.c Generates file2.o gcc -c file3.c Generates file3.o gcc -o hello hello.o file2.o file3.o Generates hello executable Yes, this was more work to type, but the.o files can be separately re-compiled if necessary Still it would be good to encode all of this in a script or something; Even better if files could be made to compile only when necessary make make : A utility for automatically compiling ("building") executables and libraries from source code. a 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. 13 14 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 (optional) run sudo make install to install on your system execute the program 15 16 4

Makefile rule syntax Running make target : source1 source2... sourcen command command... source1 through sourcen are the dependenciesfor building target Make will execute the commands in order $ make target uses the file named Makefilein 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: 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! 17 $ make builds the first target in the Makefile $ make -f makefilename $ make -f makefilename target uses a makefile other than Makefile 18 Making a Makefile Making a Makefile Here is a very simple makefile for our running example: hello: hello.c file2.c file3.c gcc -o hello hello.c file2.c file3.c Its nice and short but notice that it is really just the same as our first gcc one-liner, only wrapped in the syntax of a makefile although it does have some advantages: Documents build instructions Will re-build hello if any of the.c files change (if make is called) The disadvantage is that a change requires compilation of all files. courtesy XKCD 19 20 5

Separate Targets Lets add multiple targets to our Makefile to build the.o files individually, then build the executable: hello: hello.o file2.o file3.o gcc -o hello hello.o file2.o file3.o hello.o: hello.c gcc -c hello.c file2.o: file2.c gcc -c file2.c Rules with no dependencies clean: rm file1.o file2.o file3.o myprog make assumes that a rule's command(s) 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 file3.o: file3.c gcc -c file3.c This is better because changing one file will not require complete recompilation 21 22 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, myprog2are up to date all rule often put first, so that typing make will build everything 23 NAME = value $(NAME) Example Makefile: Variables OBJFILES = file1.o file2.o file3.o PROGRAM = myprog $(PROGRAM): $(OBJFILES) gcc -o $(PROGRAM) $(OBJFILES) clean: (declare) (use) Note that Makefile syntax is similar to bash syntax but also differs (e.g. spaces allowed in variable assignment) rm $(OBJFILES) $(PROGRAM) variables make it easier to change one option throughout the file also makes the makefile more reusable for another project 24 6

Adding Features to Example OBJECTS = hello.o file2.o file3.o PROG = hello all: $(PROG) hello: $(OBJECTS) gcc -o $(PROG) $(OBJECTS) hello.o: hello.c gcc -c hello.c file2.o: file2.c gcc -c file2.c file3.o: file3.c gcc -c file3.c clean: rm -f $(OBJECTS) $(PROG) Disadvantages of this version: Much longer! (we will fix this) Advantages: Variables allow customization The clean target allows us to start from a clean slate 25 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" 26 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 $< 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 $@ $< *http://www.gnu.org/software/make/m anual/html_node/autom atic-var iabl es.html #Automati c-vari abl es 27 28 7

Final Version of Example OBJECTS = hello.o file2.o file3.o PROG = hello all: $(PROG) $(PROG): $(OBJECTS) gcc -o $(PROG) $(OBJECTS).c.o: gcc -c $< Nice features of this final version: More compact than previous Can be easily customized for similar projects just by modifying variables clean: rm -f $(OBJECTS) $(PROG) 29 8