GCC(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器。它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分。GCC原本作为GNU操作系统的官方编译器,现已被大多数类Unix操作系统(如Linux、BSD、Mac OS X等)采纳为标准的编译器,GCC同样适用于微软的Windows。 GCC是自由软件过程发展中的著名例子,由自由软件基金会以GPL协议发布。它处理能够高效的编译C语言以外,还可以编译其他语言。并且,现在的GCC已经不光包括编译器本身,还包含了编译过程中的工具链。

1 GCC编译流程

在学习使用GCC编译程序之前,首先要知道编译C程序的基本流程,一般情况下分为下面四步:

(1) 对C语言进行预处理,生成*.i文件。

(2) 将上一步生成的*.i文件编译生成汇编语言文件,后缀名为*.s

(3) 将汇编语言文件*.s经过汇编,生成目标文件,后缀名为*.o

(4) 将各个模块的*.o文件链接起来,生成最终的可执行文件

2 GCC常用选项

GCC的编译选项非常多,现在有上千个,但是我们常用的并不多,下面我们只介绍其中非常实用的几个。

在这之前,我们先编写下面的几个源文件,以备测试只用。

  1. //main.c
  2. #include <stdio.h>
  3.  
  4. extern int add(int a, int b);
  5. extern int mul(int a, int b);
  6.  
  7. int main(void)
  8. {
  9. int a = , b = ;
  10. int result;
  11.  
  12. result = add(a, mul(a, b));
  13. printf("result = %d\n", result);
  14. return ;
  15. }

main.c

  1. //test1.c
  2. int add(int a, int b)
  3. {
  4. return a+b;
  5. }

test1.c

  1. //test2.c
  2. int mul(int a, int b)
  3. {
  4. return a*b;
  5. }

test2.c

2.1 -c选项

  该选项(小写c)表示编译、汇编指定的源文件,但是不进行链接。该选项的使用方法如下:

  1. gcc -c source.c

也就是在-c选项后面紧跟要编译、汇编的C源文件,最终生成与源文件名称相同,但是后缀为*.o结尾的目标文件。

  1. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  2. main.c test1.c test2.c
  3. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc -c test1.c
  4. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  5. main.c test1.c test1.o test2.c

可以看到,使用-c选项编译之后生成了对应的*.o目标文件。当然,你也可以一次性指定多个C源文件,使用-c选项后,会针对每一个C源文件生成一个相应的*.o目标文件。

  1. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc -c test2.c main.c
  2. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  3. main.c main.o test1.c test1.o test2.c test2.o

2.2 -S选项

  该选项(大写S)将C语言源文件编译生成汇编语言文件,但是并不汇编该程序。注意:汇编过程的作用是将汇编语言文件编译成目标文件*.o,而-S选项的作用是得到汇编语言文件*.s。该选项的使用方法为:

  1. gcc -S source.c

使用该选项,最终生成与源文件名称相同,但是后缀为*.s结尾的汇编语言文件。

  1. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc -S test1.c
  2. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  3. main.c test1.c test1.s test2.c

当然,输入的源文件也不止一个,你可以编译当前目录下的所有C语言源文件:

  1. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc -S *.c
  2. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  3. main.c main.s test1.c test1.s test2.c test2.s

我们也可以查看生成的汇编语言代码:

  1. .file "test1.c"
  2. .text
  3. .globl add
  4. .type add, @function
  5. add:
  6. .LFB0:
  7. .cfi_startproc
  8. pushl %ebp
  9. .cfi_def_cfa_offset
  10. .cfi_offset , -
  11. movl %esp, %ebp
  12. .cfi_def_cfa_register
  13. movl (%ebp), %eax
  14. movl (%ebp), %edx
  15. addl %edx, %eax
  16. popl %ebp
  17. .cfi_restore
  18. .cfi_def_cfa ,
  19. ret
  20. .cfi_endproc
  21. .LFE0:
  22. .size add, .-add
  23. .ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
  24. .section .note.GNU-stack,"",@progbits

test1.s

2.3 -E选项

  该选项对C语言源文件进行预处理,但是并不编译该程序。对于一般的预处理问题(比如,宏的展开问题、文件的包含问题等),可以使用这个选项进行查看。另外,如果直接使用此选项,程序会将预处理结果直接输出到终端,不便于查看,因此,一般可以使用重定向将程序输出结果保存到一个文本文件中,格式如下:

  1. gcc -E source.c > output

如果我们对main.c执行预编译,得到的output文件内容如下:

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

output

当然,我们也可以使用接下来要介绍的-o命令来指定输出文件名称,格式如下:

  1. gcc -E source.c -o source.i

使用该命令对main.c进行预处理,得到的main.i文件内容与之前重定向输出得到的output文件的内容完全相同。

2.4 -o选项

  该选项(小写O)用于将输入文件编译后输出指定名称的文件。该选项有两种使用方法,第一种是紧跟gcc命令之后:

  1. gcc -o app source1.c source2.c source3.c

那么,编译我们的测试程序可以使用下面的方式:

  1. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc -o app *.c
  2. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  3. app main.c test1.c test2.c
  4. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ./app
  5. result =

还有另外一种方式,即-o选项放置在最后:

  1. gcc source1.c source2.c source3.c -o app

这种方式的逻辑性更强,语义可以理解为编译C语言源文件得到最终的可执行程序app,使用这种方式编译我们的测试程序过程如下:

  1. xiaomanon@xiaomanon-machine:~/Documents/c_code$ rm app
  2. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc test1.c test2.c main.c -o app
  3. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  4. app main.c test1.c test2.c
  5. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ./app
  6. result =

此外,此选项很多时候用作链接多个目标文件的时候,我们可能需要先对不同的源文件进行相应的操作来得到目标文件*.o,然后在最后将这些目标文件链接成一个可执行文件。

  1. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc -c *.c
  2. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  3. main.c main.o test1.c test1.o test2.c test2.o
  4. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc main.o test1.o test2.o -o app
  5. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ls
  6. app main.c main.o test1.c test1.o test2.c test2.o
  7. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ./app
  8. result =

2.5 -I选项

  该选项用于指定包含的头文件的目录,这一点对于大型的代码组织来说是很有用的。

2.6 -g选项

  该选项用来生成可以被gdb调试器使用的调试信息。只有使用了该选项后生成的可执行文件才带有程序中引用的符号表,这是gdb调试程序才能对可执行程序进行调试。

  1. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc *.c -o app
  2. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ll app
  3. -rwxrwxr-x xiaomanon xiaomanon 12 : app*
  4. xiaomanon@xiaomanon-machine:~/Documents/c_code$ gcc -g *.c -o app
  5. xiaomanon@xiaomanon-machine:~/Documents/c_code$ ll app
  6. -rwxrwxr-x xiaomanon xiaomanon 8825 12 : app*

以上命令分别生成了不带调试信息的可执行文件和带有调试信息的可执行文件,并对比了两者的文件大小,可以看出使用的-g选项生成的可执行文件明显要比没有使用-g选项的可执行文件大。

GCC编译器入门的更多相关文章

  1. C语言的本质(33)——GCC编译器入门

    GCC(GNU CompilerCollection,GNU编译器套装),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分.GCC原本作为GNU操作系 ...

  2. GCC编译器入门[转载]

    GCC编译器(GNU C Compiler)是GNU组织的一款开源 编译器,它是Linux环境下的默认C语言编译器.它处理能够高效的编译C语言以外,还可以编译其他语言.并且,现在的GCC已经不光包括编 ...

  3. GCC编译器基础入门

    导语 GCC(GNU Compiler Collection,GNU 编译器套件) 是由 GNU 开发的编程语言编译器,支持C.C++.Objective-C.Fortran.Java.Ada和Go语 ...

  4. Linux环境下的GCC编译器与GDB调试工具介绍

    假如现在我们有如下代码需要编译运行和调试.文件名为:test.c #include <stdio.h> int main() { int day, month, year, sum, le ...

  5. 在CentOS 7.2下升级gcc编译器的版本

    默认情况下,CentOS 7.2预装的gcc版本是4.8.x,通过执行命令 gcc -v 可以看到,一般情况下这个版本的编译器已经满足需要了,但是某些特殊的时候为了支持C++更高的特性,需要对gcc编 ...

  6. C语言的编译过程、安装gcc编译器以及设置环境变量

    以我对C语言编译过程的了解,我用了一点时间画了一个图,提供给大家参考一下,希望有些能对您的问题提上帮助. 前几天刚初步学习了C语言的编译过程,感触挺深的.在C语言中头文件其实起了一个很大的作用. 1. ...

  7. 利用GCC编译器生成动态链接库和静态链接库

    转载请标明:http://www.cnblogs.com/winifred-tang94/ 1.编译过程 gcc –fPIC –c xxx.c 其中-fPIC是通知gcc编译器产生位置独立的目标代码. ...

  8. GCC编译器编译链接

    在gcc编译器环境下,常见的文件扩展名的含义如下: .c:C源程序,经过预编译后的源程序也为.c文件,它可以通过-E参数输出. .h:头文件 .s:经过编译得到的汇编程序代码,它可以通过-S参数输出. ...

  9. Linux安装gcc编译器详解

    本人使用的是CentOS 6.5 64位系统,由于在安装系统的时候并没有勾选安装gcc编译器,因此需要自行安装gcc编译器. 使用yum安装gcc 对于配备了yum的Linux发行版而言,安装gcc编 ...

随机推荐

  1. mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication的解决方法

    直接命令行操作没有问题,但是PHP连接就会报上面的错误. SET old_passwords =0; USE mysql; UPDATE user SET password =PASSWORD('yo ...

  2. [Effective C++ --010]令赋值操作符返回一个reference to *this

    差不多最经典的就是这个了: x = y = z = ; 解读为: x = (y = ( z = )); 如果没有返回值,上述代码就不能通过编译. 其实看到标题就差不多明白这一条了,但是为什么连续赋值时 ...

  3. 信号之system函数

    在http://www.cnblogs.com/nufangrensheng/p/3512291.html中已经有了一个system函数的实现,但是该版本并不执行任何信号处理.POSIX.1要求sys ...

  4. Tomcat 配置 Probe 监控

    转至:http://9771104.blog.163.com/blog/static/19446622009811112836524/ 手上接触Tomcat的项目越来越多,虽说tomcat的manag ...

  5. Supervised Learning-Regression

    假设我们有一张房子属性及其价格之间的关系表(如下图所示) ,根据这些数据如何估计其他房子的价格?我们的第一个反应肯定是参考属性相似的房子的价格.在属性较少时这个方法还行得通,属性太复杂时就不那么简单了 ...

  6. Regular Expressions --正则表达式官方教程

    http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...

  7. ajax的一部分知识

    jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...

  8. update目标在查询返回结果集中的解决方案

     示例: students为学生信息表 Score为成绩表 两个表通过学生号关联 要求:将总成绩小于100的学生名称改为‘天才’ sql如下: error提示:update目标不能在查询返回结果集中 ...

  9. java算法小知识练习

    偶尔翻开了以前的练习题,不自觉又想随手敲一遍,虽然有些思想依然是那么老套,但毕竟也算是对知识的巩固 了. 一.题目:有1.2.3.4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 具体 ...

  10. arguments 函数内部属性

    1.arguments 是在function方法里面的,是实参数组,用法是挺多的,下面来记录一下 2.利用arguments实现方法的重载 //01.使用argument模拟方法重载 function ...