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

Similar documents
Separate Compilation of Multi-File Programs

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

CS Basics 15) Compiling a C prog.

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

2 Compiling a C program

Projects and Make Files

CS 247: Software Engineering Principles. Modules

Lab 1: First Steps in C++ - Eclipse

Final Topics. Ali Malik

The Compilation Process

The Linux Programming Environment. Computer Science Department Texas State University

Follow us on Twitter for important news and Compiling Programs

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

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

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

CSC 2500: Unix Lab Fall 2016

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

C++ Programming for Non-C Programmers. Supplement

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

CSI33 Data Structures

ENERGY 211 / CME 211. Evolution

The University Of Michigan. EECS402 Lecture 09. Andrew M. Morgan. Savitch Ch Compiling With Multiple Files Make Utility.

A Fast Review of C Essentials Part II

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

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Object Oriented Design

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

Your First C++ Program. September 1, 2010

Crash Course in C++ R F L Evans. www-users.york.ac.uk/~rfle500/

Laboratorio di Tecnologie dell'informazione

CPSC 427: Object-Oriented Programming

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

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

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

CS2141 Software Development using C/C++ C++ Basics

Laboratorio di Programmazione. Prof. Marco Bertini

UNIX Makefile. C Project Library Distribution and Installation.

C++ Lab 03 - C++ Functions

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

assembler Machine Code Object Files linker Executable File

Using the EE 109 Tools. Allan Weber Mark Redekopp

Understanding main() function Input/Output Streams

Chapter 1 INTRODUCTION

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Object Oriented Design

Lecture 7. Log into Linux New documents posted to course webpage

OBJECT ORIENTED PROGRAMMING USING C++

independent compilation and Make

PIC 10A. Lecture 17: Classes III, overloading

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Due Date: See Blackboard

Implementing an ADT with a Class

CS101 Linux Shell Handout

How Compiling and Compilers Work

Topic 6: A Quick Intro To C

Laboratorio di Programmazione. Prof. Marco Bertini

Tutorial 2: Compiling and Running C++ Source Code

Makefiles SE 2XA3. Term I, 2018/19

TDDE18 & 726G77. Functions

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Cpt S 122 Data Structures. Introduction to C++ Part II

Part III Synchronization A bit of C++ and ThreadMentor

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Practicum 5 Maps and Closures

Intermediate Programming, Spring 2017*

y

Getting Started with Visual Studio

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

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

CS240: Programming in C

C++ Programming: make

Laboratorio di Programmazione. Prof. Marco Bertini

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

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

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

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Comp Sci 1570 Introduction to C++

5. Applicative Programming. 1. Juli 2011

A A B U n i v e r s i t y

Intermediate Programming, Spring 2017*

CMSC202 Computer Science II for Majors

Multiple Inheritance and Object Layout in C++

Programmazione. Prof. Marco Bertini

Object-oriented Programming and Introduction to C++

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Due Date: See Blackboard

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

Binomial pricer (1.1)

Maemo Diablo GNU Make and makefiles Training Material

Why Is Repetition Needed?

C++ Mini Lessons Last Update: Feb 7, 2018

CS 1337 Computer Science II Page 1

Arrays in C++ Instructor: Andy Abreu

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

UEE1303(1070) S 12 Object-Oriented Programming in C++

Transcription:

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

g++ g++ is the GNU C++ compiler. A program in a file called hello.cpp: #include <iostream> using namespace std; int main( ) { cout << Hello world! << endl; } Typing 'g++ hello.cpp' in a shell will produce an executable called a.out. Typing './a.out' will run the program. Compiling a C++ Program 2

g++ Options Use the -o flag to change the executable name: > g++ -o hello hello.cpp >./hello Hello world! Some other flags: -g Adds debugging information to the executable -Wall Turns on all the warnings. Sometimes this complains about things that the programmer did on purpose, which is why it is turned off by default. > g++ -g -Wall -o hello hello.cpp Compiling a C++ Program 3

