Linux编程-GCC编译命令的使用,gcc

小灰灰 2023-10-16 18:50 135阅读 0赞

1.内容简介:

GCC,即GNU编译器套件(GNU Compiler Collection),是最为广泛使用的编译器之一,几乎包含了我们熟知的大部分编程语言,包括CC、C++、 Objective-C、 Fortran、Java、Ada和Go语言前端,也包括了这些语言的库(如libstdc++,libgcj等)。正如某些流行的技术一样,一开始,并没有想到会发展的如此庞大和广泛,GCC的初衷是为GNU操作系统专门编写的一款编译器。

2. GCC快速使用:gcc filename.c直接编译成可执行文件

先来快速看看GCC怎么使用,看下面这个例子:

  1. #include <stdio.h>
  2. int main() {
  3. int a = 5;
  4. int b =3;
  5. int res = sum(a,b);
  6. printf("sum=%d",res);
  7. }
  8. int sum(int a,int b) {
  9. int c = a+b;
  10. return c;
  11. }

用GCC编译结果:

gcc my_sum.c

my_sum.c:5:12: error: implicit declaration of function ‘sum’ is invalid in C99 [-Werror,-Wimplicit-function-declaration]

int res = sum(a,b);

^

1 error generated.

很显然,在编译时,找不到my_sum,可是明明定义了呀。还记得,谭浩强的C语言编程书中说的,先声明,再引用么?把my_sum放在main函数前面,试一下:

  1. #include <stdio.h>
  2. int sum(int a,int b) {
  3. int c = a+b;
  4. return c;
  5. }
  6. int main() {
  7. int a = 5;
  8. int b =3;
  9. int res = sum(a,b);
  10. printf("sum=%d",res);
  11. }

编译成功,运行:

gcc my_sum.c

aibook@aibookdeMacBook-Pro c_stu % ./a.out

sum=8

3. GCC命令常用选项和说明:

将C,C++源代码编译为可执行代码的过程,可以拆分成4个步骤:预处理(Preprocessing)、编译(Compilation)、汇编(Assembly)和链接(Linking)。
上面的gcc filename.c就直讲完成上面4个步骤,编译生成a.out,这个是默认的目标文件名称。

GCC还有许多选项,常用的如下:
-E: 对源文件进行预处理。
-S: 汇编,生成汇编文件。
-c: 生成目标文件。但不进行链接,生成的文件还不能直接运行。当-c后跟多个源文件,会为每个源文件生成一个.o文件,但此时是不能使用-o的。
-o: 指定要生成的结果文件,o是output,而非object。即生成可执行文件。

这个过程,分开可以这样编译:

  1. aibook@aibookdeMacBook-Pro chaper1 % gcc -E my_sum.c -o my_sum.i
  2. aibook@aibookdeMacBook-Pro chaper1 % ls
  3. a.out my_sum.c my_sum.i
  4. aibook@aibookdeMacBook-Pro chaper1 % gcc -S my_sum.i
  5. aibook@aibookdeMacBook-Pro chaper1 % ls
  6. a.out my_sum.c my_sum.i my_sum.s
  7. aibook@aibookdeMacBook-Pro chaper1 % gcc -c my_sum.s
  8. aibook@aibookdeMacBook-Pro chaper1 % ls
  9. a.out my_sum.c my_sum.i my_sum.o my_sum.s
  10. aibook@aibookdeMacBook-Pro chaper1 % gcc -o my_sum my_sum.o
  11. aibook@aibookdeMacBook-Pro chaper1 % ls
  12. a.out my_sum my_sum.c my_sum.i my_sum.o my_sum.s
  13. aibook@aibookdeMacBook-Pro chaper1 % my_sum
  14. sum=8 aibook@aibookdeMacBook-Pro chaper1 %

当然,我们可以一步到位,知道结果文件:

  1. aibook@aibookdeMacBook-Pro chaper1 % gcc -o my_sum2 my_sum.c
  2. aibook@aibookdeMacBook-Pro chaper1 % ls
  3. a.out my_sum my_sum.c my_sum.i my_sum.o my_sum.s my_sum2
  4. aibook@aibookdeMacBook-Pro chaper1 %

运行my_sum2,结果如下:

aibook@aibookdeMacBook-Pro chaper1 % my_sum2

sum=8

4. 这些中间文件长啥样:

