Presentation is loading. Please wait.

Presentation is loading. Please wait.

Navigating the C++ Development Environment

Similar presentations


Presentation on theme: "Navigating the C++ Development Environment"— Presentation transcript:

1 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

2 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)

3 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

4 Lifecycle of a CSE 232/332 C++ Program
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

5 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

6 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

7 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

8 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

9 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) $<

10 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; }

11 #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…

12 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…

13 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

14 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

15 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

16 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;

17 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]

18 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?

19 {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?


Download ppt "Navigating the C++ Development Environment"

Similar presentations


Ads by Google