Compiling Multiple Files If the source is in multiple files, all the files can be listed on one line and compiled together: > g++ -o quack quack.cpp moo.cpp Always recompiling every file when just one file has changed is inefficient. Use the -c flag to create an object file: > g++ -c quack.cpp > g++ -c moo.cpp This produces files called quack.o and moo.o. Compiling a C++ Program 4

Compiling Multiple Files cont. The object files are then linked to form the executable: > g++ -o quack quack.o moo.o Now if a source file is changed, only that one file needs to be recompiled: > g++ -c quack.cpp > g++ -o quack quack.o moo.o An object file contains machine code and must be linked with other object files to form an executable program. Compiling a C++ Program 5

The Compilation Process What happens after typing 'g++ hello.cpp': First hello.cpp gets preprocessed The preprocessed file is compiled to assembly code. The assembly code is assembled to machine code. The machine code is linked to produce the executable. #include <iostream> using namespace std int main( ) { int i, n; } cin >> n; for( i = 0; i < cout << Hell cout << From return 0; hello.cpp Preprocessor # 1 "hello.cpp" # 1 "<built-in>" # 1 "<command line # 1 "hello.cpp" # 1 "/usr/include/ # 42 "/usr/include # 43 "/usr/include # 4294967220 "/usr # 44 "/usr/include # 1 "/usr/include/ # 35 "/usr/include hello.ii Compiler.file "hello.cpp.local _ZSt8 i.comm _ZSt8 i.text.align 2.globl main.type main, @f main:.lfb1512: pushl %ebp.lcfi0: movl %esp, %e.lcfi1: subl $24, %es hello.s Assembler 01011101010101011 10101011010101100 10100001001111001 00001011110110101 01011100001010100 00001110101100111 01010101100101010 01010101101000011 00010010101010100 11001011011011010 10101010101001011 10111100010101111 11100000001010100 hello.o Linker a.out Compiling a C++ Program 6

The Compilation Process cont. Compilation can be stopped at any stage: cpp hello.cpp > hello.ii produces hello.ii (cpp is the C and C++ preprocessor) g++ -s hello.cpp produces hello.s g++ -c hello.cpp produces hello.o #include <iostream> using namespace std int main( ) { int i, n; } cin >> n; for( i = 0; i < cout << Hell cout << From return 0; hello.cpp Preprocessor # 1 "hello.cpp" # 1 "<built-in>" # 1 "<command line # 1 "hello.cpp" # 1 "/usr/include/ # 42 "/usr/include # 43 "/usr/include # 4294967220 "/usr # 44 "/usr/include # 1 "/usr/include/ # 35 "/usr/include hello.ii Compiler.file "hello.cpp.local _ZSt8 i.comm _ZSt8 i.text.align 2.globl main.type main, @f main:.lfb1512: pushl %ebp.lcfi0: movl %esp, %e.lcfi1: subl $24, %es hello.s Assembler 01011101010101011 10101011010101100 10100001001111001 00001011110110101 01011100001010100 00001110101100111 01010101100101010 01010101101000011 00010010101010100 11001011011011010 10101010101001011 10111100010101111 11100000001010100 hello.o Linker a.out Compiling a C++ Program 7

Header Files and Source Files Programs are usually divided into multiple files. Header files (end in.h) contain things like class definitions and function prototypes. Source files (end in.cpp) contain implementations of functions and class methods. Header files are included into source files and other header files using the #include directive: #include <file.h> or #include file.h Compiling a C++ Program 8

Header Files and Source Files cont. The #include directive tells the preprocessor to paste the contents of the included file at that spot. Filenames in angle brackets are system headers. Filenames in quotes are local headers. Usually header files use include guards to prevent a header from being included more than once in the same compilation unit: #ifndef FILENAME_H // If FILENAME_H is not defined... #define FILENAME_H //... then define FILENAME_H and...... //... whatever else... #endif //... up until here. Compiling a C++ Program 9

