从位于多个文件夹中的源文件生成目标文件

Generate object files from source files located in multiple folders

我想生成依赖于目标文件 (OBJS = $(wildcard $(BUILD_DIR)/*.o) 的可执行文件 (TARGET = $(BUILD_DIR)/MySDLProgram),目标文件依赖于位于 src/application 和 [=15] 中的源文件=].我应该如何进行?

我不断收到此错误:

Note that Makefile not found the object files

这是我的 Makefile:

#CC specifies which compiler we're using
CC = g++

# Directories
SRC_DIR = src
BUILD_DIR = build

TARGET = $(BUILD_DIR)/MySDLProgram

# Source files
SRC_DIRS = src/engine src/application
SRCS = $(foreach dir, $(SRC_DIRS), $(wildcard $(dir)/*.cpp))

OBJS = $(wildcard $(BUILD_DIR)/*.o)

# INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -Iheader

# COMPILER_FLAGS specifies the additional compilation options we're using
#   -w suppresses all warnings
#   -Wl,-subsystem,windows gets rid of the console window
# COMPILER_FLAGS = -w -Wl,-subsystem,windows
COMPILER_FLAGS = -g -O0 -Wall -std=c++11

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lSDL2

.PHONY: all

all: $(TARGET)

$(TARGET): $(OBJS)
    $(CC) $(INCLUDE_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) $^ -o $@

$(BUILD_DIR)/%.o: $(foreach dir, $(SRC_DIRS), $(dir))/%.cpp | $(BUILD_DIR)
    $(CC) $(INCLUDE_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) $< -o $@

$(BUILD_DIR):
    mkdir -p $@

exec: $(BIN)
    $(BIN).exe

clean:
    @$(RM) -rv $(BUILD_DIR)/*

这些是我想要的命令 运行:

$ g++ -Iheader -g -O0 -Wall -std=c++11 -lmingw32 -lSDL2main -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lSDL2 src/engine/Game.cpp -c -o build/Game.o

$ g++ -Iheader -g -O0 -Wall -std=c++11 -lmingw32 -lSDL2main -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lSDL2 src/engine/Music.cpp -c -o build/Music.o

$ g++ -Iheader -g -O0 -Wall -std=c++11 -lmingw32 -lSDL2main -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lSDL2 src/engine/State.cpp -c -o build/State.o

$ g++ -Iheader -g -O0 -Wall -std=c++11 -lmingw32 -lSDL2main -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lSDL2 src/engine/Sprite.cpp -c -o build/Sprite.o

$ g++ -Iheader -g -O0 -Wall -std=c++11 -lmingw32 -lSDL2main -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lSDL2 src/application/Main.cpp -c -o build/Main.o

$ g++ -g -O0 -Wall -std=c++11 -lmingw32 -lSDL2main -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lSDL2 build/Main.o build/Game.o build/Music.o build/Sprite.o build/State.o

我正在使用 MSYS2 环境。

这行不通:

$(BUILD_DIR)/%.o: $(foreach dir, $(SRC_DIRS), $(dir))/%.cpp | $(BUILD_DIR)

变量解析后展开成什么?你得到这个:

build/%.o: src/engine src/application/%.cpp | build
                    ^^

这显然不是您想要的。即使您修改了定义以执行您可能期望的操作:

$(BUILD_DIR)/%.o: $(foreach dir, $(SRC_DIRS), $(dir)/%.cpp) | $(BUILD_DIR)
                                                    ^^^^^^^

给出:

build/%.o: src/engine/%.cpp src/application/%.cpp | build

这是什么意思?这意味着为了匹配此模式规则,每个 build/XXX.o 文件必须找到(或能够构建)匹配的 src/engine/XXX.cpp AND 匹配的 src/application/XXX.cpp。基本上每个目标文件都有两个源文件作为先决条件。

由于您的 none 个文件满足该条件,此规则永远无法匹配,因此 make 将无法找到构建目标的方法 build/XXX.o

你必须在这里写两个不同的规则,每个目录一个:

$(BUILD_DIR)/%.o: src/engine/%.cpp | $(BUILD_DIR)
        ...
$(BUILD_DIR)/%.o: src/application/%.cpp | $(BUILD_DIR)
        ...

或者你可以使用 VPATH 让 make 在不同的目录中查找,像这样:

VPATH = $(SRC_DIRS)

$(BUILD_DIR)/%.o: %.cpp | $(BUILD_DIR)
        ...