学习 GNUMake 的资源?

发布于 2024-07-07 18:51:21 字数 125 浏览 5 评论 0原文

我正在尝试为我正在进行的一个小项目学习 GNUMake。 到目前为止,即使是“基本”教程也显得相当粗糙,而且我还没有理解 makefile 语法。

有没有人有一些好的资源可以帮助绝对的初学者熟悉 GNUMake?

I'm trying to learn GNUMake for a small project I'm working on. So far, even the "basic" tutorials seem pretty rough and I've yet to make sense of the makefile syntax.

Does anyone have some good resources for an absolute beginner to get familiar with GNUMake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

瞳孔里扚悲伤 2024-07-14 18:51:21

权威指南是 http://www.gnu.org/software/make/manual /make.html
有一本 o'reilly 的书《Managing Projects with GNU Make》有更多解释。
您也可以使用早期版本,它们不专门涵盖 GnuMake,但要薄得多。

Make 在开发人员中是一个肮脏的秘密——我们没有人理解它,我们只是从别人那里借用了一个 make 脚本并改变了它。 我想只有一个脚本是从头开始编写的(可能是由该工具的创建者编写的)。

当您需要做的不仅仅是简单的示例时,大多数人要么切换到更现代的构建系统(如 Ant),要么使用 Perl/Python/等构建自己的系统。

The definitive guide is http://www.gnu.org/software/make/manual/make.html
There is an o'reilly book "Managing Projects with GNU Make" which has more explanation.
You can also uses the earlier editions, they don't cover GnuMake specifically but are a lot thinner.

Make is a dirty secret among developers - none of us understand it, we just borrow a make script from somebody else and change it. I imagine only one script was ever written from scratch (probably by the creator of the tool).

When you need to do more than the simple example most people either switch to a more modern build system like Ant or roll their own in Perl/Python/etc.

将军与妓 2024-07-14 18:51:21

make 最常用的功能可以分为几个简单的概念、目标、依赖项和变量。

目标是您想要构建的东西,但目标下的命令可以是编译器命令或脚本。 一般来说,每个目标都指代代码中的一个模块,但您可以根据您的项目需要进行细化。

依赖项是项目中的文件或其他目标。 最好的例子是 C 项目,您正在其中从一堆目标文件构建二进制文件。 在构建二进制文件之前,每个目标文件都需要存在,因此 make 将遍历您的目标,直到完成所有依赖项,然后运行整个目标的命令。

变量并不总是必需的,但对于处理诸如编译器标志之类的事情非常方便。 典型的示例是 CC 和 CCFLAG,它们将引用您使用的编译器(即 gcc)和 -ansi -Wall -o2 等标志。

一些更一般的提示和技巧:

  • 命令必须由[制表符]字符进行,否则它们将不会被执行,这只是 make 的旧遗迹,我不记得为什么这样是。
  • 按照约定,您可能需要包含一个 all 目标来指定默认目标。 当您有一个复杂的 makefile 并且有一个您始终希望成为默认目标的特定目标时,这非常有用。
  • 你的 makefile 应该被称为 makefile 或 Makefile,但如果你想将其称为其他名称,请使用 $make -f [makefilename]
  • 始终使用完整的变量扩展语法,即 $(VARIABLE),否则 make 可能不会输出你想要的命令。
  • make 可以递归工作,因此如果您的项目中有一堆子模块位于目录内,则可以从 make 中调用子目录 makefile 来构建每个子模块。
  • 如果您有一个非常复杂的项目,需要安装脚本等,您可能还想研究自动工具,它为您生成生成文件,并执行一系列技巧来检查库是否存在以及其他可移植性问题。

The most commonly used features of make can be broken down into a couple simple concepts, targets, dependencies and variables.

Targets are the things you want to build, but the command(s) beneath a target can be compiler commands or scripts. Generally each target refers to a module in your code, but you can make these as granular as you want to suit your project.

Dependencies are files or other targets in your project. The best example of this is for a C project where you're building a binary from a bunch of object files. Each object file will need to exist before you can build the binary, so make will traverse your targets until all of the dependencies have been completed, and then run the command for the overall target.

Variables aren't always necessary, but are pretty handy for handling things like compiler flags. The canonical examples are CC and CCFLAGs which will refer to the compiler your using i.e. gcc and the flags like -ansi -Wall -o2.

A couple more general tips and tricks:

  • Commands must be proceeded by a [tab] character, or they won't be executed, this is just an old relic of make, I don't recall why this is.
  • By convention, you may want to include an all target to specify the default which target should be the default. This is useful when you have a complex makefile and there's a particular target you always want to be the default.
  • Your makefile should be called makefile or Makefile, but if you want to call it something else, use $make -f [makefilename]
  • Always use the full variable expansion syntax i.e. $(VARIABLE) or make may not output the commands you want.
  • make can work recursively, so if you have a bunch of sub-modules in your project that live inside of directories, you can call make on the sub-directory makefile from within make to build each.
  • If you have a really complicated project that needs an installation scripts, etc. you'll probably also want to investigate autotools which generates the makefile for you and does a bunch of tricks to check for library existence and for other portability issues.
少年亿悲伤 2024-07-14 18:51:21

《使用 GNU Make 管理项目,第 3 版》被置于“GNU 自由文档许可证”之下,可以合法地免费在线阅读:链接

"Managing Projects with GNU Make, 3rd edition" is put under "GNU Free Documentation License" and can be legally read online for free: link.

灯角 2024-07-14 18:51:21

我同意奥莱利的书建议。

有关 Make 的一些有用的提示、技巧和见解,请查看 Mr. 制作文章

I concur with the O'Reilly book suggestion.

For some helpful tips, tricks and insights into Make look at the Mr. Make articles

So要识趣 2024-07-14 18:51:21

mgb:更糟糕的是。 我曾经从头开始编写过一个复杂的 make 系统(几千个文件、五十或一百个目录、四五个编译器和交叉编译目标、2 个操作系统等)。 我坐下来,首先从里到外学习了 gnu make,设计了系统,首先尝试了一个原型。 我们对结果都非常满意。

但那是几年前的事了。 你知道我今天怎么写吗? 和你描述的一样。 它实在是太繁琐了,语法细节也太晦涩难懂了,除非你经常这样做,否则你就记不住细节。 我想,就像任何其他具有非直观语法和一些奇怪规则的语言一样。

mgb: It's even worse that that. I once did write a complicated make system from scratch (a few thousand files, fifty or a hundred directories, four or five compilers and cross-compilation targets, 2 OS's, etc.). I sat down and learned gnu make inside and out first off, designed the system, played around with a prototype first. We were all very happy with the result.

But that was years ago. You know how I write them today? Same way you describe. It's just fiddly enough and the syntax details are obscure enough than unless you do it fairly regularly you can't remember the details. Just like any other language with non-intuitive syntax and some quirky rules, I guess.

ˇ宁静的妩媚 2024-07-14 18:51:21

我不太喜欢 GNU 信息系统,但我发现 GNU make 信息页面非常有用。 一旦您了解了非常基本的概念以及大致可用的内容,我发现信息页面是引用信息(例如预构建函数)的最快方式。

(忘记 GNU info 程序,并使用例如 pinfo 相反。)

I am not great fan of GNU info system, but I have found the GNU make info pages to be very useful. Once you know the very basic concepts and roughly what kind of things are available, I have found that info pages are the quickest way to refer information like prebuilt functions, for example.

(Forget the GNU info program, and use e.g. pinfo instead.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文