使用-MM在Makefile中生成依赖项时出现问题 [英] Problem generating dependencies in Makefile using -MM

查看:159
本文介绍了使用-MM在Makefile中生成依赖项时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Makefiles和g ++的新手,在使用-MM标志生成项目文件的依赖项时遇到了问题.我正在发布我正在使用的Makefile,供您考虑.请看一下.

I am new to Makefiles and g++ and i am struck with a problem while generating dependencies of the project files using -MM flag. I'm posting the Makefile i am using for your consideration. Please take a look.

OUTPUT_ROOT := output/
SOURCE_ROOT := source/

TITLE_NAME := TestProj 

SOURCES := \
 MyFile.cpp \
 stdAfx.cpp \
 Main.cpp \

OUT_DIR  := $(OUTPUT_ROOT)

OUT_O_DIR := $(OUT_DIR)

OBJS = $(SOURCES:%.cpp=$(OUT_O_DIR)%.o)
DEPS = $(OBJS:%.o=%.d)
DIRS = $(subst /,/,$(sort $(dir $(OBJS))))
SOURCE_TARGET = $(SOURCES:%.cpp=$(SOURCE_ROOT)%.cpp)
OUTPUT_TARGET = $(OUT_DIR)$(TITLE_NAME)

#---------------------------------------------------------------------
# executables
#---------------------------------------------------------------------
MD := mkdir -p
RM := rm
CC := g++

#---------------------------------------------------------------------
# rules
#---------------------------------------------------------------------
.PHONY: clean directories objects title

all: directories objects title

directories:
 @$(MD) $(DIRS)

clean:
 $(RM) -rf $(OUT_DIR)

$(OBJS): $(SOURCE_TARGET)
 @$(CC) -c $< -o $@

$(DEPS): $(SOURCE_TARGET)
 @$(CC) -c -MM $< > $(DEPS)

-include $(DEPS)

objects:$(OBJS) $(DEPS)

title: $(OBJS)
 @$(CC) $< -o $@

我尝试了几种选择,而且反复多次.我用谷歌搜索了解决方案,但找不到任何解决方案.

I tried several options and sooo many times. I googled for the solution but couldn't find any.

使用"-MM"标志生成依赖项是正确的选择吗?如果没有,请向我建议生成依赖关系的正确方法.我想自动生成依赖项,因为我的项目将包含许多文件.我认为这比手动写下evey依赖关系更好.

Is using "-MM" flag to generate dependencies the right option?? If not please suggest me the right way to generate the dependencies. I wanted to generated dependencies automatically because my project will have sooo many files. I thought it is the better option than to write down evey dependency manually.

这些是我遇到的错误

g++: stdAfx.d: No such file or directory
g++: Main.d: No such file or directory
make: *** No rule to make target `stdAfx.d', needed by `objects'.  Stop.

谢谢.

推荐答案

您似乎正在尝试为每个.cpp文件生成一个依赖文件(根据makefile规则称为* .d).这不是我对如何使用依赖项文件的理解.

It looks like you are trying to generate a dependency file (called *.d, by your makefile rules) for each .cpp file. This is not my understanding of how a dependencies file is used.

使用-M选项为您的项目生成一个依赖文件,然后包含依赖文件.

Use the -M option to generate a single dependencies file for your project and then include the dependencies file.

DEPS = $(OUR_DIR)/make.dep

$(DEPS): $(SOURCE_TARGET)
    @$(CC) -M $(SOURCE_TARGET) > $(DEPS)

include $(DEPS)

编辑,您的依赖文件也应取决于标题

edit Your dependency file should also depend on your headers

$(DEPS): $(SOURCE_TARGET) $(HEADER_TARGET)
    @$(CC) -M $(SOURCE_TARGET) > $(DEPS)

其中HEADER_TARGET的定义与SOURCE_TARGET相同.这样,当头文件更改时,依赖关系文件就会重新构建.

where HEADER_TARGET is defined the same as SOURCE_TARGET. That way, when a header file is changed the dependency file is rebuilt.

这篇关于使用-MM在Makefile中生成依赖项时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