上面编译生成的有.i,.s.o等文件,我们主要来看.i和.s文件的代码:

my_sum.i:

  1. # 1 "my_sum.c"
  2. # 1 "<built-in>" 1
  3. # 1 "<built-in>" 3
  4. # 367 "<built-in>" 3
  5. # 1 "<command line>" 1
  6. # 1 "<built-in>" 2
  7. # 1 "my_sum.c" 2
  8. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 1 3 4
  9. # 64 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  10. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 1 3 4
  11. # 68 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 3 4
  12. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 1 3 4
  13. # 649 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 3 4
  14. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_symbol_aliasing.h" 1 3 4
  15. # 650 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 2 3 4
  16. # 715 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 3 4
  17. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_posix_availability.h" 1 3 4
  18. # 716 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 2 3 4
  19. # 69 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4
  20. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h" 1 3 4
  21. # 135 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h" 3 4
  22. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/AvailabilityVersions.h" 1 3 4
  23. # 136 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h" 2 3 4
  24. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/AvailabilityInternal.h" 1 3 4
  25. # 137 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h" 2 3 4
  26. # 70 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4
  27. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_types.h" 1 3 4
  28. # 27 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_types.h" 3 4
  29. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 1 3 4
  30. # 33 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 3 4
  31. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/_types.h" 1 3 4
  32. # 32 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/_types.h" 3 4
  33. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/_types.h" 1 3 4
  34. # 37 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/_types.h" 3 4
  35. typedef signed char __int8_t;
  36. typedef unsigned char __uint8_t;
  37. typedef short __int16_t;
  38. typedef unsigned short __uint16_t;
  39. typedef int __int32_t;
  40. typedef unsigned int __uint32_t;
  41. typedef long long __int64_t;
  42. typedef unsigned long long __uint64_t;
  43. typedef long __darwin_intptr_t;
  44. typedef unsigned int __darwin_natural_t;
  45. # 70 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/_types.h" 3 4
  46. typedef int __darwin_ct_rune_t;
  47. typedef union {
  48. char __mbstate8[128];
  49. long long _mbstateL;
  50. } __mbstate_t;
  51. typedef __mbstate_t __darwin_mbstate_t;
  52. typedef long int __darwin_ptrdiff_t;
  53. typedef long unsigned int __darwin_size_t;
  54. typedef __builtin_va_list __darwin_va_list;
  55. typedef int __darwin_wchar_t;
  56. typedef __darwin_wchar_t __darwin_rune_t;
  57. typedef int __darwin_wint_t;
  58. typedef unsigned long __darwin_clock_t;
  59. typedef __uint32_t __darwin_socklen_t;
  60. typedef long __darwin_ssize_t;
  61. typedef long __darwin_time_t;
  62. # 33 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/_types.h" 2 3 4
  63. # 34 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 2 3 4
  64. # 55 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 3 4
  65. typedef __int64_t __darwin_blkcnt_t;
  66. typedef __int32_t __darwin_blksize_t;
  67. typedef __int32_t __darwin_dev_t;
  68. typedef unsigned int __darwin_fsblkcnt_t;
  69. typedef unsigned int __darwin_fsfilcnt_t;
  70. typedef __uint32_t __darwin_gid_t;
  71. typedef __uint32_t __darwin_id_t;
  72. typedef __uint64_t __darwin_ino64_t;
  73. typedef __darwin_ino64_t __darwin_ino_t;
  74. typedef __darwin_natural_t __darwin_mach_port_name_t;
  75. typedef __darwin_mach_port_name_t __darwin_mach_port_t;
  76. typedef __uint16_t __darwin_mode_t;
  77. typedef __int64_t __darwin_off_t;
  78. typedef __int32_t __darwin_pid_t;
  79. typedef __uint32_t __darwin_sigset_t;
  80. typedef __int32_t __darwin_suseconds_t;
  81. typedef __uint32_t __darwin_uid_t;
  82. typedef __uint32_t __darwin_useconds_t;
  83. typedef unsigned char __darwin_uuid_t[16];
  84. typedef char __darwin_uuid_string_t[37];
  85. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_pthread/_pthread_types.h" 1 3 4
  86. # 57 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_pthread/_pthread_types.h" 3 4
  87. struct __darwin_pthread_handler_rec {
  88. void (*__routine)(void *);
  89. void *__arg;
  90. struct __darwin_pthread_handler_rec *__next;
  91. };
  92. struct _opaque_pthread_attr_t {
  93. long __sig;
  94. char __opaque[56];
  95. };
  96. struct _opaque_pthread_cond_t {
  97. long __sig;
  98. char __opaque[40];
  99. };
  100. struct _opaque_pthread_condattr_t {
  101. long __sig;
  102. char __opaque[8];
  103. };
  104. struct _opaque_pthread_mutex_t {
  105. long __sig;
  106. char __opaque[56];
  107. };
  108. struct _opaque_pthread_mutexattr_t {
  109. long __sig;
  110. char __opaque[8];
  111. };
  112. struct _opaque_pthread_once_t {
  113. long __sig;
  114. char __opaque[8];
  115. };
  116. struct _opaque_pthread_rwlock_t {
  117. long __sig;
  118. char __opaque[192];
  119. };
  120. struct _opaque_pthread_rwlockattr_t {
  121. long __sig;
  122. char __opaque[16];
  123. };
  124. struct _opaque_pthread_t {
  125. long __sig;
  126. struct __darwin_pthread_handler_rec *__cleanup_stack;
  127. char __opaque[8176];
  128. };
  129. typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;
  130. typedef struct _opaque_pthread_cond_t __darwin_pthread_cond_t;
  131. typedef struct _opaque_pthread_condattr_t __darwin_pthread_condattr_t;
  132. typedef unsigned long __darwin_pthread_key_t;
  133. typedef struct _opaque_pthread_mutex_t __darwin_pthread_mutex_t;
  134. typedef struct _opaque_pthread_mutexattr_t __darwin_pthread_mutexattr_t;
  135. typedef struct _opaque_pthread_once_t __darwin_pthread_once_t;
  136. typedef struct _opaque_pthread_rwlock_t __darwin_pthread_rwlock_t;
  137. typedef struct _opaque_pthread_rwlockattr_t __darwin_pthread_rwlockattr_t;
  138. typedef struct _opaque_pthread_t *__darwin_pthread_t;
  139. # 81 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 2 3 4
  140. # 28 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_types.h" 2 3 4
  141. # 40 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_types.h" 3 4
  142. typedef int __darwin_nl_item;
  143. typedef int __darwin_wctrans_t;
  144. typedef __uint32_t __darwin_wctype_t;
  145. # 72 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4
  146. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_va_list.h" 1 3 4
  147. # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_va_list.h" 3 4
  148. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/types.h" 1 3 4
  149. # 35 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/types.h" 3 4
  150. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 1 3 4
  151. # 76 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 3 4
  152. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int8_t.h" 1 3 4
  153. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int8_t.h" 3 4
  154. typedef signed char int8_t;
  155. # 77 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  156. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int16_t.h" 1 3 4
  157. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int16_t.h" 3 4
  158. typedef short int16_t;
  159. # 78 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  160. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int32_t.h" 1 3 4
  161. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int32_t.h" 3 4
  162. typedef int int32_t;
  163. # 79 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  164. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int64_t.h" 1 3 4
  165. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int64_t.h" 3 4
  166. typedef long long int64_t;
  167. # 80 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  168. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int8_t.h" 1 3 4
  169. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int8_t.h" 3 4
  170. typedef unsigned char u_int8_t;
  171. # 82 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  172. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int16_t.h" 1 3 4
  173. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int16_t.h" 3 4
  174. typedef unsigned short u_int16_t;
  175. # 83 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  176. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int32_t.h" 1 3 4
  177. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int32_t.h" 3 4
  178. typedef unsigned int u_int32_t;
  179. # 84 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  180. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int64_t.h" 1 3 4
  181. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int64_t.h" 3 4
  182. typedef unsigned long long u_int64_t;
  183. # 85 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  184. typedef int64_t register_t;
  185. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_intptr_t.h" 1 3 4
  186. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_intptr_t.h" 3 4
  187. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/types.h" 1 3 4
  188. # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_intptr_t.h" 2 3 4
  189. typedef __darwin_intptr_t intptr_t;
  190. # 93 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  191. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_uintptr_t.h" 1 3 4
  192. # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_uintptr_t.h" 3 4
  193. typedef unsigned long uintptr_t;
  194. # 94 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/types.h" 2 3 4
  195. typedef u_int64_t user_addr_t;
  196. typedef u_int64_t user_size_t;
  197. typedef int64_t user_ssize_t;
  198. typedef int64_t user_long_t;
  199. typedef u_int64_t user_ulong_t;
  200. typedef int64_t user_time_t;
  201. typedef int64_t user_off_t;
  202. typedef u_int64_t syscall_arg_t;
  203. # 36 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/types.h" 2 3 4
  204. # 32 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_va_list.h" 2 3 4
  205. typedef __darwin_va_list va_list;
  206. # 76 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4
  207. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_size_t.h" 1 3 4
  208. # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_size_t.h" 3 4
  209. typedef __darwin_size_t size_t;
  210. # 77 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4
  211. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_null.h" 1 3 4
  212. # 78 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4
  213. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/stdio.h" 1 3 4
  214. # 39 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/stdio.h" 3 4
  215. int renameat(int, const char *, int, const char *) __attribute__((availability(macosx,introduced=10.10)));
  216. int renamex_np(const char *, const char *, unsigned int) __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0)));
  217. int renameatx_np(int, const char *, int, const char *, unsigned int) __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0)));
  218. # 80 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4
  219. typedef __darwin_off_t fpos_t;
  220. # 92 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 3 4
  221. struct __sbuf {
  222. unsigned char *_base;
  223. int _size;
  224. };
  225. struct __sFILEX;
  226. # 126 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 3 4
  227. typedef struct __sFILE {
  228. unsigned char *_p;
  229. int _r;
  230. int _w;
  231. short _flags;
  232. short _file;
  233. struct __sbuf _bf;
  234. int _lbfsize;
  235. void *_cookie;
  236. int (* _Nullable _close)(void *);
  237. int (* _Nullable _read) (void *, char *, int);
  238. fpos_t (* _Nullable _seek) (void *, fpos_t, int);
  239. int (* _Nullable _write)(void *, const char *, int);
  240. struct __sbuf _ub;
  241. struct __sFILEX *_extra;
  242. int _ur;
  243. unsigned char _ubuf[3];
  244. unsigned char _nbuf[1];
  245. struct __sbuf _lb;
  246. int _blksize;
  247. fpos_t _offset;
  248. } FILE;
  249. # 65 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4
  250. extern FILE *__stdinp;
  251. extern FILE *__stdoutp;
  252. extern FILE *__stderrp;
  253. # 142 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  254. void clearerr(FILE *);
  255. int fclose(FILE *);
  256. int feof(FILE *);
  257. int ferror(FILE *);
  258. int fflush(FILE *);
  259. int fgetc(FILE *);
  260. int fgetpos(FILE * restrict, fpos_t *);
  261. char *fgets(char * restrict, int, FILE *);
  262. FILE *fopen(const char * restrict __filename, const char * restrict __mode) __asm("_" "fopen" );
  263. int fprintf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3)));
  264. int fputc(int, FILE *);
  265. int fputs(const char * restrict, FILE * restrict) __asm("_" "fputs" );
  266. size_t fread(void * restrict __ptr, size_t __size, size_t __nitems, FILE * restrict __stream);
  267. FILE *freopen(const char * restrict, const char * restrict,
  268. FILE * restrict) __asm("_" "freopen" );
  269. int fscanf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__scanf__, 2, 3)));
  270. int fseek(FILE *, long, int);
  271. int fsetpos(FILE *, const fpos_t *);
  272. long ftell(FILE *);
  273. size_t fwrite(const void * restrict __ptr, size_t __size, size_t __nitems, FILE * restrict __stream) __asm("_" "fwrite" );
  274. int getc(FILE *);
  275. int getchar(void);
  276. char *gets(char *);
  277. void perror(const char *) __attribute__((__cold__));
  278. int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2)));
  279. int putc(int, FILE *);
  280. int putchar(int);
  281. int puts(const char *);
  282. int remove(const char *);
  283. int rename (const char *__old, const char *__new);
  284. void rewind(FILE *);
  285. int scanf(const char * restrict, ...) __attribute__((__format__ (__scanf__, 1, 2)));
  286. void setbuf(FILE * restrict, char * restrict);
  287. int setvbuf(FILE * restrict, char * restrict, int, size_t);
  288. int sprintf(char * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))) __attribute__((__availability__(swift, unavailable, message="Use snprintf instead.")));
  289. int sscanf(const char * restrict, const char * restrict, ...) __attribute__((__format__ (__scanf__, 2, 3)));
  290. FILE *tmpfile(void);
  291. __attribute__((__availability__(swift, unavailable, message="Use mkstemp(3) instead.")))
  292. __attribute__((__deprecated__("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead.")))
  293. char *tmpnam(char *);
  294. int ungetc(int, FILE *);
  295. int vfprintf(FILE * restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0)));
  296. int vprintf(const char * restrict, va_list) __attribute__((__format__ (__printf__, 1, 0)));
  297. int vsprintf(char * restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))) __attribute__((__availability__(swift, unavailable, message="Use vsnprintf instead.")));
  298. # 205 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  299. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_ctermid.h" 1 3 4
  300. # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_ctermid.h" 3 4
  301. char *ctermid(char *);
  302. # 206 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4
  303. FILE *fdopen(int, const char *) __asm("_" "fdopen" );
  304. int fileno(FILE *);
  305. # 228 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  306. int pclose(FILE *) __attribute__((__availability__(swift, unavailable, message="Use posix_spawn APIs or NSTask instead.")));
  307. FILE *popen(const char *, const char *) __asm("_" "popen" ) __attribute__((__availability__(swift, unavailable, message="Use posix_spawn APIs or NSTask instead.")));
  308. # 249 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  309. int __srget(FILE *);
  310. int __svfscanf(FILE *, const char *, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
  311. int __swbuf(int, FILE *);
  312. # 260 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  313. inline __attribute__ ((__always_inline__)) int __sputc(int _c, FILE *_p) {
  314. if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
  315. return (*_p->_p++ = _c);
  316. else
  317. return (__swbuf(_c, _p));
  318. }
  319. # 286 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  320. void flockfile(FILE *);
  321. int ftrylockfile(FILE *);
  322. void funlockfile(FILE *);
  323. int getc_unlocked(FILE *);
  324. int getchar_unlocked(void);
  325. int putc_unlocked(int, FILE *);
  326. int putchar_unlocked(int);
  327. int getw(FILE *);
  328. int putw(int, FILE *);
  329. __attribute__((__availability__(swift, unavailable, message="Use mkstemp(3) instead.")))
  330. __attribute__((__deprecated__("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tempnam(3), it is highly recommended that you use mkstemp(3) instead.")))
  331. char *tempnam(const char *__dir, const char *__prefix) __asm("_" "tempnam" );
  332. # 324 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  333. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_off_t.h" 1 3 4
  334. # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_off_t.h" 3 4
  335. typedef __darwin_off_t off_t;
  336. # 325 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4
  337. int fseeko(FILE * __stream, off_t __offset, int __whence);
  338. off_t ftello(FILE * __stream);
  339. int snprintf(char * restrict __str, size_t __size, const char * restrict __format, ...) __attribute__((__format__ (__printf__, 3, 4)));
  340. int vfscanf(FILE * restrict __stream, const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
  341. int vscanf(const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 1, 0)));
  342. int vsnprintf(char * restrict __str, size_t __size, const char * restrict __format, va_list) __attribute__((__format__ (__printf__, 3, 0)));
  343. int vsscanf(const char * restrict __str, const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
  344. # 349 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  345. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_ssize_t.h" 1 3 4
  346. # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_ssize_t.h" 3 4
  347. typedef __darwin_ssize_t ssize_t;
  348. # 350 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4
  349. int dprintf(int, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))) __attribute__((availability(macosx,introduced=10.7)));
  350. int vdprintf(int, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))) __attribute__((availability(macosx,introduced=10.7)));
  351. ssize_t getdelim(char ** restrict __linep, size_t * restrict __linecapp, int __delimiter, FILE * restrict __stream) __attribute__((availability(macosx,introduced=10.7)));
  352. ssize_t getline(char ** restrict __linep, size_t * restrict __linecapp, FILE * restrict __stream) __attribute__((availability(macosx,introduced=10.7)));
  353. FILE *fmemopen(void * restrict __buf, size_t __size, const char * restrict __mode) __attribute__((availability(macos,introduced=10.13))) __attribute__((availability(ios,introduced=11.0))) __attribute__((availability(tvos,introduced=11.0))) __attribute__((availability(watchos,introduced=4.0)));
  354. FILE *open_memstream(char **__bufp, size_t *__sizep) __attribute__((availability(macos,introduced=10.13))) __attribute__((availability(ios,introduced=11.0))) __attribute__((availability(tvos,introduced=11.0))) __attribute__((availability(watchos,introduced=4.0)));
  355. # 367 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  356. extern const int sys_nerr;
  357. extern const char *const sys_errlist[];
  358. int asprintf(char ** restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3)));
  359. char *ctermid_r(char *);
  360. char *fgetln(FILE *, size_t *);
  361. const char *fmtcheck(const char *, const char *);
  362. int fpurge(FILE *);
  363. void setbuffer(FILE *, char *, int);
  364. int setlinebuf(FILE *);
  365. int vasprintf(char ** restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0)));
  366. FILE *zopen(const char *, const char *, int);
  367. FILE *funopen(const void *,
  368. int (* _Nullable)(void *, char *, int),
  369. int (* _Nullable)(void *, const char *, int),
  370. fpos_t (* _Nullable)(void *, fpos_t, int),
  371. int (* _Nullable)(void *));
  372. # 407 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4
  373. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 1 3 4
  374. # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 3 4
  375. # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_common.h" 1 3 4
  376. # 32 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 2 3 4
  377. # 42 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 3 4
  378. extern int __sprintf_chk (char * restrict, int, size_t,
  379. const char * restrict, ...);
  380. # 52 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 3 4
  381. extern int __snprintf_chk (char * restrict, size_t, int, size_t,
  382. const char * restrict, ...);
  383. extern int __vsprintf_chk (char * restrict, int, size_t,
  384. const char * restrict, va_list);
  385. extern int __vsnprintf_chk (char * restrict, size_t, int, size_t,
  386. const char * restrict, va_list);
  387. # 408 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4
  388. # 2 "my_sum.c" 2
  389. int sum(int a,int b) {
  390. int c = a+b;
  391. return c;
  392. }
  393. int main() {
  394. int a = 5;
  395. int b =3;
  396. int res = sum(a,b);
  397. printf("sum=%d",res);
  398. }

