Make 中的默认规则

发布于 2024-09-05 09:33:41 字数 213 浏览 3 评论 0原文

make 中是否有一种机制允许在任何地方使用默认的全局隐式规则,类似于内置规则?

Make 提供了一些用于编译 C/C++/Fortran 文件的内置隐式规则,对于简单情况甚至不需要 Makefile。但是,在编译其他语言(例如Go编程语言文件)时,始终需要Makefile。我想扩展我的 Makeenvironment 以默认使用隐式规则。

Is there a mechanism in make to allow for default global implicit rules that are available anywhere, similar to the built-in rules?

Make provides some built-inimplicit rules for compiling C/C++/Fortran files, without even requiring a Makefile for simple cases. However, when compiling other languages (e.g. Go programming language files), a Makefile is always required. I would like to extend my Makeenvironment to have implicit rules available by default.

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

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

发布评论

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

评论(3

南风几经秋 2024-09-12 09:33:41

这通常是不可取的,因为它会导致 Makefile 的可移植性较差;如果别人没有这样设置,它就无法在别人的机器上运行。

但是,如果您想这样做,请使用 Go 文件的默认规则在某处创建一个“全局”Makefile,然后将其路径添加到 MAKEFILES 环境变量中。当您运行“make”时,此全局 Makefile 将在任何 Makefile 之前处理,就像您已将其源代码包含在文件顶部一样。

This is not normally desirable, as it would cause your Makefile to be less portable; it wouldn't work on somebody else's machine if they didn't have it set up that way.

However, if you want to do this, create a "global" Makefile somewhere with your default rules for Go files, then add its path to the MAKEFILES environment variable. This global Makefile will be processed before any Makefile when you run "make", just as if you had included its source at the top of the file.

云醉月微眠 2024-09-12 09:33:41

我假设您指的是这样一个事实,即您可以执行

make hello.o

并且 make 会自动知道如何从 .c 文件(或者实际上从.f.p,如果存在) - 但您想对自定义文件类型执行此操作(例如,从构建 .bar a .foo

执行此操作的最便携方法如下(在您的 Makefile 中):

.SUFFIXES: .foo .bar
.foo.bar:
        foo2bar -in 
gt; -out $@

第一行 (.SUFFIXES) 警告 make 您将处理这些作为特殊后缀;第二行表示“这是从 .foo 制作 .bar 的方法。第三行给出了执行此操作的命令 - $ >$@ 通过 make 更改为输入和输出文件名

注意:第三行的缩进必须是制表符

一种更灵活的方法,仅适用。对于 GNU make,就是利用其对隐式规则。如果你能保证你会使用 GNU make 那么这可能是值得推荐的。

I'm assuming you're referring to the fact that you can do

make hello.o

and make will automatically know how to make the .o from a .c file (or indeed from a .f or .p, if one exists) - but you want to do this for custom file types (say, building a .bar from a .foo.

The most portable way of doing this is as follows (in your Makefile):

.SUFFIXES: .foo .bar
.foo.bar:
        foo2bar -in 
gt; -out $@

The first line (.SUFFIXES) warns make that you'll be treating these as special suffixes; the second line says "here's a recipe for making a .bar from a .foo. The third line gives the command for doing this - $> and $@ get changed by make to the input and output filenames.

NOTE: The indent for the third line MUST be a tab character.

A much more flexible method, that only works with GNU make, is to use its support for implicit rules. If you can guarantee you'll be using GNU make then this is probably to be recommended.

时光清浅 2024-09-12 09:33:41

虽然我同意 dmazzoni 的观点,但我只是想添加 Go Makefile 的制作配方:(

# Include default Golang Make magic
include $(GOROOT)/src/Make.$(GOARCH)

# Hack the following line    
your_program: your_program.$O
    $(LD) -o $@ $^

# Compiles .go-files into architecture-specific binaries
%.$O: %.go
    $(GC) -o $@ $^

clean:
    rm your_program *.$O

注意:$O 是 DOLLAR + 大写-o - 不是零!)

虽然我还没有在我拥有的所有机器上测试它,但我相信它应该可以很好地移植。

While I agree with dmazzoni, I just though I'd add my make recipe for a Go Makefile:

# Include default Golang Make magic
include $(GOROOT)/src/Make.$(GOARCH)

# Hack the following line    
your_program: your_program.$O
    $(LD) -o $@ $^

# Compiles .go-files into architecture-specific binaries
%.$O: %.go
    $(GC) -o $@ $^

clean:
    rm your_program *.$O

(Note: the $O is DOLLAR + UPPERCASE-o - not zero!)

While I haven't tested it on all the machines I have available, i believe it should port fairly well.

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