Navigating the C++ Development Environment

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Guide To UNIX Using Linux Third Edition
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Creating your first C++ program
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Chapter 02 (Part III) Introduction to C++ Programming.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Beginning C++ Through Game Programming, Second Edition
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.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
C++ Memory Overview 4 major memory segments Key differences from Java
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
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.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Modular Programming. Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve looked at programs from a text-based mode –Shell commands and command lines –Text editors,
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Programming Fundamentals Enumerations and Functions.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
A Sample Program #include using namespace std; int main(void) { cout
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Functions A bit of review (things we’ve covered so far)
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
CSE 332: Scientific debugging in C++ Scientific Debugging in C++ (with Eclipse & gdb) By now we’ve covered several key C++ features –Variable declarations.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Lecture 3: Getting Started & Input / Output (I/O)
Introduction to C Topics Compilation Using the gcc Compiler
C++ Lesson 1.
Review 1.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 CS 3370 – C++ Functions.
A bit of C programming Lecture 3 Uli Raich.
Motivation and Overview
Basic Elements of C++.
Command Line Arguments
Introduction to C Topics Compilation Using the gcc Compiler
C Basics.
Basic Elements of C++ Chapter 2.
When your program crashes
Introduction to C++ Programming
Subject:Object oriented programming
Compiler vs linker The compiler translates one .c file into a .o file
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Navigating the C++ Development Environment Two views of the development environment Graphical (stay in this mode whenever possible) Text based (also good to understand this well) We’ll look first at the text based mode Open ssh client, log into grid, get a text terminal window Create a subdirectory called cse232, open up sftp client We’ll work mainly in the nicer graphical environment later Use the Linux manual pages to look up information man <subject> Look up the following: cd ls mkdir find grep And now you know how to look up info on any command Don’t forget about www.google.com

Writing a CSE 232/332 C++ Program emacs Makefile (ASCII text) editor 1 source file = 1 compilation unit C++ source files (ASCII text) .cc Also: .C .cxx .cpp Programmer (you) Also: .H .hxx .hpp C++ header files (ASCII text) .h readme (ASCII text)

What Goes Into a C++ Program? Declarations: data types, function signatures, classes Allows the compiler to check for type safety, correct syntax Usually kept in “header” (.h) files Included as needed by other files (to keep compiler happy) class Simple { public: typedef unsigned int UINT32; Simple (int i); void print_i (); void usage (); private: int i_; }; Definitions: static variable initialization, function implementation The part that turns into an executable program Usually kept in “source” (.cc) files void Simple::print_i () { cout << “i_ is ” << i_ << endl; } Directives: tell compiler (or precompiler) to do something More on this later

Lifecycle of a CSE 232/332 C++ Program e-mail files you wrote (and any we provided) by due date/time xterm console/terminal Makefile make make turnin mail “make” utility Runtime/utility libraries (binary) .lib .a .so debugger make Eclipse Programmer (you) C++ source code g++ precompiler gcc link compiler linker executable program compiler object code (binary, one per compilation unit) .o

A Very Simple C++ Program #include <iostream> // precompiler directive using namespace std; // compiler directive /* definition of function named “main” */ int main (int, char *[]) { cout << “hello, world!” << endl; return 0; } comments

Download hello.cc to desktop, upload it to grid Let’s Build and Run It! Download hello.cc to desktop, upload it to grid First, we need to compile the program g++ -Wall -W -g -c -o hello.o hello.cc Then we need to link the compiled code g++ -o hello -Wall -W -g hello.o Then we can run the program ./hello

Ok, but that was rather Annoying! Typing that in each time you want to compile is a “do once” idea Can use the shell (for example, bash) to automate Put the following lines into a file called build.sh (or even better download and upload from web to grid) #!/bin/bash g++ -Wall -W -g -c -o hello.o hello.cc g++ -o hello -Wall -W -g hello.o Then, make the file executable and run it chmod u+x build.sh ./build.sh

A Better Way: the make Utility Controls the build process Enforces dependencies If something changes, update what depends on it (e.g., object files depend on header and body files) Relies on file update times Can “touch” a file to set its time stamp to now touch Calculator.cc Builds targets First one declared in Makefile is the default Usually want to have this one build the executable file Can build other targets: (the first two get rid of unwanted files) make clean make realclean make turnin

