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);

转载于:https://www.cnblogs.com/forfuture1978/archive/2010/02/11/1667457.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值