Notes for Advanced Linux Programming - 1. Getting Started
1. Getting Started
1.1. Compiling with GCC
1.1.1. Create the source code files
-
(main.c) C source file—main.c
#include <stdio.h>
#include “reciprocal.hpp”
int main (int argc, char **argv)
{
int i;
i = atoi (argv[1]);
printf (“The reciprocal of %d is %g\n”, i, reciprocal (i));
return 0;
}
-
(reciprocal.cpp) C++ source file—reciprocal.cpp
#include <cassert>
#include “reciprocal.hpp”
double reciprocal (int i) {
// I should be non-zero.
assert (i != 0);
return 1.0/i;
}
-
(reciprocal.hpp) Header file—reciprocal.hpp
#ifdef __cplusplus
extern “C” {
#endif
extern double reciprocal (int i);
#ifdef __cplusplus
}
#endif
1.1.2. Compiling a Single Source File
-
compile the main.c source file:
% gcc -c main.c
-
Compile the reciprocal.cpp source file:
% g++ -c reciprocal.cpp
-
The -I option is to tell GCC where to search for header files. By default, GCC looks in the current directory and in the directories where headers for the standard libraries are installed.
% g++ -c -I ../include reciprocal.cpp
-
If you want to define macros on the command line, use –D
-
If you want to turn off the assert check in reciprocal.cpp by defining the macro NDEBUG, you can simply define NDEBUG on the command line.
% g++ -c –D NDEBUG reciprocal.cpp
-
You can define NDEBUG to some particular value.
% g++ -c -D NDEBUG=3 reciprocal.cpp
-
You can let GCC optimize the code to a certain level.
% g++ -c -O2 reciprocal.cpp
1.1.3. Linking Object Files
-
You can use g++ to link a program that contains C++ code
% g++ -o reciprocal main.o reciprocal.o
-
If you need to link in another library, you have to specify the library with –l option.
For example, the Pluggable Authentication Module (PAM) library is called libpam.a, to link in it,
% g++ -o reciprocal main.o reciprocal.o –lpam
The compiler automatically adds the lib prefix and .a suffix.
-
The linker looks for libraries in the /lib and /usr/lib directories that contain the standard system libraries. If you want the linker to search other directories as well, you should use the -L option.
% g++ -o reciprocal main.o reciprocal.o -L/usr/local/lib/pam –lpam
1.2 Automating the Process with GNU Make
-
Here' s what Makefile contains:
reciprocal: main.o reciprocal.o
g++ $(CFLAGS) -o reciprocal main.o reciprocal.o
main.o: main.c reciprocal.hpp
gcc $(CFLAGS) -c main.c
reciprocal.o: reciprocal.cpp reciprocal.hpp
g++ $(CFLAGS) -c reciprocal.cpp
clean:
rm -f *.o reciprocal
-
Targets are listed on the left, followed by a colon and then any dependencies.
-
The rule to build that target is on the next line.
-
The line with the rule on it must start with a Tab character
-
The $(CFLAGS) is a make variable.You can define this variable either in the Makefile itself or on the command line.
% make CFLAGS=-O2
gcc -O2 -c main.c
g++ -O2 -c reciprocal.cpp
g++ -O2 -o reciprocal main.o reciprocal.o
1.3 Debugging with GNU Debugger (GDB)
1.3.1. Compiling with Debugging Information
-
If you want to compile with debugging information, add the –g switch on the compilation command line.
% make CFLAGS=-g
gcc -g -c main.c
g++ -g -c reciprocal.cpp
g++ -g -o reciprocal main.o reciprocal.o
1.3.2. Running GDB
-
start up gdb by typing:
% gdb reciprocal
-
Enter the command run and any program arguments to run the program.
(gdb) run
Starting program: reciprocal
-
You can see the stack by using the where command:
(gdb) where
#0 __strtol_internal (nptr=0x0, endptr=0x0, base=10, group=0)
at strtol.c:287
#1 0x40096fb6 in atoi (nptr=0x0) at ../stdlib/stdlib.h:251
#2 0x804863e in main (argc=1, argv=0xbffff5e4) at main.c:8
-
You can go up two levels in the stack by using the up command:
(gdb) up 2
#2 0x804863e in main (argc=1, argv=0xbffff5e4) at main.c:8
8 i = atoi (argv[1]);
-
You can view the value of variables using the print command:
(gdb) print argv[1]
$2 = 0x0
-
You can set a breakpoint by using the break command:
(gdb) break main
Breakpoint 1 at 0x804862e: file main.c, line 8.
-
You can step over the call to atoi using the next command:
(gdb) next
9 printf (“The reciprocal of %d is %g\n”, i, reciprocal (i));
-
If you want to see what’s going on inside reciprocal, use the step command like this:
(gdb) step
reciprocal (i=7) at reciprocal.cpp:6
6 assert (i != 0);