Energy 211 cme 211
Advertisement
This presentation is the property of its rightful owner.
1 / 47

ENERGY 211 / CME 211 PowerPoint PPT Presentation

ENERGY 211 / CME 211. Lecture 2 September 24, 2008. Evolution. In the beginning, we all used assembly That was too tedious, so a very crude compiler for FORTRAN was built FORTRAN was still too painful to work with, so ALGOL 60 was created

Download Presentation

ENERGY 211 / CME 211

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Energy 211 cme 211

ENERGY 211 / CME 211

Lecture 2

September 24, 2008


Evolution

Evolution

  • In the beginning, we all used assembly

  • That was too tedious, so a very crude compiler for FORTRAN was built

  • FORTRAN was still too painful to work with, so ALGOL 60 was created

  • ALGOL 60 merged with COBOL to form CPL, for both science and business


Evolution cont d

Evolution, cont’d

  • CPL was too large and complex, so it was simplified to obtain BCPL

  • BCPL was stripped down even more for systems programming, leading to B

  • B was stripped down too much for more advanced operating systems, so it was enhanced to create C


From c to c

From C to C++

  • Bjarne Stroustrup wanted a language that was efficient, like C, AND suitable for development of large applications, like SIMULA

  • He enhanced C with SIMULA-like features to create “C with classes”

  • Rick Mascitti first used the name C++

  • First commercial release in 1985


Design considerations

Design Considerations

  • There is no lower-level language between C++ and machine language (can write assembly in C++, but few do)

  • For backward compatibility, any valid C program is a valid C++ program

  • Unlike other languages, C++ supports multiple programming paradigms, such as procedural, object-oriented, generic, functional, etc.


From the text you type to the program you run

From the Text you Type to the Program you Run

  • As with other languages, you type your source code into source files, using the editor of your choice

  • A C++ compiler translates the source code into object code, after checking for errors

  • A linker combines your object code with other object code from existing libraries to create an executable file


Tools needed for projects

Tools Needed for Projects

  • Projects will be submitted electronically and graded on the elaine workstations

  • Must have ssh client to connect

  • Must be able to edit files in UNIX/Linux (with vi or emacs, for example), or transfer them using SecureFX

  • Must be able to use GNU C++ compiler

  • Visit computing.stanford.edu for needed software


Essential software link

Essential Software Link


Link to download page

Link to Download Page


Download securecrt

Download SecureCRT


Launch securecrt

Launch SecureCRT


Create new session

Create New Session


New session wizard

New Session Wizard


New session wizard cont d

New Session Wizard, cont’d


New session wizard cont d1

New Session Wizard, cont’d


Connect to elaine

Connect to Elaine


Use sunet password

Use SUNet Password


Connected

Connected!


Getting started in linux

Getting Started in Linux


Launching the vi editor

Launching the vi editor


Very basic vi usage

VERY Basic vi Usage

  • Type i to enter insert mode

  • Use ESC key to exit insert mode

  • Commands (when not in insert mode):

    • h: left, l: right, j: down, k: up

    • x: delete character at cursor

  • Colon takes you to command prompt. There, use w to save, and q to exit

  • Resource for learning vi:

    • http://www.infobound.com/vi.html


Typing in your program

Typing in your program


Saving

Saving…


Saved

…saved!


Exiting vi

Exiting vi


Creating executables in linux

Creating Executables in Linux

  • The c++ command invokes the GNU C++ compiler on given source files, indicated by .cpp extension

  • By default, it will also invoke the linker to create an executable

    • Use –c option to only create an object file which has .o extension

  • By default, executable is called a.out

    • Use –o option to specify another name

    • Can be run from the command prompt


Compiling and executing

Compiling and Executing


Dissecting hello cpp

Dissecting hello.cpp

// #include is a preprocessor directive that

// specifies a header file to be included in the

// program (in this case, iostream)

#include <iostream>

// When a program is run, its main function is

// invoked. It returns an integer (int) value

// indicating its status (not done here, though)

int main()

{

// std::cout denotes the “standard output”

// device, which is normally the screen. The

// << operator, in this case, is used to

// write data to this device.

std::cout << "Hello world!" << std::endl;

}


