UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.

Slides:



Advertisements
Similar presentations
Compiling. Your C, C++ or Fortran program won’t work unless you compile it The compiler will build your program as an executable file (typically in the.
Advertisements

The make Utility Programming Tools and Environments Winter 2006.
Make. A simple make file $ make program Strength of make is its sensitivity to dependency hierarchies. Specify such dependencies in a description file.
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.
Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.
The Makefile utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1 Linkage: g++ read.o main.o list.o –o prog1.o main.o.o list.o.o read.o Compilation:
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.
Systems Dev. Tutorial II: Make, utilities, & scripting Recitation Wednesday, Sept 13 th, 2006.
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
Basic linux shell commands and Makefiles. Log on to engsoft.rutgers.edu Open SSH Secure Shell – Quick Connect Hostname: engsoft.rutgers.edu Username/password:
1 ENERGY 211 / CME 211 Lecture 2 September 24, 2008.
Guide To UNIX Using Linux Third Edition
CS465 - Unix C Programming (cc/make and configuration control)
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
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.
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.
Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.
Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.cpp.
리눅스 : Lecture 5 UNIX 유틸리티 : text editor, compilation (make), …
Jump to first page (C) 1998, Arun Lakhotia 1 Software Configuration Management: Build Control Arun Lakhotia University of Southwestern Louisiana Po Box.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
C Tutorial - Program Organization CS Introduction to Operating Systems.
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.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
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.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
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.
An Introduction to the UNIX Make Utility Introduction: Although make can be used in conjunction with most programming languages all examples given here.
CSI605 Introduction to make. Advantages of Make Significantly reduces the amount of time spent compiling a program. Insures that programs are compiled.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 2 Class web site:
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
Lecture 1: Introduction UNIX programming environment –Editors –Remote shell setup –C compilers –Debugger –make.
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).
Lecture 6 UNIX Development Tools. Software Development Tools.
Multiple file project management & Makefile
Makefiles CSSE 332 Operating Systems
The make utility (original presentation courtesy of Alark Joshi)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Brief Intro to Make CST494/ Gannod.
Makefiles Caryl Rahn.
Editor, Compiler, Linker, Debugger, Makefiles
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
Unix Programming Environment
CMPSC 60: Week 4 Discussion
Appendix F C Programming Environment on UNIX Systems
Makefiles and the make utility
Makefiles, GDB, Valgrind
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.
The make utility (original presentation courtesy of Alark Joshi)
Presentation transcript:

UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, Overview of development process Creation of source files (.c,.h,.cpp) Compilation (e.g. *.c *.o) and linking Running and testing programs

(C) Doug Bowman, Virginia Tech, Development tools in UNIX Creation of source files (.c,.h,.cpp) Text editors (e.g. vi ) Revision control systems (e.g. rcs ) Compilation (e.g. *.c *.o) and linking Compilers (e.g. g++ ) Automatic building tools (e.g. make ) Running and testing programs Debuggers (e.g. gdb ) Integrated development environments (IDEs)

(C) Doug Bowman, Virginia Tech, Compiling with g++ GNU C++ compiler Performs one or more of the following: C++ pre-processing Compilation Linking

(C) Doug Bowman, Virginia Tech, Basic g++ examples g++ hello.cc (compile hello.cc, produce executable a.out ) g++ -o hello hello.cc (compile hello.cc, produce executable hello ) g++ -o hello hello.cc other_functions.cc (compile hello.cc and other_functions.cc, produce executable hello )

(C) Doug Bowman, Virginia Tech, Using intermediate files From any source file, you can produce an object file to be linked in later to an executable g++ -c hello.cc g++ -c other_functions.cc g++ -o hello hello.o other_functions.o

(C) Doug Bowman, Virginia Tech, Other important g++ options -g : include debugging symbols in the output -l : include a library called libname.a

(C) Doug Bowman, Virginia Tech, Include and library paths There are default directories in which g++ looks for include files and libraries -I : also look for include files in this directory -L : also look for library files in this directory

