Debugging Flashcards
What is the problem with printf for debugging
- Lots of work
- output is buffered
- trave statements mess up output
How do you use lldb(mac) or gdb(linux)?
Compiling:
clang -g -O0
Then run the executable in the debugger:
lldb a.out
What are the basic lldb commands?
- r - run
- b 18 - breakpoint set
- s - step - stepping in to functions
- n - next: stepping over function calls
- c - continue execution
Why use a a Makefile?
Single-file programs do not work well when code gets large
- compilation can be slow
- hard to collaborate between multiple programmers
- cumbersome to edit
Large programs are split into multiple files:
- each file represents a partial program or module
- modules can be compiled separately or together
- modules can be shared between multiple programs
What are bad ways to compile programs with modules?
- Includning .c files
- multifile compilation (.c and .o files)
How do you work around having similar variables or function names when compiling multiple files
- static is the preferred method(like private in Java)
- use static for functions you odnt want exposed
How is static used for variables/functions and inside functions
static used on variable or function makes it not visible or accessable by other files.
Inside a function makes the variable maintain it’s value and doesn’t reset after function calls
What is make?
A utility for automatically compiling executables and libraries from source code
What is makefile?
- a script file that defines rules for what must be compiled and how to compile it
- compare file modification dates and to rebuild any file A dependant on another file B that has changed more recently than A
What are the makefile rule/syntax
- Command line but be indented by a single tab(spaces don’t work)
- make target - uses the file named Makefile in the current dir
- find rules for Makefile for building target
- if the file needs to be updated, it is
- alt
- make (builds first target in Makefile
- make -f makefilename, make -f makefilename target
- uses a filename other than Makefile
- clean:
- rm file1.o file2.0 file3.0 myprog
- is convention for removing all compiled files(but not source or header files)
- all: myprog myprog2
- has no commands but depends on myprog and myprog2
- make all ensures that myprog, myprog2 are up to date
- all rule often put first, so that typing make will build everything
How do makefile variables work?
define a variable:
NAME = value
$(NAME)
OBJFILES = file.0 file2.0 file3.0
PROGRAM = myprog
$(PROGRAM): $(OBJFILES)
clang -g -Wall -o $(PROGRAM) $(OBJFILES)
clean:
rm $(OBJFILES) $(PROGRAM)
How do you tell a makefile where your header files are located?
clang -I ./include
What are the common causes of errors during linking?
- missspellings
- missing files
- missing libraries