首页 技术 正文
技术 2022年11月15日
0 收藏 357 点赞 4,128 浏览 17376 个字

在上一篇文章中,我们说到,C 语言系统应该由程序开发环境,C 语言本身和 C 语言的库组成。且同时说了程序开发环境做了“编写”,“预处理”,“编译”和“链接”这几件事情。但是细节并没有一一呈现。不知道同学们是否想过,这里都经历了一些什么呢?!

在这一篇文章中,我们就来看看“上帝说,要有光,就有了光。”是怎么实现的。假设你是上帝,想要实现“给我输出 ‘HELLO,WORLD!’”。同学们使用的 IDE 是 C-Free 5,这里的所有操作,都以此编译器为例。假设编译器安装路径为“C:\Program Files\C-Free 5”。

1. 代码编写

同学们已经上过第一节课了,我想,编写一个“HELLO,WORLD!”一定不成问题。代码本身是这样的:

 #include <stdio.h> int main(void)
{
printf("HELLO,WORLD!\n");
return ;
}

将此代码保存为“HelloWord.c”后准备下一步的处理。

2. 预处理

在保存了“HelloWord.c”文件夹中打开一个“cmd”窗口(按住 Shift 后右击选择“在此处打开命令窗口”),输入:

gcc -E HelloWorld.c -o HelloWorld.i
# 命令解释:
# 在 C-Free 身后,默默工作的就是这个名叫 gcc 的编译器了。
# 当输入上面的命令,紧跟其后的“-E”告知编译器想要做的是动作是仅做预处理这个动作。
# 然后后面跟随的“.c”文件告知 gcc 需要处理的文件的名称
# -o HelloWorld.i,是告诉编译器,输出一个文件,文件名就是“HelloWorld.i”
# (你看,计算机就是一个输入然后输出的系统吧)

你会发现,在此目录下生成了一个名为“HelloWorld.i”的文件,打开文件,观察内容你会发现是这样的:

 #  "HelloWorld.c"