(C) Doug Bowman, Virginia Tech, Defines in g++ Often programs contain conditional parts based on defines: #ifdef DEBUG printf(“value of var is %d”, var); #endif You can set preprocessor defines on the command line g++ -DDEBUG -o prog prog.c

(C) Doug Bowman, Virginia Tech, Using make in compilation With medium to large software projects containing many files, it’s difficult to: Type commands to compile all the files correctly each time Keep track of which files have been changed Keep track of files’ dependencies on other files The make utility automates this process

(C) Doug Bowman, Virginia Tech, Basic operation of make Reads a file called [Mm]akefile, which contains rules for building a “target” If the target depends on a file, then that file is built If that file depends on a third file, then the third file is built, and so on… Works backward through the chain of dependencies Targets only built if they are older than the files they depend on

(C) Doug Bowman, Virginia Tech, Basic Makefile example program : main.o iodat.o dorun.o g++ -o program main.o iodat.o dorun.o main.o : main.cc g++ -c main.cc iodat.o : iodat.cc g++ -c iodat.cc dorun.o : dorun.cc g++ -c dorun.cc

(C) Doug Bowman, Virginia Tech, Types of lines in Makefiles Dependency or rules lines Commands Macro assignments Comments

(C) Doug Bowman, Virginia Tech, Dependency/rules lines Specify a target and a list of prerequisites (optional) for that target target : prereq1 prereq2 prereq3 …

(C) Doug Bowman, Virginia Tech, Command lines Follow dependency lines MUST start with a TAB! Any command that can be run in the shell can be placed here target : prereq1 prereq2 command1 command2 Special variables in commands: represents the target $? represents prereqs that are newer than target

(C) Doug Bowman, Virginia Tech, Macro (variable) assignments You can use macros to represent other text in a Makefile Saves typing Allows you to easily change the action of the Makefile Assignment: MACRONAME = macro value Usage: ${MACRONAME}

(C) Doug Bowman, Virginia Tech, Comments and other Makefile notes Comments begin with a ‘#’ Can be placed at the beginning of a line or after a non-comment line Lines that are too long can be continued on the next line by placing a ‘\’ at the end of the first line

(C) Doug Bowman, Virginia Tech, Invoking make Be sure that your description file: is called makefile or Makefile is in the directory with the source files make (builds the first target in the file) make target(s) (builds target(s)) Important options: -n : don’t run the commands, just list them -f file : use file instead of [Mm]akefile

(C) Doug Bowman, Virginia Tech, Basic Makefile example program : main.o iodat.o dorun.o g++ -o program main.o iodat.o dorun.o main.o : main.cc g++ -c main.cc iodat.o : iodat.cc g++ -c iodat.cc dorun.o : dorun.cc g++ -c dorun.cc

(C) Doug Bowman, Virginia Tech, Simplifying the example Makefile with macros OBJS = main.o iodat.o dorun.o CC = /usr/bin/g++ program : ${OBJS} ${CC} -o ${OBJS} main.o : main.cc ${CC} -c $? iodat.o : iodat.cc ${CC} -c $? dorun.o : dorun.cc ${CC} -c $?

(C) Doug Bowman, Virginia Tech, Suffix rules It’s still tedious to specifically tell make how to build each.o file from a.c/.cc file Suffix rules can be used to generalize such situations A default suffix rule turns.c/.cc files into.o files by running the command: ${CC} ${CFLAGS} -c $< $< refers to the prerequisite (file.cc)

(C) Doug Bowman, Virginia Tech, Simplifying the example Makefile again OBJS = main.o iodat.o dorun.o CC = /usr/bin/g++ program : ${OBJS} ${CC} -o ${OBJS}

(C) Doug Bowman, Virginia Tech, Other useful Makefile tips Include a way to clean up your mess clean: /bin/rm -f *.o core Include a target to build multiple programs all: program1 program2 program3