Recitation%20Week%203 - PowerPoint PPT Presentation

About This Presentation
Title:

Recitation%20Week%203

Description:

Object Oriented Programming COP3330 / CGS5409 – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 23
Provided by: Michael4291
Learn more at: https://ww2.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Recitation%20Week%203


1
Recitation Week 3
  • Object Oriented Programming
  • COP3330 / CGS5409

2
Todays Recitation
  • Compiling with g
  • Using Makefiles
  • Debugging

3
Compiling C / C programs with gcc g
  • The base command for the Gnu C compiler is "gcc"
  • The base command for the Gnu C compiler is "g"

4
Single File Programs
  • To compile a program that is in a single file,
    the easiest compilation uses the command format
  • g ltfilenamegt
  • Where the filename ends with ".cpp
  • Example
  • g prog1.cpp

5
Multiple File Programs
  • To invoke the Compile stage, which translates
    source code (.cpp files) into object code (.o
    files), use the -c flag. Format
  • g -c ltfilenamegt
  • To name a target (something other than the
    default filename, use the -o flag. Format
  • g -o lttarget_namegt ltremainder of commandgt

6
Multiple File Examples
  • g -o yadda.o -c fraction.cpp
  • This command invokes just the compile stage on
    fraction.cpp, but names the object code file
    "yadda.o" (instead of the default "fraction.o").
  • g -o bob.exe circle.o main.o
  • This command links the two object code files
    ("circle.o" and "main.o") into an executable,
    called "bob.exe" (instead of the default
    "a.out").
  • g -o myProgram thing.cpp main.cpp
  • This command compiles and links (since -c not
    used) the code files "thing.cpp" and "main.cpp"
    together into the executable program called
    "myProgram".

7
Reminders
  • Source code is just text!
  • For the purposes of assignments, ANY text editor
    can be used to
  • Practice with at least one Unix text editor
    create code files
  • For unix beginners, "pico" is recommended, due to
    easy learning curve.
  • Emacs, Vim, MUCH more powerful

8
CS Account login
  • Understand how to log into both CS machines
  • linprog.cs.fsu.edu
  • program.cs.fsu.edu
  • Use SSH (Secure SHell) client to login
  • Files created on a windows machine can be FTP-ed
    to CS accounts with the SFTP feature built into
    the SSH software

9
SSH / SCP
  • Usage
  • sftp username_at_hostname
  • get filename - retrieve remote file
  • put filename - upload local file
  • Standard Unix commands
  • cd, ls, pwd, chmod, rename, rm, mkdir, rmdir,
    help, quit
  • Alternatively, GUI File Managers
  • WinSCP - Free Windows client with SFTP capability
  • FileZilla - Open source cross-platform GUI client

10
Makefiles
  • Unix system has what is called a make utility
  • Configuration file to assist with compilation
  • Simple text file, should be named either
    makefile or Makefile

11
Makefiles
  • Idea of the target
  • What is able to be made?
  • Dependency list
  • What needs to be re-made each time?
  • Command list, and formatting
  • i.e. it must be preceded by a single tab
    character
  • Extra targets, like clean, for cleanup
  • target that lists a cleanup command (like the
    remove rm command)
  • More than one target
  • placing a target like all at the top, and
    listing the executables made by the file as the
    dependency list

12
Sample Makefile
  • This is a comment line
  • Sample makefile for fraction class
  • frac main.o frac.o
  • g -o frac main.o frac.o
  • main.o main.cpp frac.h
  • g -c main.cpp
  • frac.o frac.cpp frac.h
  • g -c frac.cpp
  • clean
  • rm .o frac

13
Sample Makefile
  • frac main.o frac.o
  • g -o frac main.o frac.o
  • Specifies frac as the target
  • Depends on main.o and frac.o
  • If either of these files changed since the last
    build, then frac must be rebuilt
  • Links two object code files together into a
    target executable called frac

14
Sample Makefile
  • main.o main.cpp frac.h
  • g -c main.cpp
  • Specifies how to built the target main.o
  • Depends on main.cpp and frac.h
  • If either file changes, main.o must be rebuilt
  • Uses normal g commands for the compile stage

15
Sample Makefile
  • Any section can be invoked specifically with the
    command
  • make lttarget_namegt
  • For instance, to build only the frac.o target,
    use
  • make frac.o

16
Sample Makefile
  • clean
  • rm .o frac
  • The target name is clean
  • Executes the remove command (rm)
  • Removes the object code file(s) and the
    executable(s) from the current directory

17
Types of Errors
  • Compilation errors -- usually syntax errors,
    undeclared variables and functions, improper
    function calls.
  • Linker errors -- usually involve undefined
    functions or multiply-defined functions or
    symbols
  • Run-time errors -- two varieties.
  • Fatal -- cause program to crash during execution
  • Non-fatal (or logical) -- don't crash the
    program, but produce erroneous results.

18
Error Reporting
  • Compile stage errorsĀ  These are errors that will
    be reported by the compiler, which usually
    provides a filename and line number indicating
    where it ran into trouble, for each error
    reported.
  • Linking stage errors These errors, also reported
    by the compiler, do not usually contain line
    numbers, since the linker works on object code,
    not on the original source code.
  • Run-time errorsĀ  These must be tested while
    running a fully-compiled program.

19
Debugging Compile Stage Errors
  • Always start at the top of the list of errors.
    Fix the first error, then recompile and see what
    is left.
  • If a list of errors is too long, compile and
    debug one file at a time.
  • When searching for an error, start with the
    indicated line number, but also look in the
    vicinity (usually previous lines) for the
    possible error.
  • Compile portions of programs as you go -- don't
    wait until the program is fully written to do the
    first compile!

20
Debugging Linking Stage Errors
  • Learn what kinds of problems cause linker errors
    (usually problems with agreement between
    definitions and calls).
  • Linker errors usually specify some kind of symbol
    (used by the compiler), which often resembles a
    function or variable name. This is usually a good
    clue.
  • Learn about good compilation techniques and
    pre-processor directives that help avoid linker
    errors.

21
Debugging Compile Stage Errors
  • To catch fatal errors, try to wedge the mistake
    between extra printout statements to locate the
    cause.
  • To catch logic errors, place extra printout
    statements in code while testing (to be removed
    in the finished version). Especially, print out
    values of internal variables to locate
    computation problems.

22
Questions?
Write a Comment
User Comments (0)
About PowerShow.com