Notes for Advanced Linux Programming – 1. Getting Started

來源:互聯網
上載者:User
文章目錄
  • 1.1.1. Create the source code files
  • 1.1.2. Compiling a Single Source File
  • 1.1.3. Linking Object Files
  • 1.3.1. Compiling with Debugging Information
  • 1.3.2. Running GDB
1. Getting Started1.1. Compiling with GCC1.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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.