Notes for Advanced Linux Programming - 1. Getting Started
2010-02-11 11:54
441 查看
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 __cplusplusextern “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.cCompile the reciprocal.cpp source file:
% g++ -c reciprocal.cppThe -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.cppIf 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.cppYou can define NDEBUG to some particular value.
% g++ -c -D NDEBUG=3 reciprocal.cppYou can let GCC optimize the code to a certain level.
% g++ -c -O2 reciprocal.cpp1.1.3. Linking Object Files
You can use g++ to link a program that contains C++ code
% g++ -o reciprocal main.o reciprocal.oIf 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 –lpam1.2 Automating the Process with GNU Make
Here' s what Makefile contains:
reciprocal: main.o reciprocal.og++ $(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=-O2gcc -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=-ggcc -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 reciprocalEnter the command run and any program arguments to run the program.
(gdb) runStarting 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 mainBreakpoint 1 at 0x804862e: file main.c, line 8.
You can step over the call to atoi using the next command:
(gdb) next9 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) stepreciprocal (i=7) at reciprocal.cpp:6
6 assert (i != 0);
相关文章推荐
- Notes for Advanced Linux Programming - 1. Getting Started
- Notes for Advanced Linux Programming - 2. Writing Good GNU/Linux Software
- Notes for Advanced Linux Programming - 6. Devices
- Notes for Advanced Linux Programming - 3. Processes
- Notes for Advanced Linux Programming - 2. Writing Good GNU/Linux Software
- Notes for Advanced Linux Programming
- Notes for Advanced Linux Programming - 3. Processes
- Notes for Advanced Linux Programming - 4. Threads
- Notes for Advanced Linux Programming - 4. Threads
- Notes for Advanced Linux Programming - 5. Interprocess Communication
- Notes for Advanced Linux Programming - 5. Interprocess Communication
- Notes for Advanced Linux Programming - 6. Devices
- Serial Notes for Linux Programming Interface
- notes for linux programming
- Beginning Linux Programming 4th Edition 学习笔记 - Chapter 1:Getting started
- Data structures for game programming learning notes (五)——linked list
- 读书笔记 - Beginning Linux Programming - Chapter 1 Getting Started
- Linux System Programming note 4 —— Advanced File I/O
- notes for the c programming language
- 读《New Riders - Advanced Linux Programming.chm》笔记1(未完)