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.

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

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.
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.
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
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:
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.
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)
Testing and Debugging Hakam Alomari
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
Makefiles, and.h files, and.c files, and.o files, OH MY! For projects with more complexity. (Great.. Just what we needed)
Make: the good, the bad, and the ugly Dan Berger Titus Winters
Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros.
Makefiles CISC/QCSE 810. BeamApp and Tests in C++ 5 source code files After any modification, changed source needs to be recompiled all object files need.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
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.
Data Display Debugger (DDD)
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.
Object Oriented Programming COP3330 / CGS5409.  Assignment Submission Overview  Compiling with g++  Using Makefiles  Misc. Review.
Lecture 8  make. Using make for compilation  With medium to large software projects containing many files, it’s difficult to: Type commands to compile.
Make: File Dependency System Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third.
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 all linked.
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.
Problem Solving With C++ Recitation – make February 2016.
Multiple File Compilation and linking By Bhumik Sapara.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
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:
C P ROGRAMMING T OOLS. C OMPILING AND R UNNING S INGLE M ODULE P ROGRAM.
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.
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.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
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
Introduction to C Topics Compilation Using the gcc Compiler
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.
Makefiles and the make utility
Data Structures and Programming Techniques
CMPSC 60: Week 4 Discussion
Kyle Fitzpatrick Konstantin Zak 11/29/2004
SCMP Software Development Spring 2018 James Skon
Appendix F C Programming Environment on UNIX Systems
Makefiles and the make utility
Makefiles, GDB, Valgrind
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
Presentation transcript:

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 into.o object modules Phase 2, Linking:.o modules are linked together to create a single executable.

The Process main.cpp blurb.cppdatabase.cpp main.oblurb.odatabase.o a.out libraries Compiler Linker

How g++ Does All This Calling: “g++ ” When g++ is given.cpp files, it performs both compilation and linking. If given.o files, it performs linking only. Assignment 1: “g++ *.cpp” Quick and easy, but not ideal. Who wants a program named “a.out?”

Options For g++ Compilation “-c”: Compiles.cpp file arguments to.o but does not link (we’ll need this for “make” later). “-g”: Generates debugging information that is used by gdb-based debuggers “-I ”: Adds the directory to the list of directories searched for include files.

Compilation Options, cont. “-Wall”: Directs g++ to give warnings about code that’s probably in error. For example, it will catch and report: int number = GetRandomInteger() If (number = 6) std::cout << “Wow, number always seems to equal 6!”;

Options for g++ Linking “-o”: Specifies the name of the program to be linked together. Name textr “textr!” “-L ”: Adds the directory to the list of directories searched for library files (string, STL Vector, examples of libraries.) “-l ”: Makes compiler search the library for unresolved names when linking.

Sample g++ Commands: What Do They Do? 1)g++ -o textr *.cpp 2)g++ -Wall -g -c main.cpp 3)g++ -o myProg -L/usr/class/cs193/danLib -lDansFns *.o 4) g++ -I/usr/class/cs193/inc main.cpp src1.cpp src2.cpp

Now Onto “make” A GNU utility that determines which pieces of a large program need to be compiled or recompiled, and issues a commands to compile and link them in an automated fashion. Saves you from tedious, huge g++ commands! Just type in “make” in the directory containing the Makefile.

The Makefile: Input for make Create a file called “Makefile” that contains a series of properly formatted commands that make will take as input. The file can be huge and complex, but when it’s done and is working, you just have to type in “make” and the utility uses your makefile to do everything. “make” command searches for “Makefile.”

A Sample Makefile: “Huh?” # Makefile for “textr” C++ application #Created by Dan Wilson 1/29/06 PROG = textr CC = g++ CPPFLAGS = -g –Wall –I/usr/class/cs193d/textrInc LDFLAGS = -L/usr/class/cs193/lib –lCoolFns OBJS = main.o blurb.o database.o $(PROG) : $(OBJS) $(CC) $(LDFLAGS) -o $(PROG) $(OBJS) main.o : $(CC) $(CPPFLAGS) -c main.cpp blurb.o : blurb.h $(CC) $(CPPFLAGS) -c blurb.cpp database.o : database.h $(CC) $(CPPFLAGS) -c database.cpp clean: rm -f core $(PROG) $(OBJS)

