CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.

Slides:



Advertisements
Similar presentations
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Advertisements

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Today ’ s Topic Breakdown of GCC script Breakdown of GCC script Description of how different system programs work together to build (and run) a C program.
SYSTEM PROGRAMMING & SYSTEM ADMINISTRATION
Systems Software.
Software. What Is Software? software –Also called Computer programs –Are a list of instructions –Instructions are called code –CPU performs the instructions.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Software Language Levels Machine Language (Binary) Assembly Language –Assembler converts Assembly into machine High Level Languages (C, Perl, Shell)
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Program Flow Charting How to tackle the beginning stage a program design.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Course Introduction and Getting Started with C 1 USF - COP C for Engineers Summer 2008.
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
1 Chapter Two Introduction to the Programming Language C.
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
Assembler Compiler Interpreter ASSEMBLER To convert the assembly language into machine code. Translate mnemonic operation codes to their machine language.
Programming In C++ Spring Semester 2013 Programming In C++, Lecture 1.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
Introduction 01_intro.ppt
Introduction to C Programming. A Brief History u Created by Dennis Ritchie at AT&T Labs in 1972 u Originally created to design and support the Unix operating.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
Creating your first C++ program
Programming With C.
TAMU CSCE 313 (the basics). Basic Unix/Linux programming Accessing CS systems  PuTTY (putty.exe) – a Telnet and SSH client  Common hosts: unix.cs.tamu.edu.
CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++
Editing & Compiling: UNIX vs. IDE and an Intro to Architecture.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
C Language: Introduction
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 Original Source : and Problem and Problem Solving.ppt.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 05, 2005 Lecture Number: 4.
Chapter Three The UNIX Editors.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
Agenda Computer Languages How to Write a Simple C Program
Introduction to OOP CPS235: Introduction.
Department of Electronic & Electrical Engineering Introduction to C - The Development cycle. Why C? The development cycle. Using Visual Studio ? A simple.
Multiple File Compilation and linking By Bhumik Sapara.
The Development Process Compilation. Compilation - Dr. Craig A. Struble 2 Programming Process Problem Solving Phase We will spend significant time on.
Lecture1 Instructor: Amal Hussain ALshardy. Introduce students to the basics of writing software programs including variables, types, arrays, control.
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
Sung-Dong Kim Dept. of Computer Engineering, Hansung University Chapter 3 Programming Tools.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Software Engineering Algorithms, Compilers, & Lifecycle.
C++ Programming Basics C++ Lecture 1 Stacy MacAllister.
Introduction to C Topics Compilation Using the gcc Compiler
CS1010: Intro Workshop.
Programming languages
CSCI-235 Micro-Computer Applications
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Guide To UNIX Using Linux Third Edition
Computer Engineering 1nd Semester
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Unit# 8: Introduction to Computer Programming
TRANSLATORS AND IDEs Key Revision Points.
Creating your first C program
General Computer Science for Engineers CISC 106 Lecture 03
Introduction to C Topics Compilation Using the gcc Compiler
Presentation transcript:

CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line

Compilers A compiler is a program that reads source code, which is the C++ code written by a programmer, and produces an executable or binary file A Compiler is an important tool that can check your syntax, issue warnings about questionable usage, indicate errors, and allow you to create binaries or executables that can run to test your program's logic

Command Line Compiles You'll eventually use a more sophisticated interface (an IDE - Integrated Development Environment) such as Visual C++ –The common denominator you'll always find is the plain command line interface. –Even if you use an IDE, it could help you understand how things work "behind the scenes"

Editors He first step in creating a C++ program is writing the source code. This, you will need access to a text editor –Unix VI EMACS PICO –Windows NotePad WordPad

Using Unix at WCC 1.START 2.PROGRAMS 3.NET TOOLS 4.TERRATERM PRO

Using Unix at WCC 5. Host: cidermill.wccnet.org TCP/IP TCP PORT #23 OTHER * 6.OK 7.Login: pmillis 8.PW: ************ 9.PICO (or EMACS or VI)

Entering Your Program Type it into your editor and save the file as myprog.cpp #include int main() { cout << "Hello World\n" ; return 0; }

Compiling The source file is a plain text file containing your code. The executable file, consist of machine codes, 1's and 0's, that are not meant to be understood or read by people, but only by computers

The Compiling Process The compilation is split in to many different phases 1.Driver - The "engine", that drives the whole set of tools the compiler is made of. We invoke it, and it begins to invoke the other tools one by one, passing the output of each tool as an input to the next tool. 2.C++ Pre-Processor - It takes a C source file, and handles all the pre-processor definitions (#include files, #define macros, conditional source code inclusion with #ifdef, etc.)

The Compiling Process 3.T he C++ Compiler - This is the actual compiler, that translates the input file into assembly language. 4.Optimizer - This one handles the optimization on a representation of the code that is language- neutral. 5.Assembler - This takes the assembly code generated by the compiler, and translates it into machine language code kept in object files. 6.Linker-Loader - This is the tool that takes all the object files (and C++ libraries), and links them together, to form one executable file, in a format the operating system supports.

Why the Complexity? The basic idea is quite simple - split the compiler into many different parts to give the programmer more flexibility and to allow the compiler developers to re-use as many modules as possible

C++ Compilers easiest case of compilation is when you have all your source code set in a single file C++ compiler is rally named c++ or g++ "g++ source file" You may see some errors or warning messages if you typed something incorrectly in your source file. If so, correct your errors in the source file, using the text editor and recompile. In all cases, you'll get a file 'a.out' as a result, if the compilation completed successfully

C++ Compilers To compile, you type, at the prompt "g++ source file -o executable_file " Every compiler recognizes the '-o' flag as specifying the name the resulting executable file.

Running the Program Simply type myprog at the command line This requires that the current directory be in our PATH. In many cases, this directory is not placed in our PATH. We specify it explicitly:./myprog This time we explicitly told our Unix shell that we want to run the program from the current directory. One more obstacle could block our path - file permission flags.

File Permissions Normally you'll have no problem running the file, but if you copy it to a different directory, or transfer it to a different computer over the network, it might loose its original permissions, and thus you'll need to set them properly, as shown: chmod u+rwx myprog

Debuggable Code Normally, when we write a program, we want to be able to debug it. We need to tell the compiler to insert information to the resulting executable program that'll help the debugger g++ -g myprog.cpp -o myprog The '-g' flag tells the compiler to use debug info

Removing the Debugging Note that the resulting file is much larger then that created without usage of the '-g' flag. The difference in size is due to the debug information. We may still remove this debug information using the strip command, like this: strip myprog You'll note that the size of the file now is even smaller then if we didn't use the '-g' flag in the first place

Creating Optimized Code The basic way to create an optimized program would be like this: g++ -O myprog.cpp -o myprog The '-O' flag tells the compiler to optimize the code.

Getting Extra Compiler Warnings To get the compiler to use all types of warnings it is familiar with, we'll use a command line like this: g++ -Wall myprog.cpp -o myprog