Notes for Advanced Linux Programming - 1. Getting Started
来源:互联网 发布:java逻辑编程题 编辑:程序博客网 时间:2024/06/03 17:48
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);
- Notes for Advanced Linux Programming - 1. Getting Started
- Notes for Advanced Linux Programming - 3. Processes
- Notes for Advanced Linux Programming - 4. Threads
- Notes for Advanced Linux Programming - 6. Devices
- Notes for Advanced Linux Programming - 5. Interprocess Communication
- notes for linux programming
- NVIDIA CUDA Getting Started Guide for Linux
- Notes for Advanced Linux Programming - 2. Writing Good GNU/Linux Software
- Getting Started with Python Programming for Mac Users
- 2016.9.13 Programming for Everybody (Getting Started with Python)
- 读书笔记 - Beginning Linux Programming - Chapter 1 Getting Started
- Getting started with Shell Programming
- 《C++ GUI Programming with Qt4》.1.Getting Started
- NOTES ON GIT PRO CH1 GETTING STARTED
- Advanced Android: Getting Started with the NDK
- Advanced Android: Getting Started with the NDK
- Advanced Android: Getting Started with the NDK
- Getting started with the MeeGo SDK for Linux
- "数学传奇"丘成桐:《诗》明方向,《史记》教抉择
- As3中删除数组的重复值
- Memcpy(), a fast and portable implementation
- jquery实现点击信息弹出层的功能
- 亲密接触C可变参数函数
- Notes for Advanced Linux Programming - 1. Getting Started
- 多线程同步机制摘要
- Notes for Advanced Linux Programming - 2. Writing Good GNU/Linux Software
- xml dom
- PC(项目经理)-职责
- 奥斯特洛夫斯基谈“幸福”
- Visual C++中的异常处理浅析
- 也来聊斋
- 一个减少多进程下python内存占用的小技巧