指定 C++ 的目录Makefile 中的目标文件和可执行文件

发布于 2024-08-03 17:47:00 字数 1439 浏览 1 评论 0原文

我正在使用 gcc -MM 生成将源文件编译为目标文件的依赖关系规则的先决条件。我想指定一个目录来保存某个变量 BIN_DIR=bin/release/ 的值中的目标文件和可执行文件。有两种情况:


情况 1

%.d: *.h *.cc Makefile
     @$(CXX) -MM $(CXXFLAGS) *.cc > $@.$$$$; \
     sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; $(RM) -rf $@.$$$$

虽然对 sed 不熟悉,但我了解到 sed 实用程序的目的是翻译(例如):

main.o:main.cc defs.h

到:

main.o main.d : main.cc defs.h

我的问题是如何将其进一步更改为: bin/release/main.o bin/release/main.d : main.cc defs.h

如果有人可以在这里解释 sed 的用法,我们将不胜感激!


CASE 2

Makefile.depend: *.h *.cc Makefile
    $(CXX) -MM $(CXXFLAGS) *.cc > Makefile.depend

在 Makefile.depend 文件中,您将看到每个规则如下例所示:

main.o:main.cc defs.h

如何将其更改为:

bin/release/main.o:main.cc defs.h

谢谢并致以问候!


注意:

我不知道为什么我现在不能发表评论。所以我把我的更新放在这里:

VPATH是指定源代码的目录。但随后您必须从您想要目标文件的位置运行 make -f path2Makefile 。在这里,我希望能够从任何地方运行 make -f path2 Makefile 。所以最好指定目标文件的目录。


致贝塔:

感谢您的帮助!请原谅我无法对您的答案发表评论。 在第二种情况下,您的方法的问题在于,在命令之后,一些具有多行的规则将在每一行接收前缀。例如

main.o : main.cc defs.h \

src/misc.h

变为:

$(BIN_DIR)main.o : main.cc defs.h \

$(BIN_DIR) src/misc.h

I am using gcc -MM to generate prerequisites of dependencies rules for compiling source files into object files. I want to specify a directory to hold object files and executable in the value of some variable BIN_DIR=bin/release/. There are two cases:


CASE 1

%.d: *.h *.cc Makefile
     @$(CXX) -MM $(CXXFLAGS) *.cc > $@.$$; \
     sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$ > $@; $(RM) -rf $@.$$

Though unfamiliar to sed, I learned that the purpose of the sed utility is to translate (for example):

main.o : main.cc defs.h

into:

main.o main.d : main.cc defs.h

My question is how to change it further to:
bin/release/main.o bin/release/main.d : main.cc defs.h

If someone could explain the usage of sed here, that would be greatly appreciated!


CASE 2

Makefile.depend: *.h *.cc Makefile
    $(CXX) -MM $(CXXFLAGS) *.cc > Makefile.depend

In the file Makefile.depend, you will see each rule is like this example:

main.o : main.cc defs.h

How to change it to be:

bin/release/main.o : main.cc defs.h

Thanks and regards!


NOTE:

I don't know why I cannot comment now. So I put my update here:

VPATH is to specify the directory of source code. But then you have to run make -f path2Makefile from where you want the object files be. Here I'd like to be able to run make -f path2 Makefile from anywhere. So it is prefered to specify directory for object files.


To Beta:

Thanks for your help! Please excuse me for not being able to comment on your answer.
The problem with your method on second case is that after the command, some rules with several lines will recieve the prefix at every single line. e.g.

main.o : main.cc defs.h \

src/misc.h

becomes:

$(BIN_DIR)main.o : main.cc defs.h \

$(BIN_DIR) src/misc.h

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

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

发布评论

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

评论(2

动听の歌 2024-08-10 17:47:00

对于情况 1,您可以使用:

sed 's,\($*\)\.o[ :]*,$(BIN_DIR)\1.o $(BIN_DIR)$@ : ,g'

正则表达式对于初学者来说非常难以阅读,但基本上这表示“将目标词干(记住是事物 1)后跟 .o,吞噬掉后面的所有空格和冒号,然后替换整个事情与BIN_DIR,记住的事情,.o,空格,BIN_DIR,规则的目标,空格和冒号“。

情况1会给你带来一些麻烦。一方面,无论你是否真的需要,你总是会重建 main.o 和 main.d。有多种方法可以解决此问题 - 最简单的方法是创建目标 $(BIN_DIR)main.d 和 $(BIN_DIR)main.o,但是您可以稍后尝试更复杂的方法。

对于情况 2,这应该可以解决问题:

sed 's,^,$(BIN_DIR),'

这里正则表达式表示“将行的开头更改为 BIN_DIR”。

编辑:第二次尝试

sed 's,\(.*:\),$(BIN_DIR)\1,'

这表示“找到以冒号结尾的内容并将 BIN_DIR 粘贴在其前面”。防弹的下一步将是“吸入多行并将 BIN_DIR 放在冒号前面的每个类似单词的前面”,但这可能没有必要,敲木头。

For case 1 you can use:

sed 's,\($*\)\.o[ :]*,$(BIN_DIR)\1.o $(BIN_DIR)$@ : ,g'

Regular expressions are notoriously hard to read for beginners, but basically this says "take the target stem (remember that as thing 1) followed by .o, gobble up any spaces and colons that follow, and replace the whole thing with BIN_DIR, the remembered thing, .o, space, BIN_DIR, the target of the rule, a space and a colon".

Case 1 will cause you some trouble. For one thing, you will always rebuild main.o and main.d whether you really need to or not. There are several ways to fix this-- the simplest is to make the targets $(BIN_DIR)main.d and $(BIN_DIR)main.o, but there are more sophisticated methods you can try later.

For case 2, this should do the trick:

sed 's,^,$(BIN_DIR),'

Here the regex says "change the beginning of the line to BIN_DIR".

EDIT: second try

sed 's,\(.*:\),$(BIN_DIR)\1,'

This says "find something that ends with a colon and stick BIN_DIR in front of it". The next step in bulletproofing would be "suck in multiple lines and put BIN_DIR in front of every word-like thing preceding a colon", but that's probably not necessary, knock wood.

诗笺 2024-08-10 17:47:00

在 gnu.org make 手册中查找“vpath”。我想这就是你所需要的。

Look up 'vpath' in the gnu.org make manual. I think it's what you need.

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