编译和链接 —— C 语言程序设计

编译和链接 —— C 语言程序设计

C 语言编译链接

编译和链接

把程序转化为机器可以执行的形式,对 C 语言来说,通常包含下列 3 个步骤1

  1. 预处理。首先程序会被交给预处理器 (preprocessor)。预处理器执行以 # 开头的命令(通常称为指令)。预处理器有点类似于编辑器,它可以给程序添加内容,也可以对程序进行修改。

  2. 编译。修改后的程序现在可以进入编译器 (compiler) 了。编译器会把程序翻译成机器指令(即目标代码)。然而,这样的程序还是不可以运行的。

  3. 链接。在最后一步,链接器 (linker) 把由编译器产生的目标代码和所需的其他附加代码整合在一起,这样才最终产生了完全可执行的程序。这些附加代码包括程序中用到的库函数(如 printf 函数)。

在 UNIX 系统环境下,通常把 C 编译器命名为 cc。为了编译和链接 C 程序,需要在终端或命令行窗口录入如下命令:

cc hello.c

在使用编译器 cc 时,系统会自动进行链接操作,而无需单独的链接命令。

在编译和链接好程序后,编译器 cc 会把可执行程序放到默认名为 a.out 的文件中。编译器 cc 由许多选项,其中 -o 选项允许为含有可执行程序的文件选择名字。例如:

cc -o hello hello.c

GCC 编译器时最流行的 C 编译器之一,它随 Linux 发行。与传统的 UNIX CC 编译器相似,编译 C 程序可以使用一下命令:

gcc -o hello hello.c
  1. K. N. King, etc., 2010. C语言程序设计:现代方法(第2版). 

THE END

林宏

Frank Lin

Hey, there! This is Frank Lin (@flinhong), one of the 1.41 billion . This 'inDev. Journal' site holds the exploration of my quirky thoughts and random adventures through life. Hope you enjoy reading and perusing my posts.

YOU MAY ALSO LIKE

HTML 相对路径和绝对路径区别分析

Web Notes

2015.09.26

HTML 相对路径和绝对路径区别分析

HTML 初学者会经常遇到这样一个问题,如何正确引用一个文件。比如,怎样在一个 HTML 网页中引用另外一个 HTML 网页作为超链接(hyperlink),怎样在一个网页中插入一张图片。如果你在引用文件时(如加入超链接,或者插入图片等),使用了错误的文件路径,就会导致引用失效(无法浏览链接文件,或无法显示插入的图片等)。

Using Liquid in Jekyll - Live with Demos

Web Notes

2016.08.20

Using Liquid in Jekyll - Live with Demos

Liquid is a simple template language that Jekyll uses to process pages for your site. With Liquid you can output complex contents without additional plugins.

GitHub Markdown emojis for Jekyll with JoyPixels icons

Tools

2022.05.23

GitHub Markdown emojis for Jekyll with JoyPixels icons

It's time to add a new plugin for my Jekyll site to support emojis, with a convenient and consistent way. Also a cheat sheet list for available emojis in GitHub flavoured Markdown documents using JoyPixels icons is presented.

Ads by Google