如何在不同的目录中指定 makefile 目标?
我试图弄清楚为什么我无法将目标指定为位于不同的目录中。
我正在尝试从 json 文件生成一些 php 类,但我已将实际生成 php 的行更改为虚拟 echo 语句,因此其他人可以复制粘贴并测试他们是否感觉非常慷慨。
还有另外两件事需要添加到包含 makefile 的目录中:
- 一个名为 PatientDbPath.json 的虚拟依赖文件
- 一个名为 PatientDbPath.json 的目录
如果我有一个包含以下内容的 makefile:
.SUFFIXES: .php .json
.json.php:
echo "HelloMe" > PatientDbPath.php
PatientDbPath.php: PatientDbPath.json
clean:
$(RM) PatientDbPath.php
那么当我运行 make 时一切正常; PatientDbPath.php 已正确创建,下次运行 make 时,我收到消息 make: 'PatientDbPath.php' is up to date.
但是,我想在单独的目录中生成 php 文件,所以我将我的 makefile 更新为以下内容:
.SUFFIXES: .php .json
.json.php:
echo "HelloMe" > out/PatientDbPath.php
out/PatientDbPath.php: PatientDbPath.json
clean:
$(RM) out/PatientDbPath.php
一旦我这样做,Make 就会告诉我 make: Nothing to be do for 'out/PatientDbPath.php'
即使没有文件 PatientDbPath.php在out目录中。
所以我想这可能是后缀规则的问题,于是我创建了第三个 makefile。
out/PatientDbPath.php: PatientDbPath.json
echo "Whatever" > out/PatientDbPath.php
clean:
rm out/PatientDbPath.php
这个效果很好,就像第一个一样。有人能看到我在第二个 makefile 中做错了什么吗?
谢谢。
I am trying to figure out why I can't specify a target as being in different directory.
I am trying to generate some php classes from json files, but I've changed the line that actually generates the php to a dummy echo statement, so somebody else can just copy paste and test if they feel very generous.
There are two other things to add to the directory containing the makefile:
- A dummy dependency file called PatientDbPath.json
- A directory called out
If I have a makefile with the following:
.SUFFIXES: .php .json
.json.php:
echo "HelloMe" > PatientDbPath.php
PatientDbPath.php: PatientDbPath.json
clean:
$(RM) PatientDbPath.php
Then everything works when I run make; PatientDbPath.php is correctly created and the next time I run make, I get the message make: 'PatientDbPath.php' is up to date.
However, I want to generate the php file in a separate directory, so I updated my makefile to the following:
.SUFFIXES: .php .json
.json.php:
echo "HelloMe" > out/PatientDbPath.php
out/PatientDbPath.php: PatientDbPath.json
clean:
$(RM) out/PatientDbPath.php
As soon as I do that, Make tells me make: Nothing to be done for 'out/PatientDbPath.php'
even though there is no file PatientDbPath.php in the out directory.
So I thought maybe it was something with the suffix rules and I created a third makefile.
out/PatientDbPath.php: PatientDbPath.json
echo "Whatever" > out/PatientDbPath.php
clean:
rm out/PatientDbPath.php
This one works well, like the first one. Can anybody see what I'm doing wrong in my 2nd makefile?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还有另一种方法可以做同样的事情,也许更优雅:
这将为
JSON_FILES
中列出的每个文件创建一个规则,因此如果定义了JSON_FILES= foo.json bar.json,那么这相当于:
如果您确实想要所有 .json 文件,则可以使用
$(wildcard *.json)
设置该值,或者如果规则是手动设置只有少数人独有。There's another way to do the same thing, perhaps more elegantly:
This will create a rule for every file listed in
JSON_FILES
, so if it were definedJSON_FILES= foo.json bar.json
, then this would be equivalent to:You could then use
$(wildcard *.json)
to set that value if you really wanted all the .json files, or set it manually if the rule is unique to just a few.您的通配符规则 (.json -> .php) 中有两个错误,一是概念性的,一是实际性的。
概念上的错误是,这是如何将任意 json 文件(例如,foo.json)转换为任意但名称相同的 .php 文件(例如,json.php)的规则。然而,您的规则是特定于生成 PatientDbPth.php 的。更好的规则是
$@
是命名输出文件的变量。这也说明了实际问题:这些规则确实仅限于同名文件。如果您有一条在 .php 中翻译 .json 的规则,并且您有 foo.php,并且您想要 bar.php,则通配符规则将不匹配。匹配涉及整个文件名,因此当您请求 out/PatientDbPath.php 时,通配符规则不匹配。这给你留下了没有任何动作的规则,因此 make 推断什么都不要做。
如果这是您想要转换的唯一文件,最好按照第三个文件中的方式编写。如果你有很多 json 文件,并且都想将它们转换为 out/*.php,并且你使用的是 GNU make,则可以使用 GNU make 的通用通配符规则:
There are two errors in your wildcard rule (.json -> .php), one conceptual and one practical.
The conceptual error is that this is a rule how to convert arbitrary json files (say, foo.json) to arbitrary-but-same-named .php files (say, json.php). Yet, your rule is specific for gnerating PatientDbPth.php. A better rule would be
where
$@
is a variable naming the output file.This also shows the practical problem: These rules are really restricted to same-named files. If you have a rule translating .json in .php, and you have foo.php, and you want bar.php, then the wildcard rule won't match. Matching involves the entire file name, so the wildcard rule doesn't match when you ask for out/PatientDbPath.php. This leaves you with the rule that has no action, hence make infers that nothing is to be done.
If this is the only file you want to convert, it's best to write it the way you did in the third file. If you have many json files, and you all want to convert them to out/*.php, and you are using GNU make, you can use GNU make's generalized wildcard rules: