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:
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.
There are many reasons, the most important IMHO are:
gcc [ option | filename ]...
gcc [flags] file1 [file2 [file3 ...]] [-o output_file]
gcc foo1.c foo2.c -o foo
-ansi | This flag tells the compiler to enforce ANSI C standards |
-pedantic | More pedantic ansi, warnings for stuff you probably didn't mean. |
-Wall | Show all reasonable warnings (there are more). |
-g | Produce debug information, necessary for debugging. |
-llibrary | Links to a standard library. Use -lm to load the standard maths library. |
-c | Compile or assemble the source files, but do not link. The compiler output is object files corresponding to each source file. |
-S | Compile only; output assembly code. |
-E | Pre-process only. Output pre-processed code. |
-Dmacro | Define 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. |
gcc -ansi -pedantic -Wall file1 [file2 [file3...]] -o output
.C
, .cc
, .cxx
, .cpp
, or .c++
.g++ -Wall file1 [file2 [file3...]] -o output_file
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
make [target_name]
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
pinfo make
command, or by typing: info:make
in the Konqueror address line.emacs
type C-h t
(that is Control+h
and then t
), and you will get to an emacs tutorial (that can also be found in the help menu)Previous Line: C-p | ||||
:: | ||||
Backward: C-b | ... | Current Cursor Position | ... | Forward: C-f |
:: | ||||
Next Line: C-n |
M-f
and M-b
to move one word at a time instead of one character at a time.M-v
. Page Down is C-v
.C-e
. Beginning of the Line is C-a
.C-k
. C-k cuts until the end of line, typing C-k
many times will cut many lines. If you cut a few lines then move around and cut more lines. The first lines would be lost this way (not including possible undo).C-y
you can yank(paste) out however many times you want wherever you want.C-x u
which is the undo option it will undo the last act. You can do several of these in a row, notice there is a big difference between C-x u
and C-x C-u
you must let go of the Ctrl key, before hitting the u
key.C-x C-f
you can use the tab to complete file names as you can in the bash/tcsh command prompt.C-x C-s
which saves the current buffer(file) or C-x s
which will prompt to verify and save each and every open buffer.C-x C-c
.C-s
.M-%
.M-x compile
. A new buffer will open and it will display compilation results. You can go through them easily while having your cursor jump to where the errors are using C-x `
. (backquote).C-x 1
, (C-x 2
will split the buffer)M-/
.gdb program_name
(it can be run differently but for that RTFM)help
from the gdb command line and get a list of stuff to get help on, or type help <something>
, and get specific help on something - either a command or a topic name.pinfo gdb
or info gdb
if pinfo
is not available.run | Executes the program from the beginning. You can use: run < file_in > file_out to redirect I/O for the program being executed. |
break | Used to set breakpoints. Can be used in many ways:break line_number break function_name break file_name:line_number break class::method |
next | Proceed one command line while not entering function calls. |
step | Proceed one command line, step into function calls if necessary. |
cont | Continue until next breakpoint. |
where | Display call stack. |
print | Print a variable or an expression. Used print expression |
up/down | Move up and down in the call stack. Allows you to examine local variables of previous functions. |
<menu>->Status->Backtrace
.<menu>->Data->Detect Aliases
. In the latest version, this is default behaviour.Next
; use Step
when you specifically want to step into a function.valgrind -h
).--num-callers=NNN
which sets how far back to print the function call sequence when an error is detected, (sometimes the error actually occurs in a sequence of functions you didn't write). By default it is --num-callers=4
.valgrind --num-callers=15 progname
. It will run the program progname
and intercept calls to malloc/free/new/delete and monitor all memory uses.--gdb-attach=yes
that invokes gdb on the process when errors are detected, and let you debug the place of the error../configure
make
make install
docs/index.html
.