如何使 Makefile 更短?

发布于 2024-09-10 16:10:51 字数 343 浏览 3 评论 0原文

我有以下Makefile:

all: hello.exe hellogtk.exe hellogtktng.cs

hello.exe: hello.cs
 gmcs hello.cs

hellogtk.exe: hellogtk.cs
 gmcs -pkg:gtk-sharp-2.0 hellogtk.cs

hellogtktng.exe: hellogtktng.cs
 gmcs -pkg:gtk-sharp-2.0 hellogtktng.cs

clean:
 rm -f *.exe

我才开始学习如何编写Makefile,我觉得这一切有点重复。 Makefile 专家会如何做这件事呢?

I've got the following Makefile:

all: hello.exe hellogtk.exe hellogtktng.cs

hello.exe: hello.cs
 gmcs hello.cs

hellogtk.exe: hellogtk.cs
 gmcs -pkg:gtk-sharp-2.0 hellogtk.cs

hellogtktng.exe: hellogtktng.cs
 gmcs -pkg:gtk-sharp-2.0 hellogtktng.cs

clean:
 rm -f *.exe

I'm only starting to learn how to write Makefiles, and I feel that all this is a bit repetitive. How would Makefile pros go about doing this?

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

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

发布评论

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

评论(2

皇甫轩 2024-09-17 16:10:51
all: hello.exe hellogtk.exe hellogtktng.exe

%.exe: %.cs
 gmcs -pkg:gtk-sharp-2.0 
lt;

clean:
 rm -f *.exe
all: hello.exe hellogtk.exe hellogtktng.exe

%.exe: %.cs
 gmcs -pkg:gtk-sharp-2.0 
lt;

clean:
 rm -f *.exe
水水月牙 2024-09-17 16:10:51

以下是向特定目标添加标志的方法。

# An empty variable for flags (Not strictly neccessary, 
# undefined variables expand to an empty string)
GMCSFLAGS =

# The first target is made if you don't specify arguments
all: hello.exe hellogtk.exe hellogtktng.exe

# Add flags to specific files
hellogtk.exe hellogtktng.exe: GCMSFLAGS = -pkg:gtk-sharp-2.0

# A pattern rule to transform .cs to .exe
# The percent sign is substituted when looking for dependancies
%.exe:%.cs
    gmcs $(GMCSFLAGS) 
lt;
# $() expands a variable, 
lt; is the first dependancy in the list

Here's how you can add flags to specific targets.

# An empty variable for flags (Not strictly neccessary, 
# undefined variables expand to an empty string)
GMCSFLAGS =

# The first target is made if you don't specify arguments
all: hello.exe hellogtk.exe hellogtktng.exe

# Add flags to specific files
hellogtk.exe hellogtktng.exe: GCMSFLAGS = -pkg:gtk-sharp-2.0

# A pattern rule to transform .cs to .exe
# The percent sign is substituted when looking for dependancies
%.exe:%.cs
    gmcs $(GMCSFLAGS) 
lt;
# $() expands a variable, 
lt; is the first dependancy in the list
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文