C语言笔记之头文件与链接(一)
来源:互联网 发布:aj1高仿淘宝店 编辑:程序博客网 时间:2024/06/05 10:24
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">虽然一直在用#include命令包含头文件,但其实一致不太明白头文件的原理。今天就研究了一下。</span></span>
首先,在大型项目中,仅仅一个源文件是不够的,巨大的代码量需要分别放在几个文件中,当然分开存放的更主要的目的是便于模块化。我们把代码按照不同的功能或作用分隔存放在不同的文件中,那么当其中一个功能有改动时,只需要重新编译相关的文件,而不必编译整个项目的所有源文件。
但是,这样就带来了一个问题:在一个文件中定义的变量或函数,能不能在另一个文件中使用呢?或者两个文件中同名的变量会不会引起冲突呢?
为了回答这个问题,首先要明白C语言的源代码如何一步步生成可执行代码的。我们先看只有一个源文件的情况:
首先经过预处理器,替换掉文件中的宏命令;
然后经过编译器,生成汇编代码;
接着是汇编器,生成二进制的目标代码,然而此时的目标代码仍然不能执行,它还缺少启动代码(程序和操作系统之间的接口)和库代码(比如printf函数的实体代码);
最后经过链接器,链接相关的代码,生成最终的可执行代码。
既然提到了编译,就不得不介绍一下C语言的编译器gcc,假设我们写好了一个源文件first.c,那么对应上面的步骤,gcc的命令参数如下:
预编译: gcc -E first.c -o first_1.c (注:-o 选项用来指定生成结果的文件名)
汇编: gcc -S first.c -o first.s
编译: gcc -c first.s -o first.o (也可以直接编译源码:gcc -c first.c -o first.o)
可执行: gcc first.o -o first (当然,这里也可以一步到位:gcc first.c -o first)
现在我们把目光集中到链接过程上。从上面的分析可以知道,所谓链接,就是把目标代码、启动代码和库代码结合到一起形成可执行代码。上面是只有一个源文件的情况,如果有多个文件,则把多个目标代码与启动代码和库代码粘合在一起。那么问题来了:多个目标代码真的就能随随便便粘合在一起吗?
要回答这个问题,还是得回到对源代码的分析上,毕竟目标代码只是源代码的编译版本。虽然源代码被分隔成几个部分并存放到不同的文件中,但是在逻辑或者上下文中,还是必须要保持一致的。也就是说,把几个文件中的代码重新放回到一个文件中,它们还是要保持“兼容”的,比如变量啊、函数啊之类的,不能重复;再比如只能有一个main函数。
然而,我们知道,变量和函数的作用域,最大的也就是文件作用域了。???比如,如何保证一个文件中的变量也被其他的文件直接使用并且不会引起冲突呢?答案就是头文件。头文件,就是把各个被分割的文件在逻辑上串起来的关键。
现在给出一个例子,在这个例子中,我用C代码模仿游戏“石头剪子布”,0、1、2分别代表石头、剪子、布。游戏过程中,程序随机产生一个数,同时提示用户输入一个数,然后根据规则做出输赢判断。完整的代码如下:
#include <stdio.h>#include <math.h>#include <stdlib.h>int gen_rnd(void);void judge(int, int);int main(void){ int user, computer; printf("Please input a number, 0 for stone, 1 for scissors, 2 for cloth and q for quit: "); while(scanf("%d", &user) && user != 'q') { if(user > 2 || user < 0) { printf("Please input a number between 0 and 2: "); continue; } computer = gen_rnd(); judge(user, computer); printf("number: "); } return 0;}int gen_rnd(void){ int ret; time_t t; srand((unsigned)time(&t)); ret = rand() % 3; return ret;}void judge(int user, int computer){ char *name[] = {"stone", "scissors", "cloth"}; int res = abs(user - computer); if(res == 0) printf("The computer is %s and you are %s, even\n", name[computer], name[user]); else if(res == 1) { if(user < computer) printf("The computer is %s and you are %s, you win\n", name[computer], name[user]); else printf("The computer is %s and you are %s, you lose\n", name[computer], name[user]); } else { if(user < computer) printf("The computer is %s and you are %s, you lose\n", name[computer], name[user]); else printf("The computer is %s and you are %s, you win\n", name[computer], name[user]); }}
file.c
#include <stdio.h>#include <math.h>int gen_rnd(void);void judge(int, int);int main(void){ int user, computer; printf("Please input a number, 0 for stone, 1 for scissors, 2 for cloth and q for quit: "); while(scanf("%d", &user) && user != 'q') { if(user > 2 || user < 0) { printf("Please input a number between 0 and 2: "); continue; } computer = gen_rnd(); judge(user, computer); printf("number: "); } return 0;}main.c
<span style="font-size:12px;">#include <stdlib.h>int gen_rnd(void){ int ret; time_t t; srand((unsigned)time(&t)); ret = rand() % 3; return ret;}</span>gen_rnd.c
<span style="font-family: Arial, Helvetica, sans-serif;font-size:12px;">#include <stdio.h></span>
<span style="font-size:12px;"></span><pre name="code" class="plain">void judge(int user, int computer){ char *name[] = {"stone", "scissors", "cloth"}; int res = abs(user - computer); if(res == 0) printf("The computer is %s and you are %s, even\n", name[computer], name[user]); else if(res == 1) { if(user < computer) printf("The computer is %s and you are %s, you win\n", name[computer], name[user]); else printf("The computer is %s and you are %s, you lose\n", name[computer], name[user]); } else { if(user < computer) printf("The computer is %s and you are %s, you lose\n", name[computer], name[user]); else printf("The computer is %s and you are %s, you win\n", name[computer], name[user]); }}judge.c
可以看到,由于成为了单独的文件,judge.c必须要自己包含<stdio.h>,否则编译目标文件时会报错:
m@sys:~/program/C_codes$ gcc -c judge.c judge.c: In function ‘judge’:judge.c:8:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default] printf("The computer is %s and you are %s, even\n", name[computer], name[user]); ^同样的道理,gen_rnd.c则要自己包含<stdlib.c>,而main.c则不需要这个头文件了。
现在,我们分别为其生成目标文件:
gcc -c judge.c main.c gen_rnd.c
这会在当前目录下自动生成gen_rnd.o judge.o main.o
接着就可以生成可执行文件了:gcc gen_rnd.o judge.o main.o -o exe
这三个目标文件之所以还能被正确的粘合在一起,是因为它们仍然存在着逻辑上的联系:首先,只有main.c文件有一个main函数,这就提供了正确的入口;其次,各个文件都能包含需要的头文件,从而正确的生成各自的目标代码;再次,因为main.c要调用另外两个函数,所以声明了另外两个函数的原型,虽然该文件中没有它们的代码,但是在链接阶段两个函数的代码却会一起组合到可执行文件中,同样的道理,printf()等函数的代码也会在链接阶段被组合到可执行文件中,即所谓的链接库文件。
- C语言笔记之头文件与链接(一)
- C语言笔记之头文件与链接(二)
- C语言(预处理与头文件)
- C语言实现链表之单向链表(一)头文件
- C语言实现链表之双向链表(一)头文件
- C语言笔记(头文件,库,结构体)
- 头文件与链接
- C语言头文件的使用与写法(转贴)
- C语言中的头文件与原文件
- C语言中的头文件与原文件
- C语言中的头文件与原文件
- C语言中的头文件与源文件
- 分离链接散列表头文件C语言
- c语言一起学习之建立头文件(二)
- C语言头文件之float.h
- C语言头文件之ctype.h
- c语言之头文件的使用
- C语言之头文件包含问题
- 黑马程序员--java基础复习之网络编程
- C++第二十题20150412
- 纯css制作的漂亮好看的进度条
- 正确解决 Invalid module format
- 手机产品设计禁忌
- C语言笔记之头文件与链接(一)
- Linux内存点滴:用户进程内存空间
- OpenCv3.0+SVM的使用心得(二)
- 深入浅出UML类图(二)
- LeetCode # 3Sum #
- postgresql时间戳转字符串
- nyoj 17 单调递增最长子序列
- 走在互联网的大陆上:一、几款流行的JSON库效率分析
- VC多文档MFC程序的背景色不能时时刷新