Notes for Advanced Linux Programming-1. Getting Started

Source: Internet
Author: User
Document directory
  • 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 shoshould 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 special 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 shocould 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. 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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.