Software Development under Linux

In this lecture we are going to cover what you need to know in order to start programming in C/C++ in a Linux environment. We will cover the following:

License

This document is Copyright by Meir Maor, 2004, and is available under the terms of the Creative Commons Attribution-ShareAlike License 4.0 Unported (or at your option any later version). Whatever source code is provided is under BSD-3-Clause

Shlomi Fish hereby places his changes under CC-by 4.0-or later with source code changes under BSD-3-Clause.


1. Why Program under Linux? (Briefly)

There are many reasons, the most important IMHO are:


2. Use of gcc/g++


2.1. Important gcc Flags

-ansiThis flag tells the compiler to enforce ANSI C standards
-pedanticMore pedantic ansi, warnings for stuff you probably didn't mean.
-WallShow all reasonable warnings (there are more).
-gProduce debug information, necessary for debugging.
-llibraryLinks to a standard library. Use -lm to load the standard maths library.
-cCompile or assemble the source files, but do not link. The compiler output is object files corresponding to each source file.
-SCompile only; output assembly code.
-EPre-process only. Output pre-processed code.
-DmacroDefine a macro, one can also use -Dmacro=val in order to assign a value for the macro. This will be used for preprocessing all files.

2.2. g++


3. Writing Makefiles


3.1. Makefile Basics

Example

foo: foo.o helper.o
    gcc -Wall foo.o helper.o -o foo

bar: bar.o helper.o
    gcc -Wall bar.o helper.o -o bar

bar.o: bar.c
    gcc bar.c -c -o bar

foo.o: foo.c foo.h
    gcc -ansi -Wall -c foo.c -o foo.o

3.2. Makefile Variables

CC= gcc
LD= gcc
CC_FLAGS = -ansi -Wall -c
LD_FLAGS = -Wall

all: foo bar

foo: foo.o helper.o
    $(LD) $(LD_FLAGS) foo.o helper.o -o foo
bar: bar.o helper.o
    $(LD) $(LD_FLAGS) bar.o helper.o -o bar

bar.o: bar.c
    $(CC) $(CC_FLAGS) bar.c
foo.o: foo.c foo.h
    $(CC) $(CC_FLAGS) foo.c

clean: rm -f *.o foo bar

3.3. Final Notes

How Make Works

More Information


4. The Almighty Emacs

Previous Line: C-p
::
Backward: C-b...Current Cursor Position...Forward: C-f
::
Next Line: C-n

4.1. More Emacs


4.2. Yet More Emacs


4.3. Emacs - The End (Well at Least the Last Slide)


5. The gdb Debugger


5.1. Basic gdb Commands

runExecutes the program from the beginning. You can use: run < file_in > file_out to redirect I/O for the program being executed.
breakUsed to set breakpoints. Can be used in many ways:
break line_number
break function_name
break file_name:line_number
break class::method
nextProceed one command line while not entering function calls.
stepProceed one command line, step into function calls if necessary.
contContinue until next breakpoint.
whereDisplay call stack.
printPrint a variable or an expression. Used print expression
up/downMove up and down in the call stack. Allows you to examine local variables of previous functions.

6. DDD - The Data Display Debugger


6.1. DDD Debugging Tips


7. valgrind - a Good Tool to Detect Memory Problems


7.1. More Valgrind