SCMP Software Development Spring 2018 James Skon

Slides:



Advertisements
Similar presentations
Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files.
Advertisements

The make Utility Programming Tools and Environments Winter 2006.
Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
1 The Makefile Utility ABC – Chapter 11,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
MAKEFILES A description file that defines the relationships or dependencies between applications and functions; it simplifies the development process.
1 CS 201 Makefile Debzani Deb. 2 Remember this? 3 What is a Makefile? A Makefile is a collection of instructions that is used to compile your program.
Guide To UNIX Using Linux Third Edition
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 9.
CS465 - Unix C Programming (cc/make and configuration control)
G++ and make Dan Wilson CS193 02/01/06. The g++ Compiler What happens when you call g++ to build your program? Phase 1, Compilation:.cpp files are compiled.
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros.
July 29, 2003Serguei Mokhov, 1 Makefile Brief Reference COMP 229, 346, 444, 5201 Revision 1.2 Date: July 18, 2004.
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
Makefile M.A Doman. Compiling multiple objects Card.cpp -> Card.o Deck.cpp -> Deck.o main.cpp -> main.o main.o Deck.o Card.o -> Dealer.exe.
The Make utility. Motivation Small programs all in single cpp file “Not so small” programs : Many lines of code Multiple components More than one programmer.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Object Oriented Programming COP3330 / CGS5409.  Assignment Submission Overview  Compiling with g++  Using Makefiles  Misc. Review.
Modular Programming. Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to.
Lecture 8  make. Using make for compilation  With medium to large software projects containing many files, it’s difficult to: Type commands to compile.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
Make Make is a system utility that automatically compiles your programs for you Make looks for a file named Makefile (or makefile) in the current directory.
Multiple File Compilation and linking By Bhumik Sapara.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
CSI605 Introduction to make. Advantages of Make Significantly reduces the amount of time spent compiling a program. Insures that programs are compiled.
Build Tools 1. Building a program for a large project is usually managed by a build tool that controls the various steps involved. These steps may include:
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects can become huge My research consists of 1600.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Makefile Script file to automate program compilation and linking (making) 1. Write the "makefile" 2. Write your programs 3. Run "make" or "make -f makefile"
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Object Oriented Programming COP3330 / CGS5409.  Assignment Submission Overview  Compiling with g++  Using Makefiles  Misc. Review.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Week Seven Agenda Link of the week Review week six lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
The make utility (original presentation courtesy of Alark Joshi)
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Computer Science 210 Computer Organization
Brief Intro to Make CST494/ Gannod.
Compilation and Debugging
Compilation and Debugging
Makefiles.
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
Computer Science 210 Computer Organization
Makefile Separate Compilation
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Makefiles and the make utility
Computer Science 210 Computer Organization
Data Structures and Programming Techniques
CMSC 202 Additional Lecture – Makefiles
Kyle Fitzpatrick Konstantin Zak 11/29/2004
CSc 352: Elementary “make”
Makefiles and the make utility
Compiler vs linker The compiler translates one .c file into a .o file
SCMP Software Development Spring 2018 James Skon
SPL – PS1 Introduction to C++.
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
g++ features, better makefiles
The make utility (original presentation courtesy of Alark Joshi)
Presentation transcript:

SCMP 368.00 Software Development Spring 2018 James Skon Make Files with C++ SCMP 368.00 Software Development Spring 2018 James Skon

What is Make? A utility which maintains up-to-date versions of programs. Based on rules defined in a "makefile" it will rebuild executables by determining which modules need to be recompiled whenever you make changes to the original source code.

Make Dependancies A "makefile" is a list of rules and dependencies with corresponding commands to create "targets" using the rules and dependencies. We will be using a makefile to manage a list of files that "make" or build a C++ programs.

Make Dependancies A C++ program can be (and usually is) built with several source code files and several header files. Source code files are usually a collection of .cpp files and .h header files.

Make Dependancies When a program has a large number of source files, the files typically depend on one another in complex ways. Whenever changes are made to one file that other files depend on, all dependent files will need to be recompiled.

Make Dependancies Determining exactly which files need to be updated can be difficult and time-consuming task. The alternative, recompiling all source files regardless of whether it is necessary, wastes both cpu and elapsed time.

Make The make utility permits the definition of program dependencies by specifying the appropriate rules

Using Make when run, make reads a dependency information is read from a file named makefile (or Makefile) in the current working directory. By using the -f flag, you can specify that another filename holds this information. % make % make -f makeprog2

Using Make A simple makefile has the following form: target-file: prerequisite-file-list (tab) construction-commands target-file is the name of the a file prerequisite-file-list is the files that target-file depends on. construction-commands is a list of commands to build the target file. must start with a TAB