Exercise: Fill in the Example Makefile Get from web, to grid Makefile variables are called “macros” E.g., CMPL_SRCS and EXECUTABLE Some variables can be calculated E.g., OBJS Make infers need to build object files and the executable file Based on timestamps Chains through the dependencies Note: indented lines must start with tabs CMPL_SRCS = main.cc Calculator.cc Trace.cc EXECUTABLE = calc_test OBJS = $(CMPL_SRCS:.cc=.o) $(EXECUTABLE): $(OBJS) g++ -o $(EXECUTABLE) -Wall -W -g -DUNIX $(OBJS) clean: -rm -f *.o core *.bak *~ .cpp.o: $(COMPILE.cc) -Wall -W -g -DUNIX $(OUTPUT_OPTION) $<

Back to the Simple C++ Program #include <iostream> // precompiler directive using namespace std; // compiler directive /* definition of function named “main” */ int main (int, char *[]) { cout << “hello, world!” << endl; return 0; }

#include <iostream> #include tells the precompiler to include a file Usually, we include header files Contain declarations of structs, classes, functions <iostream> is the C++ label for a standard header file for input and output streams What would happen if we didn’t have it? Exercise: try commenting it out, then rebuild…

C++ provides such a namespace for its standard library using namespace std; The using directive tells the compiler to include code from libraries that have separate namespaces C++ provides such a namespace for its standard library cout, cin, and cerr standard iostreams, and much more Namespaces reduce collisions between symbols If another library defined cout we could say std::cout Can also apply using more selectively: E.g., just using std::cout What would happen if we didn’t have it? Exercise: try commenting it out, then rebuild…

What is int main (int, char*[]) { ... } ? Defines the main function of any C++ program Who calls main? The runtime environment, specifically often a function called crt0 What about the stuff in parentheses? A list of types of the input arguments to function main With the function name, makes up its signature Since this version of main ignores any inputs, we leave off names of the input variables, and only give their types What about the stuff in braces? It’s the body of function main, its definition

What’s cout << “hello, world!” << endl; ? Uses the standard output iostream, named cout For standard input, use cin For standard error, use cerr << is an operator for inserting into the stream “hello, world!” is a C-style string A 14-postion character array terminated by ‘\0’ endl is an iostream manipulator Ends the line, by inserting end-of-line character(s) Also flushes the stream

The main function should return an integer What about return 0; ? The main function should return an integer By convention it should return 0 for success And a non-zero value to indicate failure The program should not exit any other way Letting an exception propagate uncaught Dividing by zero Dereferencing a null pointer Accessing memory not owned by the program Indexing an array “out of range” can do this Dereferencing a “stray” pointer can do this

A Slightly Bigger C++ Program #include <iostream> using namespace std; int main (int argc, char * argv[]) { for (int i = 0; i < argc; ++i) cout << argv[i] << endl; } return 0;

A way to affect the program’s behavior int argc, char * argv[] A way to affect the program’s behavior Carry parameters with which program was called Passed as parameters to main from crt0 Passed by value (we’ll discuss what that means) argc An integer with the number of parameters (>=1) argv An array of pointers to C-style character strings Its array-length is the value stored in argc The name of the program is kept in argv[0]

for (int i = 0; i < argc; ++i) Standard C++ for loop syntax Initialization statement done once at start of loop Test expression done before running each time Expression to increment after running each time int i = 0 Declares integer i (scope is the loop itself) Initializes i to hold value 0 (not an assignment!) Exercise: what happens if you just say int i and leave off the = 0 part? (download program, upload it to grid, change file, update the Makefile) i < argc Tests whether or not we’re still inside the array! Reading/writing memory we don’t own can crash the program (if we’re really lucky!) Exercise: what happens if you say i <= argc ? ++i increments the array position (why prefix?) Exercise: what happens if you leave this statement off?

{cout << argv[i] << endl;} Body of the for loop I strongly prefer to use braces with for, if, while, etc., even w/ single-statement body Avoids maintenance errors when adding/modifying code Ensures semantics & indentation say same thing argv[i] An example of array indexing Specifies ith position from start of argv Exercise: what happens if you index by i+1?