处理丢失/删除的源文件的 Makefile 选项/规则
当输入源消失时更新档案/库怎么样?这对我来说没问题,但是有没有更干净的方法(即像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种不同的方法可以做到这一点。一个非常干净的方法是:
这有点低效,因为它为每个要删除的成员运行单独的
$(AR)
命令。我怀疑这会是一个问题,但如果是的话,你可以用一个虚假的目标来绕过它:There are a few different ways to do this. One that's pretty clean is:
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: