处理丢失/删除的源文件的 Makefile 选项/规则

发布于 2024-09-01 18:16:58 字数 1183 浏览 3 评论 0原文

需要一个可以处理丢失文件的 makefile 依赖规则 给出了一些关于如何处理已删除的源文件以生成 .o 文件的指示。我正在使用 gcc/g++,因此在生成依赖项时添加 -MP 选项对我来说非常有用,直到我使用 .a 文件进入链接阶段...

当输入源消失时更新档案/库怎么样?这对我来说没问题,但是有没有更干净的方法(即像 g++ -MP 选项一样简单的方法)?

#BUILD_DIR is my target directory (includes Debug/Release and target arch)
#SRC_OUTS are my .o files    

LIBATLS_HAS = $(shell nm ${BUILD_DIR}/libatls.a | grep ${BUILD_DIR} | sed -e 's/.*(//' -e 's/).*://')
LIBATLS_REMOVE = $(filter-out $(notdir ${SRC_OUTS}), ${LIBATLS_HAS})

${BUILD_DIR}/libatls.a: ${BUILD_DIR}/libatls.a(${SRC_OUTS})
ifneq ($(strip ${LIBATLS_REMOVE}),)
    $(AR) -d $@ ${LIBATLS_REMOVE}
endif

初步反馈后更新以下内容:

LIBATLS_HAS := $(shell $(AR) t ${BUILD_DIR}/libatls.a)
LIBATLS_REMOVE := $(filter-out $(notdir ${SRC_OUTS}), $(filter %.o,${LIBATLS_HAS}))

.PHONY: clean_archive
clean_archive:
    $(AR) -d $(BUILD_DIR)/libatls.a $(LIBATLS_REMOVE)

.PHONY: $(LIBATLS_REMOVE)
$(LIBATLS_REMOVE): clean_archive

${BUILD_DIR}/libatls.a: % : %(${SRC_OUTS}) ${LIBATLS_REMOVE}

Need a makefile dependency rule that can handle missing files gives some pointers on how to handle removed source files for generating .o files. I'm using gcc/g++, so adding the -MP option when generating dependencies works great for me, until I get to the link stage with my .a file...

What about updating archives/libraries when input sources go away? This works OK for me, but is there a cleaner way (ie, something as straightforward as the g++ -MP option)?

#BUILD_DIR is my target directory (includes Debug/Release and target arch)
#SRC_OUTS are my .o files    

LIBATLS_HAS = $(shell nm ${BUILD_DIR}/libatls.a | grep ${BUILD_DIR} | sed -e 's/.*(//' -e 's/).*://')
LIBATLS_REMOVE = $(filter-out $(notdir ${SRC_OUTS}), ${LIBATLS_HAS})

${BUILD_DIR}/libatls.a: ${BUILD_DIR}/libatls.a(${SRC_OUTS})
ifneq ($(strip ${LIBATLS_REMOVE}),)
    $(AR) -d $@ ${LIBATLS_REMOVE}
endif

Updated to the following after initial feedback:

LIBATLS_HAS := $(shell $(AR) t ${BUILD_DIR}/libatls.a)
LIBATLS_REMOVE := $(filter-out $(notdir ${SRC_OUTS}), $(filter %.o,${LIBATLS_HAS}))

.PHONY: clean_archive
clean_archive:
    $(AR) -d $(BUILD_DIR)/libatls.a $(LIBATLS_REMOVE)

.PHONY: $(LIBATLS_REMOVE)
$(LIBATLS_REMOVE): clean_archive

${BUILD_DIR}/libatls.a: % : %(${SRC_OUTS}) ${LIBATLS_REMOVE}

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

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

发布评论

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

评论(1

不如归去 2024-09-08 18:16:58

有几种不同的方法可以做到这一点。一个非常干净的方法是:

LIBATLS_HAS:=$(shell $(AR) t $(BUILD_DIR)/libatls.a)
LIBATLS_REMOVE:= $(filter-out $(SRC_OUTS),$(LIBATLS_HAS))
REMOVE_LIST = $(addprefix remove_,$(LIBATLS_REMOVE))

.PHONY: $(REMOVE_LIST)
$(REMOVE_LIST): remove_%
    $(AR) -d $(BUILD_DIR)/libatls.a $*

$(BUILD_DIR)/libatls.a: % : %($(SRC_OUTS)) $(REMOVE_LIST)

这有点低效,因为它为每个要删除的成员运行单独的 $(AR) 命令。我怀疑这会是一个问题,但如果是的话,你可以用一个虚假的目标来绕过它:

# Note that this will run once at most.
.PHONY: clean_archive
clean_archive:
    $(AR) -d $(BUILD_DIR)/libatls.a $(LIBATLS_REMOVE)

.PHONY: $(REMOVE_LIST)
$(REMOVE_LIST): clean_archive

There are a few different ways to do this. One that's pretty clean is:

LIBATLS_HAS:=$(shell $(AR) t $(BUILD_DIR)/libatls.a)
LIBATLS_REMOVE:= $(filter-out $(SRC_OUTS),$(LIBATLS_HAS))
REMOVE_LIST = $(addprefix remove_,$(LIBATLS_REMOVE))

.PHONY: $(REMOVE_LIST)
$(REMOVE_LIST): remove_%
    $(AR) -d $(BUILD_DIR)/libatls.a $*

$(BUILD_DIR)/libatls.a: % : %($(SRC_OUTS)) $(REMOVE_LIST)

This is a little inefficient in that it runs a seperate $(AR) command for each member to be removed. I doubt that'll be a problem, but if it is you can get around it with a phony target:

# Note that this will run once at most.
.PHONY: clean_archive
clean_archive:
    $(AR) -d $(BUILD_DIR)/libatls.a $(LIBATLS_REMOVE)

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