# "<built-in>"
# "<command line>"
# "HelloWorld.c"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h" # "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" # "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stddef.h" # "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h"
typedef unsigned int size_t;
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h"
typedef short unsigned int wchar_t;
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h"
typedef short unsigned int wint_t;
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stddef.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" # "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdarg.h" # "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stdarg.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stdarg.h"
typedef __builtin_va_list __gnuc_va_list;
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdarg.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
typedef struct _iobuf
{
char* _ptr;
int _cnt;
char* _base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char* _tmpfname;
} FILE;
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
extern __attribute__ ((__dllimport__)) FILE _iob[];
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fopen (const char*, const char*);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) freopen (const char*, const char*, FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fflush (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fclose (FILE*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) remove (const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) rename (const char*, const char*);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) tmpfile (void);
char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) tmpnam (char*); char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _tempnam (const char*, const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _rmtmp(void);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _unlink (const char*); char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) tempnam (const char*, const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) rmtmp(void);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) unlink (const char*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) setvbuf (FILE*, char*, int, size_t); void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) setbuf (FILE*, char*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fprintf (FILE*, const char*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) printf (const char*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) sprintf (char*, const char*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _snprintf (char*, size_t, const char*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vfprintf (FILE*, const char*, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vprintf (const char*, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vsprintf (char*, const char*, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _vsnprintf (char*, size_t, const char*, __gnuc_va_list);
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) snprintf(char *, size_t, const char *, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vsnprintf (char *, size_t, const char *, __gnuc_va_list); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vscanf (const char * __restrict__, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vfscanf (FILE * __restrict__, const char * __restrict__,
__gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vsscanf (const char * __restrict__,
const char * __restrict__, __gnuc_va_list); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fscanf (FILE*, const char*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) scanf (const char*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) sscanf (const char*, const char*, ...); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetc (FILE*);
char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgets (char*, int, FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputc (int, FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputs (const char*, FILE*);
char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) gets (char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) puts (const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ungetc (int, FILE*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _filbuf (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _flsbuf (int, FILE*); extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getc (FILE* __F)
{
return (--__F->_cnt >= )
? (int) (unsigned char) *__F->_ptr++
: _filbuf (__F);
} extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putc (int __c, FILE* __F)
{
return (--__F->_cnt >= )
? (int) (unsigned char) (*__F->_ptr++ = (char)__c)
: _flsbuf (__c, __F);
} extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getchar (void)
{
return (--(&_iob[])->_cnt >= )
? (int) (unsigned char) *(&_iob[])->_ptr++
: _filbuf ((&_iob[]));
} extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putchar(int __c)
{
return (--(&_iob[])->_cnt >= )
? (int) (unsigned char) (*(&_iob[])->_ptr++ = (char)__c)
: _flsbuf (__c, (&_iob[]));}
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
size_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fread (void*, size_t, size_t, FILE*);
size_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fwrite (const void*, size_t, size_t, FILE*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fseek (FILE*, long, int);
long __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ftell (FILE*);
void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) rewind (FILE*);
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
typedef long long fpos_t; int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetpos (FILE*, fpos_t*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fsetpos (FILE*, const fpos_t*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) feof (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ferror (FILE*);
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) clearerr (FILE*);
void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) perror (const char*); FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _popen (const char*, const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _pclose (FILE*); FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) popen (const char*, const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) pclose (FILE*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _flushall (void);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fgetchar (void);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fputchar (int);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fdopen (int, const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fileno (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fcloseall(void);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fsopen(const char*, const char*, int); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _getmaxstdio(void);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _setmaxstdio(int);
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetchar (void);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputchar (int);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fdopen (int, const char*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fileno (FILE*);
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/types.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/types.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stddef.h" # "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/include/stddef.h"
typedef int ptrdiff_t;
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stddef.h"
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/types.h" typedef long time_t; typedef long long __time64_t; typedef long _off_t; typedef _off_t off_t; typedef unsigned int _dev_t; typedef _dev_t dev_t; typedef short _ino_t; typedef _ino_t ino_t; typedef int _pid_t; typedef _pid_t pid_t; typedef unsigned short _mode_t; typedef _mode_t mode_t; typedef int _sigset_t; typedef _sigset_t sigset_t; typedef long _ssize_t; typedef _ssize_t ssize_t; typedef long long fpos64_t; typedef long long off64_t;
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
extern __inline__ FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fopen64 (const char* filename, const char* mode)
{
return fopen (filename, mode);
} int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fseeko64 (FILE*, off64_t, int); extern __inline__ off64_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ftello64 (FILE * stream)
{
fpos_t pos;
if (fgetpos(stream, &pos))
return -1LL;
else
return ((off64_t) pos);
}
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fwprintf (FILE*, const wchar_t*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) wprintf (const wchar_t*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) swprintf (wchar_t*, const wchar_t*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _snwprintf (wchar_t*, size_t, const wchar_t*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vfwprintf (FILE*, const wchar_t*, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vwprintf (const wchar_t*, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vswprintf (wchar_t*, const wchar_t*, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _vsnwprintf (wchar_t*, size_t, const wchar_t*, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fwscanf (FILE*, const wchar_t*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) wscanf (const wchar_t*, ...);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) swscanf (const wchar_t*, const wchar_t*, ...);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetwc (FILE*);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputwc (wchar_t, FILE*);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) ungetwc (wchar_t, FILE*); wchar_t* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetws (wchar_t*, int, FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputws (const wchar_t*, FILE*);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getwc (FILE*);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getwchar (void);
wchar_t* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _getws (wchar_t*);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putwc (wint_t, FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _putws (const wchar_t*);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putwchar (wint_t);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wfdopen(int, wchar_t *);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wfopen (const wchar_t*, const wchar_t*);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wfreopen (const wchar_t*, const wchar_t*, FILE*);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wfsopen (const wchar_t*, const wchar_t*, int);
wchar_t* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wtmpnam (wchar_t*);
wchar_t* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wtempnam (const wchar_t*, const wchar_t*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wrename (const wchar_t*, const wchar_t*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wremove (const wchar_t*);
void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wperror (const wchar_t*);
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _wpopen (const wchar_t*, const wchar_t*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) snwprintf (wchar_t* s, size_t n, const wchar_t* format, ...);
extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__))
vsnwprintf (wchar_t* s, size_t n, const wchar_t* format, __gnuc_va_list arg)
{ return _vsnwprintf ( s, n, format, arg);}
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vwscanf (const wchar_t * __restrict__, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vfwscanf (FILE * __restrict__,
const wchar_t * __restrict__, __gnuc_va_list);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) vswscanf (const wchar_t * __restrict__,
const wchar_t * __restrict__, __gnuc_va_list);
# "C:/Program Files (x86)/C-Free 5/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h"
FILE* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) wpopen (const wchar_t*, const wchar_t*); wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fgetwchar (void);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _fputwchar (wint_t);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _getw (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _putw (int, FILE*); wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetwchar (void);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputwchar (wint_t);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getw (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putw (int, FILE*);
# "HelloWorld.c" int main(void)
{
printf("HELLO,WORLD!\n");
return ;
}

拖动到最底部,才能够发现我们最开始写的那几行代码。但是代码又和我们之前写的似而不同:

  int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getw (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putw (int, FILE*);
# "HelloWorld.c" int main(void)
{
printf("HELLO,WORLD!\n");
return ;
}

可以发现,第一行的 #include <stdio.h> 却再也找不到了。其实,这就是预处理的功能,它会将你的代码中的 #include <XXXXXXX.h>  全部替换到,为下面的编译做准备。

需要注意的是,预处理阶段,编译器 gcc 是不做语法上的检查的,比如我们写一段貌合神离的代码:

 #include <stdio.h> printf("HELLO,WORLD!\n");

然后重新执行上面的命令,你会发现,依旧会生成一个看似可用的“.i”文件。

3. 编译

编译阶段,编译器 gcc 将会对之前预处理生成的“.i”文件进行语法上的检查,看其是否符合 C 语言的语法规范,当检查通过以后,就进行编译的过程。这里,将其过程分为两步来看,首先,将预处理文件翻译为汇编文件:

 gcc -S HelloWorld.i -o helloWorld.s

然后将汇编文件编译为目标文件:

 gcc -c HelloWorld.s -o HelloWorld.obj

这里我们再来试试之前的“李鬼”代码,看看它能否通过流程,生成汇编文件:

 gcc -S HelloWorld.i -o helloWorld.s
HelloWorldError.c:3: error: syntax error before string constant
HelloWorldError.c:3: error: conflicting types for 'printf'
HelloWorldError.c:3: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
HelloWorldError.c:3: error: conflicting types for 'printf'
HelloWorldError.c:3: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
HelloWorldError.c:3: warning: data definition has no type or storage class

哎呦,出了一堆的错误唉,有此可见,编译过程是需要执行内容的检查的,以确定其符合 C 语言的语法。

4. 链接

在上一步中,生成的目标文件“.obj”虽然本身也是二进制文件,但是其本身是不可执行的,需要对其进行链接。

 gcc helloworld.obj -o helloworld.exe

到这里,就生成可以执行的二进制文件了,赶快在终端中输入“HelloWorld”尝试运行吧:

C 语言学习的第 03 课:你的 idea 是怎么变成能够执行的程序的

虽然同学们可能直接在 C-Free 中仅需要点击一下按钮,后台就已经给你做了以上的这些事情,但是这里还是希望同学们能够理解这一过程(链接这一过程涉及到 C 语言库函数的事情,大家可以再扩展阅读哦)。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,967
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783