GNU make manual 翻译( 一百二十三)

简介:

继续翻译

复制代码
4.14 Generating Prerequisites Automatically
===========================================

In the makefile for a program, many of the rules you need to write often
say only that some object file depends on some header file.  For
example, if `main.c' uses `defs.h' via an `#include', you would write:

     main.o: defs.h

You need this rule so that `make' knows that it must remake `main.o'
whenever `defs.h' changes.  You can see that for a large program you
would have to write dozens of such rules in your makefile.  And, you
must always be very careful to update the makefile every time you add
or remove an `#include'.  

   To avoid this hassle, most modern C compilers can write these rules
for you, by looking at the `#include' lines in the source files.
Usually this is done with the `-M' option to the compiler.  For
example, the command:

     cc -M main.c

generates the output:

     main.o : main.c defs.h

Thus you no longer have to write all those rules yourself.  The
compiler will do it for you.
复制代码

4.14 自动生成前提条件
===========================================

在为程序而作的 makefile 中,很多你需要写的规则经常说 有些文件依赖于某些头文件。

例如,如果你的 main.c 通过 #include 使用了 defs.h, 你可能会写: 

main.o: defs.h

你需要这个规则,于是 make 知道它必须重新构建 main.o ,只要defs.h 文件变化了就是如此。你会看到对一个大型程序而言你不得不写很多这样的规则。并且,每次增加或减少 #include ,你必须总是十分小心地修改 makefile。

为了避免这个烦恼,很多现代的C 编译器可以通过查找源文件中的 #include 行来为你写这些规则。通常这是通过 编译器的 -M选项来实现的。例如,如下指令:

cc -M main.c

导致如下的输出:

main.o : main.c defs.h

这样你不用再自己必须写所有的规则了。编译器将要为你作这件事情。

后文待续






本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/09/26/2704115.html,如需转载请自行联系原作者

目录
相关文章
|
Linux C语言
make: gcc:命令未找到
make: gcc:命令未找到
make: gcc:命令未找到
|
编译器 Linux 开发工具
|
9月前
|
存储 编译器 C语言
深入理解GCC 和 G++ 编译器
GCC 和 G++ 是 GNU 工具链中的核心编译器,支持 C 和 C++ 程序开发。本文详细介绍其编译流程、常用选项及动态链接与静态链接的区别。编译过程分为预处理、编译、汇编和链接四个阶段,每个阶段有特定任务和命令选项。常用选项如 `-E`、`-S`、`-c` 和 `-o` 分别用于预处理、生成汇编代码、生成目标文件和指定输出文件。动态链接节省空间且易于更新,但依赖运行时库;静态链接独立高效,但文件较大且更新困难。合理选择优化选项(如 `-O0` 至 `-O3`)可提升程序性能。掌握这些知识有助于开发者更高效地编写、调试和优化代码。
382 23
深入理解GCC 和 G++ 编译器