【发布时间】:2015-04-01 18:02:49
【问题描述】:
我有一个目录结构如下的项目:
Root directory
bin/ (for final executable)
Mod1/
build/ (for *.o / *.d files)
inc/
src/
Mod2/
build/
inc/
src/
Mod.../ (future module directories)
build/
inc/
src/
makefile
我当前的 makefile 包含以下内容:
# Directory naming conventions
SRC_DIR_NAME = src
BUILD_DIR_NAME = build
# Parallel source/object/dependency file lists
SRCS = $(shell find -name *.cpp)
OBJS = $(subst $(SRC_DIR_NAME),$(BUILD_DIR_NAME),$(SRCS:%.cpp=%.o))
# Final executable
APP = bin/app
build: $(APP)
$(APP): $(OBJS)
# link objects and compile app
%.o: %.cpp
# compile sources into objects
我的文件列表按预期工作并产生:
SRCS=./mod1/src/file.cpp ./mod2/src/file.cpp
OBJS=./mod1/build/file.o ./mod2/build/file.o
DEPS=./mod1/build/file.d ./mod2/build/file.d
但是当我运行 make 时出现以下错误:
make: *** No rule to make target `mod1/build/file.o', needed by `/bin/app'. Stop.
我的假设是:
%.o: %.cpp
不适用于类似的输入
mod1/build/file.o
有没有办法制作一个通用目标,该目标接受一个模块目录并为该模块的目标文件创建目标,这些目标文件需要该模块的源文件在其各自的子目录中?
我知道使用递归 make 可以解决这个问题,但我想尽可能避免使用该解决方案。
【问题讨论】:
-
这就是static pattern rules 的用途。好吧,它们至少对一件事有用。
-
@EtanReisner 我仍然没有取得任何进展。是否有将多个 % 通配符传递给目标? (例如 %/build/%.o: %/src/%.cpp)
标签: c++ linux makefile gnu-make