英文:
Default rules in Make
问题
在make中是否有一种机制可以允许默认的全局隐式规则在任何地方都可用,类似于内置规则?
Make提供了一些内置的隐式规则用于编译C/C++/Fortran文件,甚至在简单情况下不需要Makefile
。然而,当编译其他语言(例如Go编程语言文件)时,总是需要一个Makefile
。我希望扩展我的Make环境,使默认的隐式规则可以默认可用。
英文:
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.
答案1
得分: 7
这通常是不可取的,因为这样会使您的Makefile不太可移植;如果其他人的机器没有按照这种方式设置,它将无法工作。
但是,如果您想这样做,可以在某个地方创建一个“全局”Makefile,其中包含Go文件的默认规则,然后将其路径添加到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.
答案2
得分: 5
我假设你指的是你可以执行
make hello.o
并且make会自动知道如何从.c
文件(或者.f
或.p
文件,如果存在的话)生成.o
文件 - 但是你想为自定义文件类型做到这一点(比如从.foo
文件构建.bar
文件)。
最可移植的方法如下(在你的Makefile中):
.SUFFIXES: .foo .bar
.foo.bar:
foo2bar -in $> -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 $> -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.
答案3
得分: 4
虽然我同意dmazzoni的观点,但我想补充一下我的Go Makefile
的制作方法:
# 包含默认的Golang Make魔法
include $(GOROOT)/src/Make.$(GOARCH)
# 修改以下行
your_program: your_program.$O
$(LD) -o $@ $^
# 将.go文件编译为特定架构的二进制文件
%.$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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论