Makefile Lingo “target”: Usually the name of an executable or object file that is generated by g++, but it can also be the name of an action to carry out. “prerequisites”: A list of files needed to create the target. If one of these files have changed, then the make utility knows that it has to build the target file again. Also called “dependencies.” “command”: An action that make carries out, usually a g++ compilation or linking command. In make, commands are run and generate output just as if they were entered one by one into the UNIX prompt.

Makefiles: Square One A simple Makefile consists of a series of “rules,” each of the form : target … : prerequisites... command … The rule explains how and when to make or remake a target file. A simple “make” call executes first rule by default. For other rules type “make ” Make requires a character before every command (silly, but causes errors).

The “textr” App Makefile, Take 1: textr : main.o blurb.o database.o g++ -o textr main.o blurb.o database.o main.o : main.cpp g++ -c main.cpp blurb.o : blurb.cpp blurb.h g++ -c blurb.cpp database.o : database.cpp database.h g++ -c database.cpp clean: rm -f core textr main.o blurb.o \ database.o

“clean” Target: Gotta Have One An important target that represents an action rather than a g++ operation. Has no dependencies, runs a command to remove all the compilation products from the directory, “cleaning” things up. Useful for porting code, getting rid of corrupted files, etc. Normally also removes the “core” file if it’s present from a past program meltdown. Call by typing “make clean” into prompt.

Next Step: Add Variables OBJS = main.o blurb.o database.o textr : $(OBJS) g++ -o textr $(OBJS) main.o : main.cpp g++ -c main.cpp blurb.o : blurb.cpp blurb.h g++ -c blurb.cpp database.o : database.cpp database.h g++ -c database.cpp clean: rm -f core textr $(OBJS)

Next Step: Omit the Obvious! (make knows.cpp ->.o) OBJS = main.o blurb.o database.o textr : $(OBJS) g++ -o textr $(OBJS) main.o : g++ -c main.cpp blurb.o : blurb.h g++ -c blurb.cpp database.o : database.h g++ -c database.cpp clean: rm -f core textr $(OBJS)

Adding Compiler Options Now assume that we need to specify some include files and libraries for our “textr” program (NOTE: THESE ARE IMAGINARY, DO NOT INCLUDE THEM IN YOUR MAKEFILE!) Also, we want to add the useful compile options to every g++ compilation command in the Makefile. More variables!

Adding Compiler Options CPPFLAGS = -g –Wall –I/usr/class/cs193d/include LDFLAGS = -L/usr/class/cs193/lib –lCoolFns OBJS = main.o blurb.o database.o textr : $(OBJS) g++ $(LDFLAGS) -o textr $(OBJS) main.o : g++ $(CPPFLAGS) -c main.cpp blurb.o : blurb.h g++ $(CPPFLAGS) -c blurb.cpp database.o : database.h g++ $(CPPFLAGS) -c database.cpp clean: rm -f core textr $(OBJS)

Adding the Finishing Touches Conform to standards by creating variables for the compiler and program name. Add comments.

Our Final Draft #Makefile for “textr” C++ application #Created by Dan Wilson 1/29/06 PROG = textr CC = g++ CPPFLAGS = -g –Wall –I/usr/class/cs193d/include LDFLAGS = -L/usr/class/cs193/lib –lCoolFns OBJS = main.o blurb.o database.o $(PROG) : $(OBJS) $(CC) $(LDFLAGS) -o $(PROG) $(OBJS) main.o : $(CC) $(CPPFLAGS) -c main.cpp blurb.o : blurb.h $(CC) $(CPPFLAGS) -c blurb.cpp database.o : database.h $(CC) $(CPPFLAGS) -c database.cpp clean: rm -f core $(PROG) $(OBJS)

Makefiles in Professional C++ Used in Industry for UNIX/Linux platforms Fast to run: Only recompile what you need to recompile upon code change, with a simple command Great for code versioning and deployment; just version the source code and the Makefile and quickly deploy on any compatible system (no need to version binaries).