object oriented programming cop3330 / cgs5409. compiling with g++ using makefiles debugging

22
Object Oriented Programming COP3330 / CGS5409

Upload: johnathan-beasley

Post on 19-Jan-2018

232 views

Category:

Documents


0 download

DESCRIPTION

 The base command for the Gnu C compiler is "gcc"  The base command for the Gnu C++ compiler is "g++"

TRANSCRIPT

Page 1: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

Object Oriented ProgrammingCOP3330 / CGS5409

Page 2: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

Compiling with g++ Using Makefiles Debugging

Page 3: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

The base command for the Gnu C compiler is "gcc"

The base command for the Gnu C++ compiler is "g++"

Page 4: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

To compile a program that is in a single file, the easiest compilation uses the command format:

g++ <filename>

Where the filename ends with ".cpp“ Example:

g++ prog1.cpp

Page 5: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

To invoke the Compile stage, which translates source code (.cpp files) into object code (.o files), use the -c flag. Format:

g++ -c <filename>

To name a target (something other than the default filename, use the -o flag. Format:

g++ -o <target_name> <remainder of command>

Page 6: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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".

Page 7: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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

Page 8: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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

Page 9: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

Usage:sftp [username@]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

Page 10: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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’

Page 11: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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

Page 12: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

# 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.hg++ -c main.cpp

frac.o: frac.cpp frac.h g++ -c frac.cpp

clean: rm *.o frac

Page 13: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

frac: main.o frac.o g++ -o frac main.o frac.o

Specifies ‘frac’ as the targetDepends on main.o and frac.oIf either of these files changed since the last build, then ‘frac’ must be rebuiltLinks two object code files together into a target executable called ‘frac’

Page 14: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

main.o: main.cpp frac.hg++ -c main.cpp

Specifies how to built the target ‘main.o’Depends on main.cpp and frac.hIf either file changes, main.o must be rebuiltUses normal g++ commands for the compile stage

Page 15: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

Any section can be invoked specifically with the command:

make <target_name>

For instance, to build only the ‘frac.o’ target, use:

make frac.o

Page 16: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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

Page 17: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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.

Page 18: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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.

Page 19: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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!

Page 20: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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.

Page 21: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging

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.

Page 22: Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging