C编程:单模块、多模块程序及文件依赖管理
立即解锁
发布时间: 2025-08-22 01:11:57 阅读量: 6 订阅数: 18 


UNIX编程与系统管理实战指南
### C 编程:单模块、多模块程序及文件依赖管理
#### 1. 单模块程序
在 C 语言编程中,单模块程序是基础。以下是一个简单的单模块程序示例,用于反转字符串:
```c
/* Function Prototype */
reverse ();
main ()
{
char str [100]; /* Buffer to hold reversed string */
reverse ("cat", str); /* Reverse the string "cat" */
printf ("reverse (\"cat\") = %s\n", str); /* Display */
reverse ("noon", str); /* Reverse the string "noon" */
printf ("reverse (\"noon\") = %s\n", str); /* Display */
}
reverse (before, after)
char *before; /* A pointer to the source string */
char *after; /* A pointer to the reversed string */
{
int i;
int j;
int len;
len = strlen (before);
for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */
after[i] = before[j];
after[len] = NULL; /* NULL terminate reversed string */
}
```
运行这个 C 程序,首先需要编译它。默认情况下,编译后的可执行文件名为 “a.out”:
```bash
$ cc reverse.c
...compile source.
$ ls -l reverse.c a.out
...list file information.
-rwxr-xr-x 1 glass 24576 Jan 5 16:16 a.out*
-rw-r--r-- 1 glass 439 Jan 5 16:15 reverse.c
$ a.out
...run program.
reverse ("cat") = tac
reverse ("noon") = noon
```
不过,“a.out” 这个默认名称比较晦涩,后续编译生成的 “a.out” 文件还会覆盖之前的文件。为避免这些问题,可以使用 `cc` 的 `-o` 选项指定可执行文件的名称:
```bash
$ cc reverse.c -o reverse
...call the executable "reverse".
$ ls -l reverse
-rwxr-xr-x 1 glass 24576 Jan 5 16:19 reverse*
$ reverse
...run the executable "reverse".
reverse ("cat") = tac
reverse ("noon") = noon
```
#### 2. 多模块程序
单模块程序中的函数复用性较差。例如,若要编写一个判断字符串是否为回文的函数,可以使用反转函数来实现。但直接复制粘贴反转函数存在以下问题:
- 复制粘贴操作速度慢。
- 若有更好的反转代码,需替换所有旧版本,维护困难。
- 每个复制的反转函数会占用磁盘空间。
更好的方法是将反转函数单独编译成一个模块,然后链接到需要使用它的程序中,这种函数被称为可复用函数。
##### 2.1 准备可复用函数
要准备一个可复用函数,需要创建一个包含函数源代码的模块,以及一个包含函数原型的头文件。然后使用 `cc` 的 `-c` 选项将源代码模块编译成目标模块。
以下是新的 “reverse.c” 和 “reverse.h” 文件示例:
**reverse.h**
```c
/* REVERSE.H */
reverse (); /* Declare but do not define this function */
```
**reverse.c**
```c
/* REVERSE.C */
#include <stdio.h>
#include "reverse.h"
reverse (before, after)
char *before; /* A pointer to the original string */
char *after; /* A pointer to the reversed string */
{
int i;
int j;
int len;
len = strlen (before);
for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */
after[i] = before[j];
after[len] = NULL; /* NULL terminate reversed string */
}
```
以下是使用 `reverse()` 函数的主程序 “main1.c”:
```c
/* MAIN1.C */
#include <stdio.h>
#include "reverse.h" /* Contains the prototype of reverse () */
main ()
{
char str [100];
reverse ("cat", str); /* Invoke external function */
printf ("reverse (\"cat\") = %s\n", str);
reverse ("noon", str); /* Invoke external function */
printf ("reverse (\"noon\") = %s\n", str);
}
```
##### 2.2 分别编译和链接模块
使用 `cc` 的 `-c` 选项分别编译每个源代码文件,会为每个源代码文件创建一个单独的目标模块,后缀为 “.o”。
```bash
$ cc -c reverse.c
...compile reverse.c to reverse.o.
$ cc -c main1.c
...compile main1.c to main1.o.
$ ls -l reverse.o main1.o
-rw-r--r-- 1 glass 311 Jan 5 18:24 main1.o
-rw-r--r-- 1 glass 181 Jan 5 18:08 reverse.o
```
也可以在一行中列出所有源代码文件进行编译:
```bash
$ cc -c reverse.c main1.c
...compile each .c file to .o file.
```
将所有目标模块链接成一个名为 “main1” 的可执行文件:
```bash
$ cc reverse.o main1.o -o main1
...link object modules.
$ ls -l main1
...examine the executable.
-rwxr-xr-x 1 glass 24576 Jan 5 18:25
```
0
0
复制全文
相关推荐










