Linux C July 27, 2016 Contents 1 Linux IDE 1 2 GCC 3 2.1 hello.c hello.exe........................... 5 2.2............................... 9 2.2.1 -Wall................................ 9 2.2.2 -E.................................. 10 2.2.3 -D.................................. 11 2.2.4.............................. 13 1 Linux IDE IDE Integrated Development Environment, IDE Windows Visual Studio Java Eclipse IDE IDE IDE IDE IDE IDE = + + VS Eclipse IDE Emacs Emacs 1
1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Unix do one thing, and do it well Emacs do one thing, and do it well Emacs IDE VC C/C++ eclipse Java MS-Word Everything Emacs VC/eclipse/MS-Word Emacs 18 IDE Emacs Emacs Emacs Ctrl-h t, 2
2 GCC Ctrl-h i m emacs Emacs ; Ctrl-h i m emacs lisp intro Emacs Lisp Ctrl-h i m elisp elisp Google Happy emacsing! 2 GCC GNU Compiler Collection (GCC) GNU gcc: c g++: c++ gcj: java gfortran: Fortran GNAT: ada gccgo: Go GCC gcc GCC gcc c gcc GNU Richard Stallman gcc 1987 Unix-like Linux gcc gcc x86, ARM x86 ARM gcc man gcc wc -l gcc gcc gcc c c hello.c, 1 #include <stdio.h> 2 3 int main(void) 4 { 5 printf("hello, world!\n"); 6 return 0; 7 } 3
2 GCC gcc hello.c a.out a.out gcc hello.c -o hello hello -o output, hello./hello./ hello.c Hello, world! hello.c, 1 #include <stdio.h> 2 3 int main(void) 4 { 5 printf("hello, world!\n") 6 return 0; 7 } gcc hello.c, 1 hello.c: In function main : 2 hello.c:6:3: error: expected ; before return 3 return 0; 4 ^ OMG! 1. main 2. 6 3 r 3. ^ r, hello.c, return ) 1 #include <stdio.h> 2 3 int main(void) 4 { 5 printf("hello, world!\n"); 6 return 0; 7 } 1 hello.c: In function main : 2 hello.c:5:3: error: stray \357 in program 4
2.1 hello.c hello.exe 2 GCC 3 printf("hello, world!\n"); 4 ^ 5 hello.c:5:3: error: stray \274 in program 6 hello.c:5:3: error: stray \233 in program 7 hello.c:6:3: error: expected ; before return 8 return 0; 9 ^ OMG!!! 2.1 hello.c hello.exe gcc hello.c -o hello.exe,, hello.exe, 5
2.1 hello.c hello.exe 2 GCC Emacs nano, vim, Hello, world! Emacs C C (c preprocessor) # 6
2.1 hello.c hello.exe 2 GCC #include<stdio.h> include stdio.h 1 #include<stdio.h> stdio.h cpp cpp hello.c #include<stdio.h> 800 #define SQR(x) (x * x), SQR(x), (x * x). cpp C gcc (Compiler) cpp Google C CPU C 1. C C i++ movl addl movl $0, -4(%rbp) $1, -4(%rbp) -4(%rbp), %eax i++ C hello.c hello.s: gcc -S hello.c hello.s 1 ^^I.file "hello.c" 2 ^^I.section.rodata 3.LC0: 4 ^^I.string "Hello, world!" 5 ^^I.text 1 Debian GNU/Linux /usr/include/stdio.h 7
2.1 hello.c hello.exe 2 GCC 6 ^^I.globl main 7 ^^I.type main, @function 8 main: 9.LFB0: 10 ^^I.cfi_startproc 11 ^^Ipushq %rbp 12 ^^I.cfi_def_cfa_offset 16 13 ^^I.cfi_offset 6, -16 14 ^^Imovq %rsp, %rbp 15 ^^I.cfi_def_cfa_register 6 16 ^^Imovl $.LC0, %edi 17 ^^Icall puts 18 ^^Imovl $0, %eax 19 ^^Ipopq %rbp 20 ^^I.cfi_def_cfa 7, 8 21 ^^Iret 22 ^^I.cfi_endproc 23.LFE0: 24 ^^I.size main,.-main 25 ^^I.ident "GCC: (Debian 5.4.0-6) 5.4.0 20160609" 26 ^^I.section.note.GNU-stack,"",@progbits hello.c C 2. x86 ARM, PowerPC, M68000,... CPU (Assembler) object file.o GNU Assembler (gas).o gcc -c hello.c gcc gas hello.o cat hello.o CPU hd hello.o 8
2.2 2 GCC (Linker).o ld hello.c,.o.o hello.c.o printf().o.o printf() hello.o printf().o 2 (Debugger) bug gdb Hello, world! bug hello.c hello.exe 2.2 gcc C -o, 2.2.1 -Wall W warning, all all -Warning wall.c 1 #include <stdio.h> 2 3 int main (void) 4 { 2 libc.so so shared object Unix Google shared object 9
2.2 2 GCC 5 printf ("Two plus two is %f\n", 4); 6 return 0; 7 } -Wall gcc wall.c, a.out Two plus two is 0.000000 -Wall gcc -Wall wall.c, wall.c: In function main : wall.c:5:11: warning: format %f expects argument of type double, but argument 2 has type int [-Wformat=] printf ("Two plus two is %f\n", 4); ^ %f -Wall, W 2.2.2 -E -E gcc, cpp gcc -E hello.c cpp hello.c cpp c preprocessor, Macro C Linux 1 #define INIT_LIST_HEAD(ptr) do { \ 2 ^^I (ptr)->next = (ptr);(ptr)->prev= (ptr); \ 3 ^^I} while(0) do-while INIT_LIST_HEAD(prt) do {} while(0) bug -E bug macro.c SQR(x) 1 #include <stdio.h> 2 #define SQR(x) (x * x) 3 int main() 4 { 5 int counter; /* counter for loop */ 6 for (counter = 0; counter < 5; ++counter) 7 { 8 ^^Iprintf("x %d, x squared %d\n", 9 ^^I counter+1, SQR(counter+1)); 10
2.2 2 GCC 10 } 11 return (0); 12 } SQR(x) x gcc -Wall macro.c, a.out, x 1, x squared 1 x 2, x squared 3 x 3, x squared 5 x 4, x squared 7 x 5, x squared 9 SQR(x) -E 1 /* 前 省 略 数 */ 2 int main() 3 { 4 int counter; 5 for (counter = 0; counter < 5; ++counter) 6 { 7 ^^Iprintf("x %d, x squared %d\n", 8 ^^I counter+1, (counter+1 * counter+1)); 9 } 10 return (0); 11 } 8 SQR(counter+1) (counter+1 * counter+1), ((counter+1) * (counter+1)), 1 #define SQR(x) (x * x) 1 #define SQR(x) ((x) * (x)) -E 2.2.3 -D Debug printf() bug printf() -D stackoverflow.c, 1 #include<stdio.h> 2 11
2.2 2 GCC 3 int i=0; 4 5 int main(void) 6 { 7 #ifdef DEBUG 8 printf("%d\t",i++); 9 #endif 10 main(); 11 return 0; 12 } 8 printf() i 7 9 gcc -Wall stackoverflow.c./a.out, Segmentation fault printf() gcc -Wall -DDEBUG stackoverflow.c./a.out, Segmentation fault =i= 523629 -DDEBUG printf() -D #ifdef DEBUG... #endif DEBUG Macro -DDEBUG #define DEBUG 1 #ifdef DEBUG DEBUG #endif -D, DEBUG #ifdef DEBUG false, printf() -DDEBUG, DEBUG #ifdef DEBUG true, printf() printf() -D 12
2.2 2 GCC 2.2.4 gcc -g gdb debug -l l link pthread_create() pthread_create() man pthread_create NAME pthread_create - create a new thread SYNOPSIS #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Compile and link with -pthread. 1. #include <pthread.h>; 2. (a) pthread_create() int; (b) 4 3. Compile and link with -pthread, pthread Linux C Emacs gcc gdb bug Happy hacking! 13