Example Program Consider a small program with three files: hello.h, hello.cpp, and main.cpp hello.h is a header file. It contains prototypes for a couple of functions: #ifndef HELLO_H #define HELLO_H void hello( ); void goodbye( int ); #endif Compiling a C++ Program 10

Example Program cont. hello.cpp implements functions from hello.h: #include <iostream> using namespace std; #include hello.h void hello( ) { cout << Hello world! << endl; } void goodbye( int n ) { for( int i = 0; i < n; ++i ) cout << Goodbye! << endl; } Compiling a C++ Program 11

Example Program cont. main.cpp contains the main function: #include hello.h int main( ) { hello( ); goodbye( 28 ); return 0; } Compiling a C++ Program 12

Makefiles To compile the example program: > g++ -c hello.cpp > g++ -c main.cpp > g++ -o hello hello.o main.o To avoid repeatedly issuing the commands to compile the program, a makefile can be used Once a makefile is written and saved in a file called Makefile, the program can be compiled by simply running make: > make Compiling a C++ Program 13

Creating a Makefile A makefile mostly contains rules, with each rule looking something like this: target: dependencies (tab) command (tab) another command...... The target is the name of a file or an action. Dependencies are files needed to create the target. Each command line must start with a tab. Comments start with a # Compiling a C++ Program 14

Makefile Example 1 A very simple makefile for the example program: hello: hello.cpp hello.h main.cpp g++ -o hello hello.cpp main.cpp To use the makefile and run the program: > make >./hello Hello world! Goodbye!... This is not a very good makefile since it will force make to always recompile everything Compiling a C++ Program 15

Makefile Example 2 A better makefile: hello: hello.o main.o g++ -o hello hello.o main.o hello.o: hello.cpp hello.h g++ -c hello.cpp main.o: main.cpp hello.h g++ -c main.cpp Now make will only have to recompile the files that were changed Compiling a C++ Program 16

Cleaning Makefiles often include a target called clean that removes all the object files and executables: #... other targets... clean: rm -f hello main.o hello.o By default, make builds the first target in the makefile. To use the clean target, just pass it as an argument to make: > make clean Compiling a C++ Program 17

Makefile Variables Makefiles often contain a lot of redundancy Variables can eliminate some of the redundancy To declare a variable: VARIABLENAME = value To use the variable: ${VARIABLENAME} Some common variables: CPP The name of the C++ compiler EXEC The name of the executable FLAGS Any flags for the C++ compiler Compiling a C++ Program 18

Makefile Example 3 CPP = g++ FLAGS = -g EXEC = hello OBJS = hello.o main.o # List of object files needed to # build the executable. ${EXEC}: ${OBJS} ${CPP} ${FLAGS} -o ${EXEC} ${OBJS} hello.o: hello.cpp hello.h ${CPP} ${FLAGS} -c hello.cpp main.o: main.cpp hello.h ${CPP} ${FLAGS} -c main.cpp clean: rm -f ${EXEC} ${OBJS} Compiling a C++ Program 19

Abstract Rules An abstract rule tells how to build a file *.s2 from a file *.s1, where s1 and s2 are suffixes. The following variables can be used in an abstract rule: $< The dependencies that changed. $@ The target. $^ All the dependencies. A line is needed listing the suffixes used:.suffixes: s1 s2... sn Compiling a C++ Program 20

Makefile Example 4.SUFFIXES:.cpp.o CPP = g++ FLAGS = -g EXEC = hello OBJS = hello.o main.o ${EXEC}: ${OBJS} ${CPP} ${FLAGS} -o ${EXEC} ${OBJS}.cpp.o: ${CPP} ${FLAGS} -c $< # Abstract rule # Still need to list the dependencies for object files hello.o: hello.cpp hello.h main.o: main.cpp hello.h clean: rm -f ${EXEC} ${OBJS} Compiling a C++ Program 21