my_sum.s:

  1. .section __TEXT,__text,regular,pure_instructions
  2. .build_version macos, 11, 0 sdk_version 11, 3
  3. .globl _sum ## -- Begin function sum
  4. .p2align 4, 0x90
  5. _sum: ## @sum
  6. .cfi_startproc
  7. ## %bb.0:
  8. pushq %rbp
  9. .cfi_def_cfa_offset 16
  10. .cfi_offset %rbp, -16
  11. movq %rsp, %rbp
  12. .cfi_def_cfa_register %rbp
  13. movl %edi, -4(%rbp)
  14. movl %esi, -8(%rbp)
  15. movl -4(%rbp), %eax
  16. addl -8(%rbp), %eax
  17. movl %eax, -12(%rbp)
  18. movl -12(%rbp), %eax
  19. popq %rbp
  20. retq
  21. .cfi_endproc
  22. ## -- End function
  23. .globl _main ## -- Begin function main
  24. .p2align 4, 0x90
  25. _main: ## @main
  26. .cfi_startproc
  27. ## %bb.0:
  28. pushq %rbp
  29. .cfi_def_cfa_offset 16
  30. .cfi_offset %rbp, -16
  31. movq %rsp, %rbp
  32. .cfi_def_cfa_register %rbp
  33. subq $16, %rsp
  34. movl $5, -4(%rbp)
  35. movl $3, -8(%rbp)
  36. movl -4(%rbp), %edi
  37. movl -8(%rbp), %esi
  38. callq _sum
  39. movl %eax, -12(%rbp)
  40. movl -12(%rbp), %esi
  41. leaq L_.str(%rip), %rdi
  42. movb $0, %al
  43. callq _printf
  44. xorl %ecx, %ecx
  45. movl %eax, -16(%rbp) ## 4-byte Spill
  46. movl %ecx, %eax
  47. addq $16, %rsp
  48. popq %rbp
  49. retq
  50. .cfi_endproc
  51. ## -- End function
  52. .section __TEXT,__cstring,cstring_literals
  53. L_.str: ## @.str
  54. .asciz "sum=%d"
  55. .subsections_via_symbols

发表评论

表情:
评论列表 (有 0 条评论,135人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Linux使用vi/vim、gcc编译

    我们使用vi或vim进行编译时,(i进入编辑模式,ESC进入命令模式,命令模式下 ”:wq“为保存并退出),程序写好保存退出后,我们需要先对文件进行编译,再通过生成文件运行程序