• 1.1程序被其他程序翻译成不同的格式

  1.hello.c

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. printf("hello world\n");
  6. }

  2.编译过程

  3.编译系统
    预处理器、编译器、汇编器和链接器一起构成了编译系统

  预处理阶段。预处理器(cpp)根据以字符#开通的命令,修改原始的C程序。比如hello.c中第1行的#include<stdio.h>命令告诉预处理器读取系统头文件stdio.h的内容,并把它直接插入到程序的文本中。结果就得到了另一个C程序,通常是以.i作为文件扩展名

  预处理的过程主要处理包括以下过程:

  • 将所有的#define删除,并且展开所有的宏定义
  • 处理所有的条件预编译指令,比如#if #ifdef #elif #else #endif等
  • 处理#include 预编译指令,将被包含的文件插入到该预编译指令的位置。
  • 删除所有注释 “//”和”/* */”.
  • 添加行号和文件标识,以便编译时产生调试用的行号及编译错误警告行号。
  • 保留所有的#pragma编译器指令,因为编译器需要使用它们

  通常使用以下命令来进行预处理:

  gcc -E hello.c -o hello.i

  参数-E表示只进行预处理 或者也可以使用以下指令完成预处理过程

  cpp hello.c > hello.i      /*  cpp – The C Preprocessor  */

  1. # "hello.c"
  2. # "<built-in>"
  3. # "<command-line>"
  4. # "hello.c"
  5. # "/usr/include/stdio.h"
  6. # "/usr/include/stdio.h"
  7. # "/usr/include/features.h"
  8. # "/usr/include/features.h"
  9. # "/usr/include/x86_64-linux-gnu/bits/predefs.h"
  10. # "/usr/include/features.h"
  11. # "/usr/include/features.h"
  12. # "/usr/include/x86_64-linux-gnu/sys/cdefs.h"
  13. # "/usr/include/x86_64-linux-gnu/sys/cdefs.h"
  14. # "/usr/include/x86_64-linux-gnu/bits/wordsize.h"
  15. # "/usr/include/x86_64-linux-gnu/sys/cdefs.h"
  16. # "/usr/include/features.h"
  17. # "/usr/include/features.h"
  18. # "/usr/include/x86_64-linux-gnu/gnu/stubs.h"
  19.  
  20. # "/usr/include/x86_64-linux-gnu/bits/wordsize.h"
  21. # "/usr/include/x86_64-linux-gnu/gnu/stubs.h"
  22.  
  23. # "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h"
  24. # "/usr/include/x86_64-linux-gnu/gnu/stubs.h"
  25. # "/usr/include/features.h"
  26. # "/usr/include/stdio.h"
  27.  
  28. # "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h"
  29. # "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h"
  30. typedef long unsigned int size_t;
  31. # "/usr/include/stdio.h"
  32.  
  33. # "/usr/include/x86_64-linux-gnu/bits/types.h"
  34. # "/usr/include/x86_64-linux-gnu/bits/types.h"
  35. # "/usr/include/x86_64-linux-gnu/bits/wordsize.h"
  36. # "/usr/include/x86_64-linux-gnu/bits/types.h"
  37.  
  38. typedef unsigned char __u_char;
  39. typedef unsigned short int __u_short;
  40. typedef unsigned int __u_int;
  41. typedef unsigned long int __u_long;
  42.  
  43. typedef signed char __int8_t;
  44. typedef unsigned char __uint8_t;
  45. typedef signed short int __int16_t;
  46. typedef unsigned short int __uint16_t;
  47. typedef signed int __int32_t;
  48. typedef unsigned int __uint32_t;
  49.  
  50. typedef signed long int __int64_t;
  51. typedef unsigned long int __uint64_t;
  52.  
  53. typedef long int __quad_t;
  54. typedef unsigned long int __u_quad_t;
  55. # "/usr/include/x86_64-linux-gnu/bits/types.h"
  56. # "/usr/include/x86_64-linux-gnu/bits/typesizes.h"
  57. # "/usr/include/x86_64-linux-gnu/bits/types.h"
  58.  
  59. typedef unsigned long int __dev_t;
  60. typedef unsigned int __uid_t;
  61. typedef unsigned int __gid_t;
  62. typedef unsigned long int __ino_t;
  63. typedef unsigned long int __ino64_t;
  64. typedef unsigned int __mode_t;
  65. typedef unsigned long int __nlink_t;
  66. typedef long int __off_t;
  67. typedef long int __off64_t;
  68. typedef int __pid_t;
  69. typedef struct { int __val[]; } __fsid_t;
  70. typedef long int __clock_t;
  71. typedef unsigned long int __rlim_t;
  72. typedef unsigned long int __rlim64_t;
  73. typedef unsigned int __id_t;
  74. typedef long int __time_t;
  75. typedef unsigned int __useconds_t;
  76. typedef long int __suseconds_t;
  77.  
  78. typedef int __daddr_t;
  79. typedef long int __swblk_t;
  80. typedef int __key_t;
  81.  
  82. typedef int __clockid_t;
  83.  
  84. typedef void * __timer_t;
  85.  
  86. typedef long int __blksize_t;
  87.  
  88. typedef long int __blkcnt_t;
  89. typedef long int __blkcnt64_t;
  90.  
  91. typedef unsigned long int __fsblkcnt_t;
  92. typedef unsigned long int __fsblkcnt64_t;
  93.  
  94. typedef unsigned long int __fsfilcnt_t;
  95. typedef unsigned long int __fsfilcnt64_t;
  96.  
  97. typedef long int __ssize_t;
  98.  
  99. typedef __off64_t __loff_t;
  100. typedef __quad_t *__qaddr_t;
  101. typedef char *__caddr_t;
  102.  
  103. typedef long int __intptr_t;
  104.  
  105. typedef unsigned int __socklen_t;
  106. # "/usr/include/stdio.h"
  107. # "/usr/include/stdio.h"
  108. struct _IO_FILE;
  109.  
  110. typedef struct _IO_FILE FILE;
  111.  
  112. # "/usr/include/stdio.h"
  113. typedef struct _IO_FILE __FILE;
  114. # "/usr/include/stdio.h"
  115. # "/usr/include/libio.h"
  116. # "/usr/include/libio.h"
  117. # "/usr/include/_G_config.h"
  118. # "/usr/include/_G_config.h"
  119. # "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h"
  120. # "/usr/include/_G_config.h"
  121.  
  122. # "/usr/include/wchar.h"
  123. # "/usr/include/wchar.h"
  124. typedef struct
  125. {
  126. int __count;
  127. union
  128. {
  129.  
  130. unsigned int __wch;
  131.  
  132. char __wchb[];
  133. } __value;
  134. } __mbstate_t;
  135. # "/usr/include/_G_config.h"
  136.  
  137. typedef struct
  138. {
  139. __off_t __pos;
  140. __mbstate_t __state;
  141. } _G_fpos_t;
  142. typedef struct
  143. {
  144. __off64_t __pos;
  145. __mbstate_t __state;
  146. } _G_fpos64_t;
  147. # "/usr/include/_G_config.h"
  148. typedef int _G_int16_t __attribute__ ((__mode__ (__HI__)));
  149. typedef int _G_int32_t __attribute__ ((__mode__ (__SI__)));
  150. typedef unsigned int _G_uint16_t __attribute__ ((__mode__ (__HI__)));
  151. typedef unsigned int _G_uint32_t __attribute__ ((__mode__ (__SI__)));
  152. # "/usr/include/libio.h"
  153. # "/usr/include/libio.h"
  154. # "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stdarg.h"
  155. # "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stdarg.h"
  156. typedef __builtin_va_list __gnuc_va_list;
  157. # "/usr/include/libio.h"
  158. # "/usr/include/libio.h"
  159. struct _IO_jump_t; struct _IO_FILE;
  160. # "/usr/include/libio.h"
  161. typedef void _IO_lock_t;
  162.  
  163. struct _IO_marker {
  164. struct _IO_marker *_next;
  165. struct _IO_FILE *_sbuf;
  166.  
  167. int _pos;
  168. # "/usr/include/libio.h"
  169. };
  170.  
  171. enum __codecvt_result
  172. {
  173. __codecvt_ok,
  174. __codecvt_partial,
  175. __codecvt_error,
  176. __codecvt_noconv
  177. };
  178. # "/usr/include/libio.h"
  179. struct _IO_FILE {
  180. int _flags;
  181.  
  182. char* _IO_read_ptr;
  183. char* _IO_read_end;
  184. char* _IO_read_base;
  185. char* _IO_write_base;
  186. char* _IO_write_ptr;
  187. char* _IO_write_end;
  188. char* _IO_buf_base;
  189. char* _IO_buf_end;
  190.  
  191. char *_IO_save_base;
  192. char *_IO_backup_base;
  193. char *_IO_save_end;
  194.  
  195. struct _IO_marker *_markers;
  196.  
  197. struct _IO_FILE *_chain;
  198.  
  199. int _fileno;
  200.  
  201. int _flags2;
  202.  
  203. __off_t _old_offset;
  204.  
  205. unsigned short _cur_column;
  206. signed char _vtable_offset;
  207. char _shortbuf[];
  208.  
  209. _IO_lock_t *_lock;
  210. # "/usr/include/libio.h"
  211. __off64_t _offset;
  212. # "/usr/include/libio.h"
  213. void *__pad1;
  214. void *__pad2;
  215. void *__pad3;
  216. void *__pad4;
  217. size_t __pad5;
  218.  
  219. int _mode;
  220.  
  221. char _unused2[ * sizeof (int) - * sizeof (void *) - sizeof (size_t)];
  222.  
  223. };
  224.  
  225. typedef struct _IO_FILE _IO_FILE;
  226.  
  227. struct _IO_FILE_plus;
  228.  
  229. extern struct _IO_FILE_plus _IO_2_1_stdin_;
  230. extern struct _IO_FILE_plus _IO_2_1_stdout_;
  231. extern struct _IO_FILE_plus _IO_2_1_stderr_;
  232. # "/usr/include/libio.h"
  233. typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes);
  234.  
  235. typedef __ssize_t __io_write_fn (void *__cookie, __const char *__buf,
  236. size_t __n);
  237.  
  238. typedef int __io_seek_fn (void *__cookie, __off64_t *__pos, int __w);
  239.  
  240. typedef int __io_close_fn (void *__cookie);
  241. # "/usr/include/libio.h"
  242. extern int __underflow (_IO_FILE *);
  243. extern int __uflow (_IO_FILE *);
  244. extern int __overflow (_IO_FILE *, int);
  245. # "/usr/include/libio.h"
  246. extern int _IO_getc (_IO_FILE *__fp);
  247. extern int _IO_putc (int __c, _IO_FILE *__fp);
  248. extern int _IO_feof (_IO_FILE *__fp) __attribute__ ((__nothrow__ , __leaf__));
  249. extern int _IO_ferror (_IO_FILE *__fp) __attribute__ ((__nothrow__ , __leaf__));
  250.  
  251. extern int _IO_peekc_locked (_IO_FILE *__fp);
  252.  
  253. extern void _IO_flockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__));
  254. extern void _IO_funlockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__));
  255. extern int _IO_ftrylockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__));
  256. # "/usr/include/libio.h"
  257. extern int _IO_vfscanf (_IO_FILE * __restrict, const char * __restrict,
  258. __gnuc_va_list, int *__restrict);
  259. extern int _IO_vfprintf (_IO_FILE *__restrict, const char *__restrict,
  260. __gnuc_va_list);
  261. extern __ssize_t _IO_padn (_IO_FILE *, int, __ssize_t);
  262. extern size_t _IO_sgetn (_IO_FILE *, void *, size_t);
  263.  
  264. extern __off64_t _IO_seekoff (_IO_FILE *, __off64_t, int, int);
  265. extern __off64_t _IO_seekpos (_IO_FILE *, __off64_t, int);
  266.  
  267. extern void _IO_free_backup_area (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__));
  268. # "/usr/include/stdio.h"
  269.  
  270. typedef __gnuc_va_list va_list;
  271. # "/usr/include/stdio.h"
  272. typedef __off_t off_t;
  273. # "/usr/include/stdio.h"
  274. typedef __ssize_t ssize_t;
  275.  
  276. typedef _G_fpos_t fpos_t;
  277.  
  278. # "/usr/include/stdio.h"
  279. # "/usr/include/x86_64-linux-gnu/bits/stdio_lim.h"
  280. # "/usr/include/stdio.h"
  281.  
  282. extern struct _IO_FILE *stdin;
  283. extern struct _IO_FILE *stdout;
  284. extern struct _IO_FILE *stderr;
  285.  
  286. extern int remove (__const char *__filename) __attribute__ ((__nothrow__ , __leaf__));
  287.  
  288. extern int rename (__const char *__old, __const char *__new) __attribute__ ((__nothrow__ , __leaf__));
  289.  
  290. extern int renameat (int __oldfd, __const char *__old, int __newfd,
  291. __const char *__new) __attribute__ ((__nothrow__ , __leaf__));
  292.  
  293. extern FILE *tmpfile (void) ;
  294. # "/usr/include/stdio.h"
  295. extern char *tmpnam (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ;
  296.  
  297. extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ;
  298. # "/usr/include/stdio.h"
  299. extern char *tempnam (__const char *__dir, __const char *__pfx)
  300. __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ;
  301.  
  302. extern int fclose (FILE *__stream);
  303.  
  304. extern int fflush (FILE *__stream);
  305.  
  306. # "/usr/include/stdio.h"
  307. extern int fflush_unlocked (FILE *__stream);
  308. # "/usr/include/stdio.h"
  309.  
  310. extern FILE *fopen (__const char *__restrict __filename,
  311. __const char *__restrict __modes) ;
  312.  
  313. extern FILE *freopen (__const char *__restrict __filename,
  314. __const char *__restrict __modes,
  315. FILE *__restrict __stream) ;
  316. # "/usr/include/stdio.h"
  317.  
  318. # "/usr/include/stdio.h"
  319. extern FILE *fdopen (int __fd, __const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ;
  320. # "/usr/include/stdio.h"
  321. extern FILE *fmemopen (void *__s, size_t __len, __const char *__modes)
  322. __attribute__ ((__nothrow__ , __leaf__)) ;
  323.  
  324. extern FILE *open_memstream (char **__bufloc, size_t *__sizeloc) __attribute__ ((__nothrow__ , __leaf__)) ;
  325.  
  326. extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__ , __leaf__));
  327.  
  328. extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf,
  329. int __modes, size_t __n) __attribute__ ((__nothrow__ , __leaf__));
  330.  
  331. extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf,
  332. size_t __size) __attribute__ ((__nothrow__ , __leaf__));
  333.  
  334. extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
  335.  
  336. extern int fprintf (FILE *__restrict __stream,
  337. __const char *__restrict __format, ...);
  338.  
  339. extern int printf (__const char *__restrict __format, ...);
  340.  
  341. extern int sprintf (char *__restrict __s,
  342. __const char *__restrict __format, ...) __attribute__ ((__nothrow__));
  343.  
  344. extern int vfprintf (FILE *__restrict __s, __const char *__restrict __format,
  345. __gnuc_va_list __arg);
  346.  
  347. extern int vprintf (__const char *__restrict __format, __gnuc_va_list __arg);
  348.  
  349. extern int vsprintf (char *__restrict __s, __const char *__restrict __format,
  350. __gnuc_va_list __arg) __attribute__ ((__nothrow__));
  351.  
  352. extern int snprintf (char *__restrict __s, size_t __maxlen,
  353. __const char *__restrict __format, ...)
  354. __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, , )));
  355.  
  356. extern int vsnprintf (char *__restrict __s, size_t __maxlen,
  357. __const char *__restrict __format, __gnuc_va_list __arg)
  358. __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, , )));
  359.  
  360. # "/usr/include/stdio.h"
  361. extern int vdprintf (int __fd, __const char *__restrict __fmt,
  362. __gnuc_va_list __arg)
  363. __attribute__ ((__format__ (__printf__, , )));
  364. extern int dprintf (int __fd, __const char *__restrict __fmt, ...)
  365. __attribute__ ((__format__ (__printf__, , )));
  366.  
  367. extern int fscanf (FILE *__restrict __stream,
  368. __const char *__restrict __format, ...) ;
  369.  
  370. extern int scanf (__const char *__restrict __format, ...) ;
  371.  
  372. extern int sscanf (__const char *__restrict __s,
  373. __const char *__restrict __format, ...) __attribute__ ((__nothrow__ , __leaf__));
  374. # "/usr/include/stdio.h"
  375. extern int fscanf (FILE *__restrict __stream, __const char *__restrict __format, ...) __asm__ ("" "__isoc99_fscanf")
  376.  
  377. ;
  378. extern int scanf (__const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf")
  379. ;
  380. extern int sscanf (__const char *__restrict __s, __const char *__restrict __format, ...) __asm__ ("" "__isoc99_sscanf") __attribute__ ((__nothrow__ , __leaf__))
  381.  
  382. ;
  383. # "/usr/include/stdio.h"
  384.  
  385. extern int vfscanf (FILE *__restrict __s, __const char *__restrict __format,
  386. __gnuc_va_list __arg)
  387. __attribute__ ((__format__ (__scanf__, , ))) ;
  388.  
  389. extern int vscanf (__const char *__restrict __format, __gnuc_va_list __arg)
  390. __attribute__ ((__format__ (__scanf__, , ))) ;
  391.  
  392. extern int vsscanf (__const char *__restrict __s,
  393. __const char *__restrict __format, __gnuc_va_list __arg)
  394. __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__format__ (__scanf__, , )));
  395. # "/usr/include/stdio.h"
  396. extern int vfscanf (FILE *__restrict __s, __const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vfscanf")
  397.  
  398. __attribute__ ((__format__ (__scanf__, , ))) ;
  399. extern int vscanf (__const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vscanf")
  400.  
  401. __attribute__ ((__format__ (__scanf__, , ))) ;
  402. extern int vsscanf (__const char *__restrict __s, __const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vsscanf") __attribute__ ((__nothrow__ , __leaf__))
  403.  
  404. __attribute__ ((__format__ (__scanf__, , )));
  405. # "/usr/include/stdio.h"
  406.  
  407. extern int fgetc (FILE *__stream);
  408. extern int getc (FILE *__stream);
  409.  
  410. extern int getchar (void);
  411.  
  412. # "/usr/include/stdio.h"
  413. extern int getc_unlocked (FILE *__stream);
  414. extern int getchar_unlocked (void);
  415. # "/usr/include/stdio.h"
  416. extern int fgetc_unlocked (FILE *__stream);
  417.  
  418. extern int fputc (int __c, FILE *__stream);
  419. extern int putc (int __c, FILE *__stream);
  420.  
  421. extern int putchar (int __c);
  422.  
  423. # "/usr/include/stdio.h"
  424. extern int fputc_unlocked (int __c, FILE *__stream);
  425.  
  426. extern int putc_unlocked (int __c, FILE *__stream);
  427. extern int putchar_unlocked (int __c);
  428.  
  429. extern int getw (FILE *__stream);
  430.  
  431. extern int putw (int __w, FILE *__stream);
  432.  
  433. extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
  434. ;
  435.  
  436. extern char *gets (char *__s) ;
  437.  
  438. # "/usr/include/stdio.h"
  439. extern __ssize_t __getdelim (char **__restrict __lineptr,
  440. size_t *__restrict __n, int __delimiter,
  441. FILE *__restrict __stream) ;
  442. extern __ssize_t getdelim (char **__restrict __lineptr,
  443. size_t *__restrict __n, int __delimiter,
  444. FILE *__restrict __stream) ;
  445.  
  446. extern __ssize_t getline (char **__restrict __lineptr,
  447. size_t *__restrict __n,
  448. FILE *__restrict __stream) ;
  449.  
  450. extern int fputs (__const char *__restrict __s, FILE *__restrict __stream);
  451.  
  452. extern int puts (__const char *__s);
  453.  
  454. extern int ungetc (int __c, FILE *__stream);
  455.  
  456. extern size_t fread (void *__restrict __ptr, size_t __size,
  457. size_t __n, FILE *__restrict __stream) ;
  458.  
  459. extern size_t fwrite (__const void *__restrict __ptr, size_t __size,
  460. size_t __n, FILE *__restrict __s);
  461.  
  462. # "/usr/include/stdio.h"
  463. extern size_t fread_unlocked (void *__restrict __ptr, size_t __size,
  464. size_t __n, FILE *__restrict __stream) ;
  465. extern size_t fwrite_unlocked (__const void *__restrict __ptr, size_t __size,
  466. size_t __n, FILE *__restrict __stream);
  467.  
  468. extern int fseek (FILE *__stream, long int __off, int __whence);
  469.  
  470. extern long int ftell (FILE *__stream) ;
  471.  
  472. extern void rewind (FILE *__stream);
  473.  
  474. # "/usr/include/stdio.h"
  475. extern int fseeko (FILE *__stream, __off_t __off, int __whence);
  476.  
  477. extern __off_t ftello (FILE *__stream) ;
  478. # "/usr/include/stdio.h"
  479.  
  480. extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);
  481.  
  482. extern int fsetpos (FILE *__stream, __const fpos_t *__pos);
  483. # "/usr/include/stdio.h"
  484.  
  485. # "/usr/include/stdio.h"
  486.  
  487. extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
  488.  
  489. extern int feof (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
  490.  
  491. extern int ferror (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
  492.  
  493. extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
  494. extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
  495. extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
  496.  
  497. extern void perror (__const char *__s);
  498.  
  499. # "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h"
  500. # "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h"
  501. extern int sys_nerr;
  502. extern __const char *__const sys_errlist[];
  503. # "/usr/include/stdio.h"
  504.  
  505. extern int fileno (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
  506.  
  507. extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
  508. # "/usr/include/stdio.h"
  509. extern FILE *popen (__const char *__command, __const char *__modes) ;
  510.  
  511. extern int pclose (FILE *__stream);
  512.  
  513. extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__));
  514. # "/usr/include/stdio.h"
  515. extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
  516.  
  517. extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
  518.  
  519. extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
  520. # "/usr/include/stdio.h"
  521.  
  522. # "hello.c"
  523.  
  524. int main()
  525. {
  526. printf("hello world!\n");
  527. }

hello.i

  编译阶段。编译器(ccl)将文本文件hello.i翻译成文本文件hello.s,它包含一个汇编语言程序。汇编语言程序中的每条语句都以一种标准的文本格式确切地描述了一条低级机器语言指令。汇编语言是非常有用的,因为他为不同高级语言的不同编译器提供了通用的输出语言。例如,C编译器和Fortran编译器产生的输出文件用的都是一样的汇编语言。

  编译过程就是把预处理完的文件进行一系列的词法分析,语法分析,语义分析及优化后生成相应的汇编代码。

  gcc –S hello.i –o hello.s

  1. .file "hello.c"
  2. .section .rodata
  3. .LC0:
  4. .string "hello world!"
  5. .text
  6. .globl main
  7. .type main, @function
  8. main:
  9. .LFB0:
  10. .cfi_startproc
  11. pushq %rbp
  12. .cfi_def_cfa_offset
  13. .cfi_offset , -
  14. movq %rsp, %rbp
  15. .cfi_def_cfa_register
  16. movl $.LC0, %edi
  17. call puts
  18. popq %rbp
  19. .cfi_def_cfa ,
  20. ret
  21. .cfi_endproc
  22. .LFE0:
  23. .size main, .-main
  24. .ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
  25. .section .note.GNU-stack,"",@progbits

hello.s

  汇编阶段。汇编器(as)将hello.s翻译成机器语言指令,并把这些指令打包成一种叫做可充定位目标程序(relocatable object program)的格式,并将结果保存在目标文件hello.o中,hello.o文件是一个二进制文件,它的字节编码是机器语言指令而不是字符。如果我们在文本编辑器中打开hello.o文件,看到的将是一堆乱码。

  汇编器是将汇编代码转变成机器可以执行的命令,每一个汇编语句几乎都对应一条机器指令。汇编相对于编译过程比较简单,根据汇编指令和机器指令的对照表一一翻译即可。

  $ gcc –c hello.c –o hello.o

  或者

  $ as hello.s –o hello.co

  由于hello.o的内容为机器码,不能以普通文本形式的查看

  连接阶段。请注意,hello程序调用了printf函数,他是每个C编译器都会提供的标准C库中的一个函数。printf函数存在于一个名为printf.o程序中。链接器(ld)就负责处理这种合并。结果就得到hello文件,他是一个可执行目标文件(或者简称可执行文件),可以被加载到内存,由系统执行。

  通过调用链接器ld来链接程序运行需要的一大堆目标文件,以及所依赖的其它库文件,最后生成可执行文件。

  

  参考:

  书籍:《深入理解计算机系统》

  博客:http://blog.csdn.net/koudaidai/article/details/8092647

C程序编译过程的更多相关文章

  1. 李洪强漫谈iOS开发[C语言-004]-开发概述程序设计语言程序编译过程

    汇编语言 指令用特定的名字来标记,这就是汇编语言 人比较容易看懂汇编语言 汇编直接和程序一一对应的 有汇编器把程序翻译成机器码 把高级语言编译成计算机识别的语言 程序编译过程 命令行 UNIX 系统中 ...

  2. C程序编译过程浅析

    前几天看了<程序员的自我修养——链接.装载与库>中的第二章“编译和链接”,主要根据其中的内容简单总结一下C程序编译的过程吧. 我现在一般都是用gcc,所以自然以GCC编译hellworld ...

  3. C程序编译过程浅析(转)

    前几天看了<程序员的自我修养——链接.装载与库>中的第二章“编译和链接”,主要根据其中的内容简单总结一下C程序编译的过程吧. 我现在一般都是用gcc,所以自然以GCC编译hellworld ...

  4. C程序编译过程浅析【转】

    转自:http://blog.csdn.net/koudaidai/article/details/8092647 前几天看了<程序员的自我修养——链接.装载与库>中的第二章“编译和链接” ...

  5. Linux 程序编译过程的来龙去脉

    大家肯定都知道计算机程序设计语言通常分为机器语言.汇编语言和高级语言三类.高级语言需要通过翻译成机器语言才能执行,而翻译的方式分为两种,一种是编译型,另一种是解释型,因此我们基本上将高级语言分为两大类 ...

  6. linux程序编译过程

    大家肯定都知道计算机程序设计语言通常分为机器语言.汇编语言和高级语言三类.高级语言需要通过翻译成机器语言才能执行,而翻译的方式分为两种,一种是编译型,另一种是解释型,因此我们基本上将高级语言分为两大类 ...

  7. 后台程序编译过程报错PCC-F-02104, Unable to connect to Oracle

    偶然重新编译了一下后台程序,发现编译过程报错无法连接数据库.但通过sqlplus登录数据库是正常的.后台程序改动中也做了详细的分析,没有改动相关数据库的参数和配置. 最后通过浏览器查看了很多相关问题的 ...

  8. 【转】android程序编译过程

    现在很多人想对Android工程的编译和打包进行自动化,比如建立每日构建系统.自动生成发布文件等等.这些都需要我们对Android工程的编译和打包有一个深入的理解,至少要知道它的每一步都做了什么,需要 ...

  9. unix 网路编程(卷一)第一个程序编译过程

    unix卷一去年暑假买的到现在才开始看无比惭愧,而且惭愧第一个程序就断断续续弄了几天,要好好写程序了,马上要找工作了,下面介绍下把本书第一个程序跑起来的过程: 搜各种博客 我用系统的是ubuntu 1 ...

随机推荐

  1. NuGet学习笔记(1) 初识NuGet及快速安装使用

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  2. 函数调用关于从Ring3转到Ring0 ESP堆栈变化

    在ring0堆栈获取ring3堆栈方式 第一种方式 [esp+4] == [esp+参数个数*4+4] 如果这里不相等就需要用第二种方式 [[esp+参数个数*4+8]] 这里面的值就是Ring3的堆 ...

  3. JSON详解以及可以把javabean转换成json串的json-lib应用

    JSON 1. json是什么 它是js提供的一种数据交换格式! 2. json的语法 {}:是对象! 属性名必须使用双引号括起来!单引不行!!! 属性值:null,数值,字符串,数组:使用[]括起来 ...

  4. 【jQuery 区别】attr()和prop()的区别

    1>>> 今天实现一个 点击更新按钮 ,可以勾选上本行的的checkbox的功能: 使用代码: /** * updateproduct.htmls 更新 产品信息 */ $(docu ...

  5. Loadrunner中参数化实战(9)-Unique+Once

    参数化数据30条: 脚本如下,演示登录,投资,退出操作是,打印手机号: 首先验证Vugen中迭代: Unique+Once 设置迭代4次Action 结果如下:

  6. java jdbc sqlhelper

    package com.shop.util; import java.sql.*; //SqlHelper类 //定义了数据库连接函数,关闭查询结果集,关闭Statement对象,关闭数据库连接 // ...

  7. SPOJ SUBST1 后缀数组

    题目链接:http://www.spoj.com/problems/SUBST1/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字符串 ...

  8. 用Python做自然语言处理必知的八个工具【转载】

    Python以其清晰简洁的语法.易用和可扩展性以及丰富庞大的库深受广大开发者喜爱.其内置的非常强大的机器学习代码库和数学库,使Python理所当然成为自然语言处理的开发利器. 那么使用Python进行 ...

  9. [工作中的设计模式]策略模式stategy

    一.模式解析 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. 策略模式的关键点为: 1.多种算法存在 2.算法继承同样的接口 ...

  10. js-错误处理与调试,JSON

    错误处理与调试: 1.try-catch try{ window.someNoneXistentFunction(); }catch(error){ alert(error.message) } 2. ...