An Example Lets say we have 3 .cpp files and 2 .h files that make up our program. They are: main1.cpp mylib.cpp mylib.h openfile.cpp openfile.h Want to create an executable named my_lab

Label and dependancies Example Makefile TAB Comment # Makefile for my_lab my_lab: main1.o mylib.o openfile.o g++ -o my_lab main1.o mylib.o openfile.o main1.o: main1.cpp openfile.h mylib.h g++ -c main1.cpp mylib.o: mylib.cpp mylib.h g++ -c mylib.cpp openfile.o: openfile.cpp openfile.h g++ -c openfile.cpp clean: rm *.o lab1.out Label and dependancies Actions

Make Makefiles can include comments, delimited by hash marks (#). A backslash (\) can be used at the end of a line to continue a command onto the next physical line. If needed, multiple shell commands can be issued to build a specified target file. Each construction command should be placed on its own line, indented by tabs.

How Make Works To keep a collection of subprograms up to date make compares the modification time of the target file with the modification times of the prerequisite files. Any prerequisite file that has a more recent modification time than its target file forces the target file to be recreated.

How Make Works By default, the first target file appearing in the makefile is the one that is built. Other targets are only checked if they are a prerequisite for the initial target. Other than the fact that the first target in the makefile is the default, the order of the targets does not matter. The make utility will build them in the order required.

Verifying Makefile Formats You can test makefiles by using the -n option. This option causes the make utility to display, but not execute, all the commands it would normally execute to build the program.

A Simple Makefile Example Main program source is in abc.cpp, Subroutines in a.cpp, b.cpp and c.cpp. All use abc.h Thus, the executable program will depend on all four of these files.

A Simple Makefile Example /* abc.cpp */ main() { a(); b(); c(); } /* a.cpp */ #include <stdio.h> a() printf("A\n"); /* b.cpp */ #include <stdio.h> b() { printf("B\n"); } /* c.cpp */ c() printf("C\n");

A Simple Makefile Example # makefile : makes the ABC program abc : a.o b.o c.o abc.o g++ -o abc abc.o a.o b.o c.o abc.o : abc.cpp g++ -O -c abc.cpp a.o : a.cpp, abc.h g++ -O -c a.cpp b.o : b.cpp , abc.h g++ -O -c b.cpp c.o : c.cpp , abc.h g++ -O -c c.cpp

Internal Make Rules You can rely on implied dependencies and construction commands (or internal rules) to simplify your makefile. A list of internal rules allows the make utility to understand how to create a file with one suffix from a file with another suffix.

Internal Make Rules Example Consider the following programs /* A sample program to illustrate the MAKE utility */ #include <stdio.h> main() { printf("Working\n"); compute_sales_figures(); printf(".\n"); pay_employees(); printf("Done\n"); }

Internal Make Rules Example pay_employees() { int i, k; for (i=1; i<100; ++i) k = i + 1; }

Internal Make Rules Example /* A sample program to illustrate the MAKE utility */ #include <stdio.h> compute_sales_figures() { int i, k; for (i=1; i<100; ++i) k = i + 1; }

Internal Make Rules Example Now consider the following makefile: # makefile : makes the payroll program # for the make tutorial # Default construction commands will # build the object files: # salary.o and sales.o payroll : salary.o sales.o g++ salary.o sales.o -o payroll

Internal Make Rules Example Notice that there is only one dependency line: salary.o sales.o Neither of these object files are listed as targets in the makefile make will search for source code in the working directory from which to build the object file based on the default construction commands.

Default construction commands By default, if an object file without a target is created as follows: g++ -O -c file.cpp Thus, in the case of the above example, we get: g++ -O -c salary.cpp g++ -O -c sales.cpp

Default construction commands If the default behavior is not what you want, you must explicitly give the instructions in the makefile.

Make Macros The make utility supports a sophisticated macro ability. In a manner similar to UNIX shell variables, macros can be used to substitute one set of character sequences for another.

Make Macros macroname = replacestring You can define macros in your makefile with an entry of the following form: macroname = replacestring The replacestring can consist of all characters on a single line before a comment character (#).

Make Macros Macros can be continued onto additional successive lines by using line continuations (\). Make will replace each of the characters $(macroname) in the makefile with the given replacestring.

Make Macros example # payroll2.make: another makefile for the payroll program # used in thr make tutorial. CFLAGS = -g -O # compiler options payroll : salary.o sales.o g++ -o payroll salary.o sales.o salary.o : salary.cpp g++ -c $(CFLAGS) salary.cpp sales.o : sales.cpp g++ -c $(CFLAGS) sales.cpp

Make Macros example By putting the compiler flags in a macro, you can easily change them in just one place rather than on every individual compile command.