1: DEBUG instance [root @ localhostdebug] # vimnull. cinta (int * p); intmain (void) {int * p0; returna (p);} inta (int * p) {inty * p; returny ;}~ [Root @ localhostdebug] # ulimit-cunlimited [root @ localhostdebug] # gcc-Wall? Gnul
1. DEBUG instance
[Root @ localhost debug] # vim null. c
Int a (int * p );
Int main (voId)
{
Int * p = 0;
Return a (p );
}
Int a (int * p)
{
Int y = * p;
Return y;
}
~
[Root @ localhost debug] #Ulimit-C unlimitEd
[Root @ localhost debug] # gcc-Wall? G null. c
[Root @ localhost debug] #./a. out
Segmentation fault (coreDuMped)
[Root @ localhost debug] #Ls
Null. c a. out core.21982
[Root @ localhost debug] # yum-y install gdb
[Root @ localhost debug] # gdb a. out core.21982
.................
.................
.................
(Gdb) print p
No symbol table is loaded. Use"File"Command.
(Gdb) print p
$1 = (int *) 0x0
(Gdb) backTrAce
#0 0x08048389 in a (p = 0x0) at null. c: 10
#1 0x08048377 in main () at null. c: 6
(Gdb) quit (gdb)
Second: optimize the instance (here the main use of-O0-3 and no-loops)
[Root @ localhost opm] #CatTest. c
# INcLude
Double powern (double d, unsigned n)
{
Double x = 1.0;
Unsigned j;
For (j = 1; j
X * = d;
Return x;
}
Int main (void)
{
DoubleSuM = 0.0;
Unsigned I;
For (I = 1; I <= 100000000; I ++)
{
Sum + = powern (I, I % 5 );
}
Printf ("sum = % g \ n", sum );
Return 0;
}
[Root @ localhost opm] # gcc-Wall-O0 test. c-o o0 // The optimization level is 0-not optimized
[Root @ localhost opm] #Time./O0
Sum = 5e + 30
Real 0m2. 815 s
User 0m2. 799 s
Sys 0m0. 013 s
[Root @ localhost opm] # gcc-Wall-O1 test. c-o o1 // The optimization level is 1.
[Root @ localhost opm] # time./o1
Sum = 5e + 30
Real 0m1. 849 s
User 0m1. 843 s
Sys 0m0. 006 s
[Root @ localhost opm] # gcc-Wall-O2 test. c-o o2 // The optimization level is 2.
[Root @ localhost opm] # time./o2
Sum = 5e + 30
Real 0m1. 923 s
User 0m1. 910 s
Sys 0m0. 011 s
[Root @ localhost opm] # gcc-Wall-O3 test. c-o o3 // The optimization level is 1.
[Root @ localhost opm] # time./o3
Sum = 5e + 30
Real 0m1. 460 s
User 0m1. 453 s
Sys 0m0. 006 s
[Root @ localhost opm] # gcc-Wall-O3-funroll-loops test. c-o o4 // Add no-loop Optimization
[Root @ localhost opm] # time./o4
Sum = 5e + 30
Real 0m1. 322 s
User 0m1. 308 s
Sys 0m0. 014 s
Third: optimize and help to discover DEBUG
[Root @ localhost O] # vim uninit. c
Int sign (int x)
{
Int s;
If (x> 0)
S = 1;
Else if (x <0)
S =-1;
Return s;
}
[Root @ localhost O] # gcc-Wall-c uninit. c
[Root @ localhost O] # gcc-Wall-O1-c uninit. c
Uninit. c: In function 'sign ':
Uninit. c: 3: warning:'s 'may be uSedUninitialized in this function
[Root @ localhost O] # gcc-Wall-O2-c uninit. c
Uninit. c: In function 'sign ':
Uninit. c: 3: warning:'s 'may be used uninitialized in this function
[Root @ localhost O] # gcc-Wall-O3-c uninit. c
Uninit. c: In function 'sign ':
Uninit. c: 3: warning:'s 'may be used uninitialized in this function
Compile Process Description Example
[Root @ localhost hello] # vim hello. c
# Include
Int main (void)
{
Printf ("Hello World !!! \ N ");
Return 0;
}
4. Finally, list the compiled process instances.
1. pre-processor stage (CpP hello. c> hello. I)
[Root @ localhost hello] # cpp hello. c> hello. I
2. compiler stage (gcc-Wall-S hello. I)
[Root @ localhost hello] # gcc-Wall-S hello. I
3. assembler stage (as hello. s-o hello. o)
[Root @ localhost hello] # as hello. s-o hello. o
4. connector stage (gcc hello. o)
[Root @ localhost hello] # gcc hello. o
[Root @ localhost hello] #./a. out
Hello World !!!
[Root @ localhost hello] # file a. out // check the information of the executable file
A. out: ELF 32-bit LSBExECutAble, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
[Root @ localhost hello] # lDdA. out // check the library files used
Linux-gate.so.1 => (0x002ab000)
Libc. so.6 =>/lib/libc. so.6 (0x00b3a000)
/Lib/ld-linux.so.2 (0x00b17000)
End
Summary: This article is a note for instructor Xiao Bu's video teaching, in order to deepen his memory and record it.