Download presentation
Presentation is loading. Please wait.
Published byJohnathan Beasley Modified over 9 years ago
1
Object Oriented Programming COP3330 / CGS5409
2
Compiling with g++ Using Makefiles Debugging
3
The base command for the Gnu C compiler is "gcc" The base command for the Gnu C++ compiler is "g++"
4
To compile a program that is in a single file, the easiest compilation uses the command format: g++ Where the filename ends with ".cpp“ Example: g++ prog1.cpp
5
To invoke the Compile stage, which translates source code (.cpp files) into object code (.o files), use the -c flag. Format: g++ -c To name a target (something other than the default filename, use the -o flag. Format: g++ -o
6
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
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
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
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 WinSCP ◦ FileZilla - Open source cross-platform GUI client FileZilla
10
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
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
# 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
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
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
Any section can be invoked specifically with the command: make For instance, to build only the ‘frac.o’ target, use: make frac.o
16
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
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
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
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
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
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.