我有以下项目目录结构:
I have the following project directory structure:
Prog
/include
/include/dir1
/include/dir2
/src
/src/dir1
/src/dir2
App1 依赖于 mod1 和 mod2,而 App2 只依赖于 mod1.
App1 depends on mod1 and mod2 where as App2 depends only on mod1.
使用下面表示的 makefile,模块和应用程序都可以正确构建 - 但是,如果我对 mod2 进行更改,然后执行全部构建",即使 App2 没有依赖项,它也会重新构建.
With the makefile denoted below the modules and apps all build correctly - however if I make a change to mod2 and then do a 'make all', App2 gets rebuilt even though it doesn't have a dependency.
这样做的原因是,传递到目标构建配方的 OBJ 是所有 OBJ,而不仅仅是当前目标需要的特定 OBJ.
The reason for this is because OBJ being passed into the target build recipe is all of the OBJs and not just the specific ones that the current target needs.
我想知道可以对 makefile 进行哪些更改以仅将依赖对象传递给正在构建的当前目标.
I was wondering what change to the makefile can be made to only pass the dependent objects to the current target that's being built.
生成文件:
CC := g++
LD := g++
TARGETS := app1 app2
MODULES := mod1 mod2
INC_DIR := $(addprefix include/,$(MODULES))
SRC_DIR := $(addprefix src/,$(MODULES))
BUILD_DIR := $(addprefix build/,$(MODULES))
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cpp))
OBJ := $(patsubst src/%.cpp,build/%.o,$(SRC))
INCLUDES := -Iinclude $(addprefix -I,$(INC_DIR))
vpath %.cpp $(SRC_DIR)
define make-goal
$1/%.o: %.cpp
$(CC) $(INCLUDES) -c $$< -o $$@
endef
.PHONY: all checkdirs clean
all: checkdirs $(TARGETS)
$(TARGETS) : %: $(OBJ)
$(CC) $(INCLUDES) -o build/$@ src/$@.cpp $^
checkdirs: $(BUILD_DIR)
$(BUILD_DIR):
@mkdir -p $@
makefile 已从以下答案中重新调整用途:https://stackoverflow.com/a/2484343
The makefile has been repurposed from the following answer: https://stackoverflow.com/a/2484343
在虚假的全部"目标中列出了三个目标.
there are three targets listed in the phony 'all' target.
每一个都应该有一个单独的规则列出,而不是将两个目标app1"和app2"混为一谈.
Each of those should have a separate rule listed, rather than lumping two of the targets 'app1' and 'app2' together.
注意:任何常见的文件更改/编译都会导致两个目标都被重新创建.
Note: any common file changes/compiles will result in both targets being re-created.
将SRC"分解为 SRC_APP1 SRC_APP2,然后在 app1 和 app2 目标中使用适当的 SRC_APPx 宏.
break the 'SRC' into SRC_APP1 SRC_APP2 then use the appropriate SRC_APPx macro in each of the app1 and app2 targets.
对 'OBJ' 宏和 'INCLUDES' 宏使用类似的分隔
use similar separation for the 'OBJ' macro and the 'INCLUDES' macro
不要使用foreach"规则,因为它要求构建所有目标
Do not use the 'foreach' rules as that is asking for all the targets to be built
还有其他几个细节需要解决,但以上应该为您指明了正确的方向.
there are several other details that need addressing, but the above should point you in the right direction.
应该有很大帮助的一件事.
One thing that should greatly help.
将 app1 和 app2 的目标文件放在不同的控制器中,并让 makefile 在每个 appx 的适当目录中查找
Have the object files for app1 and app2 placed in separate directors and have the makefile look in the appropriate directory for each appx
这篇关于制作配方以防止重建非依赖目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!