从茎中的文件名生成 makefile 目标依赖项

发布于 2024-10-18 06:13:58 字数 557 浏览 1 评论 0原文

您好,我正在创建一个 makefile,其中每个 .o 表示为另一个目录的相对路径,并且依赖于本地目录中的 .cpp 文件。我对问题的理解是,我不能在规则定义中使用函数,因此规则:

%.o: %.cpp

会产生先决条件 .cpp,该 .cpp 与 .o 位于同一目录中,而不是 cpp 所在的目录实际上位于。例如:

../../Tmp/MyClass.o: ../../Tmp/MyClass.cpp <--- 错误,%.o 的结果: %.cpp

../../Tmp/ MyClass.o: MyClass.cpp <--- 对,我如何以自动方式执行此操作?

最后,位于另一个目录中的输出依赖于 .o,因此它们必须从一开始就具有完整的相对路径信息:

OBJS := $(addprefix ../../../Tmp/XCode/ ${PLATFORM}/${CONFIGURATION}/, $(addsuffix .o, $(basename ${SRCS})))

${OUTPUT}: ${OBJS} ; ${AR} $@ ${OBJS}

谢谢!

Hi I have a makefile I am creating where each .o is represented as a relative path to another directory and has a dependency on a .cpp file in the local directory. My understanding of the problem is that I can't use functions in a rule definition so the rule:

%.o: %.cpp

results in a prerequisite .cpp that is in the same directory as the .o which is not where the cpp is actually located. For example:

../../Tmp/MyClass.o: ../../Tmp/MyClass.cpp <--- WRONG, result of %.o: %.cpp

../../Tmp/MyClass.o: MyClass.cpp <--- RIGHT, how do I do this in an automatic way?

Lastly the output, which is in yet another directory, has a dependency on the .o's so they must all have full relative path information from the beginning:

OBJS := $(addprefix ../../../Tmp/XCode/${PLATFORM}/${CONFIGURATION}/, $(addsuffix .o, $(basename ${SRCS})))

${OUTPUT}: ${OBJS} ; ${AR} $@ ${OBJS}

Thanks!

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

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

发布评论

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

评论(2

猥︴琐丶欲为 2024-10-25 06:13:58

我认为克里斯蒂的解决方案会起作用,但这是做同样事情的另一种方法:

# Here's how you do it:
OBJS := $(addprefix ../../../Tmp/XCode/${PLATFORM}/${CONFIGURATION}/, $(addsuffix .o, $(basename ${SRCS})))

# Here's a slightly cleaner way:
BASEPATH = ../../../Tmp/XCode/$(PLATFORM)/$(CONFIGURATION)
OBJS := $(patsubst %.cc,$(BASEPATH)/%.o,$(SRCS))

# And here's the rule:
$(OBJS): $(BASEPATH)/%.o: %.cc
    whatever...

I think kristi's solution will work, but here's another way to do the same thing:

# Here's how you do it:
OBJS := $(addprefix ../../../Tmp/XCode/${PLATFORM}/${CONFIGURATION}/, $(addsuffix .o, $(basename ${SRCS})))

# Here's a slightly cleaner way:
BASEPATH = ../../../Tmp/XCode/$(PLATFORM)/$(CONFIGURATION)
OBJS := $(patsubst %.cc,$(BASEPATH)/%.o,$(SRCS))

# And here's the rule:
$(OBJS): $(BASEPATH)/%.o: %.cc
    whatever...
(り薆情海 2024-10-25 06:13:58

这应该有效

../../Tmp/%.o: %.cpp

或者使用变量

builddir := ../../Tmp
$(builddir)/%.o: %.cpp

This should work

../../Tmp/%.o: %.cpp

Or use a variable

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