C 语言学习的第 03 课:你的 idea 是怎么变成能够执行的程序的
Posted 记东记西的 BLOG
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C 语言学习的第 03 课:你的 idea 是怎么变成能够执行的程序的相关的知识,希望对你有一定的参考价值。
在上一篇文章中,我们说到,C 语言系统应该由程序开发环境,C 语言本身和 C 语言的库组成。且同时说了程序开发环境做了“编写”,“预处理”,“编译”和“链接”这几件事情。但是细节并没有一一呈现。不知道同学们是否想过,这里都经历了一些什么呢?!
在这一篇文章中,我们就来看看“上帝说,要有光,就有了光。”是怎么实现的。假设你是上帝,想要实现“给我输出 \'HELLO,WORLD!\'”。同学们使用的 IDE 是 C-Free 5,这里的所有操作,都以此编译器为例。假设编译器安装路径为“C:\\Program Files\\C-Free 5”。
1. 代码编写
同学们已经上过第一节课了,我想,编写一个“HELLO,WORLD!”一定不成问题。代码本身是这样的:
1 #include <stdio.h> 2 3 int main(void) 4 { 5 printf("HELLO,WORLD!\\n"); 6 return 0; 7 }
将此代码保存为“HelloWord.c”后准备下一步的处理。
2. 预处理
在保存了“HelloWord.c”文件夹中打开一个“cmd”窗口(按住 Shift 后右击选择“在此处打开命令窗口”),输入:
gcc -E HelloWorld.c -o HelloWorld.i # 命令解释: # 在 C-Free 5 身后,默默工作的就是这个名叫 gcc 的编译器了。 # 当输入上面的命令,紧跟其后的“-E”告知编译器想要做的是动作是仅做预处理这个动作。 # 然后后面跟随的“.c”文件告知 gcc 需要处理的文件的名称 # -o HelloWorld.i,是告诉编译器,输出一个文件,文件名就是“HelloWorld.i” # (你看,计算机就是一个输入然后输出的系统吧)
你会发现,在此目录下生成了一个名为“HelloWorld.i”的文件,打开文件,观察内容你会发现是这样的:
1 # 1 "HelloWorld.c" 2 # 1 "<built-in>" 3 # 1 "<command line>" 4 # 1 "HelloWorld.c" 5 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 1 3 6 # 19 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 7 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h" 1 3 8 # 27 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h" 3 9 10 # 28 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h" 3 11 # 20 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 2 3 12 13 14 15 16 17 18 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stddef.h" 1 3 19 20 21 22 23 24 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h" 1 3 4 25 # 213 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h" 3 4 26 typedef unsigned int size_t; 27 # 325 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h" 3 4 28 typedef short unsigned int wchar_t; 29 # 354 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h" 3 4 30 typedef short unsigned int wint_t; 31 # 7 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stddef.h" 2 3 32 # 27 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 2 3 33 34 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdarg.h" 1 3 35 36 37 38 39 40 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stdarg.h" 1 3 4 41 # 44 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stdarg.h" 3 4 42 typedef __builtin_va_list __gnuc_va_list; 43 # 7 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdarg.h" 2 3 44 # 29 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 2 3 45 # 129 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 46 typedef struct _iobuf 47 { 48 char* _ptr; 49 int _cnt; 50 char* _base; 51 int _flag; 52 int _file; 53 int _charbuf; 54 int _bufsiz; 55 char* _tmpfname; 56 } FILE; 57 # 154 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 58 extern __attribute__ ((__dllimport__)) FILE _iob[]; 59 # 169 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 60 FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fopen (const char*, const char*); 61 FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) freopen (const char*, const char*, FILE*); 62 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fflush (FILE*); 63 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fclose (FILE*); 64 65 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) remove (const char*); 66 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) rename (const char*, const char*); 67 FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) tmpfile (void); 68 char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) tmpnam (char*); 69 70 71 char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _tempnam (const char*, const char*); 72 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _rmtmp(void); 73 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _unlink (const char*); 74 75 76 char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) tempnam (const char*, const char*); 77 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) rmtmp(void); 78 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) unlink (const char*); 79 80 81 82 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) setvbuf (FILE*, char*, int, size_t); 83 84 void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) setbuf (FILE*, char*); 85 86 87 88 89 90 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fprintf (FILE*, const char*, ...); 91 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) printf (const char*, ...); 92 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) sprintf (char*, const char*, ...); 93 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _snprintf (char*, size_t, const char*, ...); 94 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vfprintf (FILE*, const char*, __gnuc_va_list); 95 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vprintf (const char*, __gnuc_va_list); 96 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vsprintf (char*, const char*, __gnuc_va_list); 97 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _vsnprintf (char*, size_t, const char*, __gnuc_va_list); 98 # 216 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 99 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) snprintf(char *, size_t, const char *, ...); 100 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vsnprintf (char *, size_t, const char *, __gnuc_va_list); 101 102 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vscanf (const char * __restrict__, __gnuc_va_list); 103 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vfscanf (FILE * __restrict__, const char * __restrict__, 104 __gnuc_va_list); 105 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vsscanf (const char * __restrict__, 106 const char * __restrict__, __gnuc_va_list); 107 108 109 110 111 112 113 114 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fscanf (FILE*, const char*, ...); 115 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) scanf (const char*, ...); 116 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) sscanf (const char*, const char*, ...); 117 118 119 120 121 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetc (FILE*); 122 char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgets (char*, int, FILE*); 123 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputc (int, FILE*); 124 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputs (const char*, FILE*); 125 char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) gets (char*); 126 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) puts (const char*); 127 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ungetc (int, FILE*); 128 129 130 131 132 133 134 135 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _filbuf (FILE*); 136 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _flsbuf (int, FILE*); 137 138 139 140 extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getc (FILE* __F) 141 { 142 return (--__F->_cnt >= 0) 143 ? (int) (unsigned char) *__F->_ptr++ 144 : _filbuf (__F); 145 } 146 147 extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putc (int __c, FILE* __F) 148 { 149 return (--__F->_cnt >= 0) 150 ? (int) (unsigned char) (*__F->_ptr++ = (char)__c) 151 : _flsbuf (__c, __F); 152 } 153 154 extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getchar (void) 155 { 156 return (--(&_iob[0])->_cnt >= 0) 157 ? (int) (unsigned char) *(&_iob[0])->_ptr++ 158 : _filbuf ((&_iob[0])); 159 } 160 161 extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putchar(int __c) 162 { 163 return (--(&_iob[1])->_cnt >= 0) 164 ? (int) (unsigned char) (*(&_iob[1])->_ptr++ = (char)__c) 165 : _flsbuf (__c, (&_iob[1]));} 166 # 297 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 167 size_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fread (void*, size_t, size_t, FILE*); 168 size_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fwrite (const void*, size_t, size_t, FILE*); 169 170 171 172 173 174 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fseek (FILE*, long, int); 175 long __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ftell (FILE*); 176 void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) rewind (FILE*); 177 # 340 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 178 typedef long long fpos_t; 179 180 181 182 183 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetpos (FILE*, fpos_t*); 184 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fsetpos (FILE*, const fpos_t*); 185 186 187 188 189 190 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) feof (FILE*); 191 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ferror (FILE*); 192 # 365 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 193 void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) clearerr (FILE*); 194 void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) perror (const char*); 195 196 197 198 199 200 201 FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _popen (const char*, const char*); 202 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _pclose (FILE*); 203 204 205 FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) popen (const char*, const char*); 206 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) pclose (FILE*); 207 208 209 210 211 212 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _flushall (void); 213 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fgetchar (void); 214 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fputchar (int); 215 FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fdopen (int, const char*); 216 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fileno (FILE*); 217 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fcloseall(void); 218 FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fsopen(const char*, const char*, int); 219 220 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _getmaxstdio(void); 221 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _setmaxstdio(int); 222 # 402 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 223 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetchar (void); 224 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputchar (int); 225 FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fdopen (int, const char*); 226 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fileno (FILE*); 227 # 414 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 228 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/types.h" 1 3 229 # 21 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/types.h" 3 230 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stddef.h" 1 3 231 232 233 234 235 236 # 1 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h" 1 3 4 237 # 151 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h" 3 4 238 typedef int ptrdiff_t; 239 # 7 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stddef.h" 2 3 240 # 22 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/types.h" 2 3 241 242 243 244 245 246 typedef long time_t; 247 248 249 250 251 typedef long long __time64_t; 252 253 254 255 256 257 typedef long _off_t; 258 259 260 typedef _off_t off_t; 261 262 263 264 265 266 267 268 typedef unsigned int _dev_t; 269 270 271 272 273 274 typedef _dev_t dev_t; 275 276 277 278 279 280 281 typedef short _ino_t; 282 283 284 typedef _ino_t ino_t; 285 286 287 288 289 290 291 typedef int _pid_t; 292 293 294 typedef _pid_t pid_t; 295 296 297 298 299 300 301 typedef unsigned short _mode_t; 302 303 304 typedef _mode_t mode_t; 305 306 307 308 309 310 311 typedef int _sigset_t; 312 313 314 typedef _sigset_t sigset_t; 315 316 317 318 319 320 typedef long _ssize_t; 321 322 323 typedef _ssize_t ssize_t; 324 325 326 327 328 329 typedef long long fpos64_t; 330 331 332 333 334 typedef long long off64_t; 335 # 415 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 2 3 336 extern __inline__ FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fopen64 (const char* filename, const char* mode) 337 { 338 return fopen (filename, mode); 339 } 340 341 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fseeko64 (FILE*, off64_t, int); 342 343 344 345 346 347 348 extern __inline__ off64_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ftello64 (FILE * stream) 349 { 350 fpos_t pos; 351 if (fgetpos(stream, &pos)) 352 return -1LL; 353 else 354 return ((off64_t) pos); 355 } 356 # 443 "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3 357 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fwprintf (FILE*, const wchar_t*, ...); 358 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) wprintf (const wchar_t*, ...); 359 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) swprintf (wchar_t*, const wchar_t*, ...); 360 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _snwprintf (wchar_t*, size_t, const wchar_t*, ...); 361 int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) v以上是关于C 语言学习的第 03 课:你的 idea 是怎么变成能够执行的程序的的主要内容,如果未能解决你的问题,请参考以下文章