Delegating or modularity

Delegating (or: Modularity!)

hello.cpp: (subroutine)

#include <iostream>

void say_hello() // void means “does not return a value”

{

std::cout << "Hello world!" << std::endl;

}

hellomain.cpp: (main program)

void say_hello(); // external functions must be declared

int main()

{

say_hello(); // main passes the buck to say_hello

}


Compiling multiple files

Compiling Multiple Files

Neither hello.cpp nor hellomain.cpp is a complete program, so we use –c to compile only, and not link

bramble06:~/demo211> c++ -c hello.cpp

bramble06:~/demo211> c++ -c hellomain.cpp

The previous commands created object (.o) files, which are now linked to create the executable program “hello”

bramble06:~/demo211> c++ -o hello hello.o hellomain.o

The ls command lists the current directory (like dir in Windows). The a.out is from before

bramble06:~/demo211> ls

a.out hello hello.cpp hello.o hellomain.cpp hellomain.o

The “.” is used to denote the current directory, which, by default, is not in the search path used to locate programs

bramble06:~/demo211> ./hello

Hello world!

bramble06:~/demo211>


Managing projects with make

Managing Projects with make

  • Managing projects with several source files can be tedious

  • When you modify a source file, you need to recompile that file, and re-link

  • The make command recompiles any out-of-date files automatically

  • Useful for tasks such as cleaning up unnecessary files or changing compiler options


Creating makefiles

Creating Makefiles

  • The make command uses a file called Makefile to determine how to proceed

  • Makefile contains rules of the form

    target: prerequisites

    command

    where command builds target from the prerequisites

  • Can define variables for convenience


Sample makefile

Sample Makefile

# All object files that must be linked into final executable

OBJ= hello.o hellomain.o

# Rule for building executable from object files

# $@ is shorthand for the target of the rule

hello: ${OBJ}

c++ -o $@ ${OBJ}

# Rule for compiling individual sources files into object files

# $< is shorthand for the first prerequisite

${OBJ}: %.o: %.cpp

c++ -c $<

# Rule to clean up all output files

clean:

rm -f hello ${OBJ}


Using make

Using make

With the Makefile, building executable is easy!

bramble06:~/demo211> make

c++ -c hello.cpp

c++ -c hellomain.cpp

c++ -o hello hello.o hellomain.o

Reset hello.cpp’s modified time to force recompile

bramble06:~/demo211> touch hello.cpp

Note that only hello.cpp is recompiled

bramble06:~/demo211> make

c++ -c hello.cpp

c++ -o hello hello.o hellomain.o

This removes all output files

bramble06:~/demo211> make clean

rm -f hello hello.o hellomain.o


Alternative approaches

Alternative Approaches

  • Can edit source files on your computer, and transfer using SecureFX (available on Essential Software page), or Fetch if you’re using a Mac

  • Can do all of your work in Windows using MinGW Developer Studio

  • In this case, should still compile and run final program on elaine before submitting


Mingw developer studio

MinGW Developer Studio

  • Abbreviated as MDS

  • Available from http://www.parinyasoft.com/

  • MDS is an Integrated Development Environment (IDE), with editing, compiling and debugging performed inside the studio

  • Uses gcc compiler


Launching mds

Launching MDS


Creating a new project

Creating a New Project


Mds project view

MDS Project View


Adding a new source file

Adding a New Source File


Typing in your code

Typing in Your Code


Building the executable

Building the Executable


The build process

The Build Process


Executing in mds

Executing in MDS


Execution of console app

Execution of Console App


What about mac users

What About Mac Users?

  • Mac OS X is built on top of FreeBSD UNIX, so Linux discussion applies

  • Can use ssh to connect to elaine

  • OS X does not come with GNU compilers

  • Can obtain freely from Apple Developer Connection by downloading xcode package (registration required)

  • Visit http://connect.apple.com


Next time

Next Time

Learning some fundamentals of C++

  • Program Structure

  • Simple Variables

  • Literals

  • Types

  • Basic Exception Handling


  • Login