---
title: "c语言的编译过程"
author: "xiaobaiso"
date: "2011-12-15T09:29:26Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: true
lastmod: "2011-12-15T23:33:12Z"
id: 1311537
url: "https://xiaobaiso.iteye.com/blog/1311537"
---

[http://hi.baidu.com/wangsl1978/blog/item/89229516a59a0512c83d6d01.html](http://hi.baidu.com/wangsl1978/blog/item/89229516a59a0512c83d6d01.html)

先感谢这篇文章 介绍了ld的库文件怎么引用的

代码如下

----------------------- test.c-----------------------------

#include&lt;stdio.h&gt;

Void mian()

{

       Printf(“hello”);

}

 

**今天看了下编译的过程，gcc –o test text.c   是一步到位的做法**

 

**要像一步一步做出来 了解怎么编译的，是这样的**

 

\[root@localhost workspace]# cpp test.c test.i

得到了预处理文件。

 

\[root@localhost 4.1.1]# cc1 test.c  test.s

这里就得到了编译后的文件

这里cc1命令不一定在环境变量中，gcc -v hello.c 2&gt;&1 |grep cc1  可以看到cc1在哪个目录下  执行的时候可以把test.c移到该文件夹下 用./cc1

 

\[root@localhost 4.1.1]# as -o test.o test.s

现在得到了汇编文件

 

\[root@localhost 4.1.1]# ld -dynamic-linker /lib/ld-linux.so.2 -o  test   /usr/lib/crt1.o     /usr/lib/crti.o   test.o   -lc     /usr/lib/crtn.o

 

得到了test

\[root@localhost 4.1.1]# ./test  
hello\[root@localhost 4.1.1]# 

 

 

**最终编译完成了**

 

 

**下面是扩展  这篇文章对编译有一个具体的讲述**

[http://blog.csdn.net/eroswang/article/details/5983791](http://blog.csdn.net/eroswang/article/details/5983791)

 

 

**这里写一点我的理解：**

1\.

在命令行定义宏：gcc -Dmacro hello.c  
等同于在文件的开头定义宏，即#define maco，但是**在命令行定义更灵活**。例如，在源代码中有这些语句。  
**#ifdef DEBUG**  
**printf("this code is for debugging/n");**  
**#endif**  
    **如果编译时加上-DDEBUG选项    （要多加一个D），那么编译器就会把printf所在的行编译进目标代码，从而方便地跟踪该位置的某些程序状态。这样-DDEBUG就可以当作一个调试开关，编译时加上它就可以用来打印调试信息，发布时则可以通过去掉该编译选项把调试信息去掉。**

 

 

2