Makefile 找不到最后一个先决条件对象

Makefile cannot find the last prerequisite object

我正在尝试使用 src 的几个子目录中的源文件编译一个大型 C++ 项目。

我的 Makefile 如下所示:

# ---------- Compiler and linker directives ----------
CXX = g++-11

CXXFLAGS = -std=c++17 -fopenmp -MD -g -Wall -pedantic
# LDFLAGS = -Wl,--gc-sections


# Output directory after linking
BIN_PATH = ../bin
BUILD_PATH = ../build

# ------- System Include  --------------
# Change ~/include as necessary for your architecture
INC_FLAGS = -I../include -I/opt/homebrew/include



# -------- Establishing objects and sources ------

RATE_SRC = $(wildcard hartreefock/*.cpp) $(wildcard struct_hartreefock/*.cpp) $(wildcard numerical/*.cpp)
RATE_OBJ = $(addprefix $(BUILD_PATH)/,$(RATE_SRC:.cpp=.o)))

# ------------- Specific Include -----------
# RATE_HEADERS = $(wildcard hartreefock/*.hpp) $(wildcard struct_hartreefock/*.hpp) $(wildcard numerical/*.hpp)
RATE_DIR = hartreefock struct_hartreefock numerical

RATE_INC = $(addprefix -I,$(RATE_DIR))



# ---------- Libraries needed for linking ----------

# Change or remove ~/lib as necessary for your architecture
LIB_PATH = -L/opt/homebrew/lib 


# Generic .cc -> .o rule
$(BUILD_PATH)/%.o: %.cpp
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) $(INC_FLAGS) $(RATE_INC) $< -c -o $@

# ---------- Linking rules ----------

# Generic .o -> exec rule: uses all prerequisites (meant to be .o files)
ratecalc: $(RATE_OBJ)
    $(CXX) $(CXXFLAGS) $(INC_PATH) $(A_RATE_INC) $(LDFLAGS) $^ \
    $(LIB_PATH) -o $(BIN_PATH)/$@

# TODO
# simulate: 

echo: 
    echo "$(RATE_OBJ)"
    echo "$(BUILD_PATH)"


# Cleanup:
.PHONY: clean build_directories
clean:
    -rm -r $(BUILD_PATH)/**

奇怪的是,在我找到一个特定文件之前,这似乎是正确的做法:

make clean
make ratecalc
g++-11 -std=c++17 -fopenmp -MD -g -Wall -pedantic -I../include -I/opt/homebrew/include -Ihartreefock -Istruct_hartreefock -Inumerical struct_hartreefock/RateData.cpp -c -o ../build/struct_hartreefock/RateData.o
g++-11 -std=c++17 -fopenmp -MD -g -Wall -pedantic -I../include -I/opt/homebrew/include -Ihartreefock -Istruct_hartreefock -Inumerical numerical/Constant.cpp -c -o ../build/numerical/Constant.o
make: *** No rule to make target `../build/numerical/wignerSymbols.o)', needed by `ratecalc'.  Stop.

这特别奇怪,因为 Constant.cppwignerSymbols.cpp 在同一个目录中。手动运行此目标根据需要编译目标文件,

make ../build/numerical/wignerSymbols.o
g++-11 -std=c++17 -fopenmp -MD -g -Wall -pedantic -I../include -I/opt/homebrew/include -Ihartreefock -Istruct_hartreefock -Inumerical numerical/wignerSymbols.cpp -c -o ../build/numerical/wignerSymbols.o

但这不被认为满足 ratecalc 规则的依赖性。

wignerSymbols.cppRATE_OBJ 中的最后一个条目可能很重要,但说实话,我不明白这里发生了什么。

为了完整起见:我在 M1 macbook air 上使用 GNU make 3.81。

错别字:

RATE_OBJ = $(addprefix $(BUILD_PATH)/,$(RATE_SRC:.cpp=.o)))

这里有一个额外的闭包,您可以在错误消息中看到:

make: *** No rule to make target `../build/numerical/wignerSymbols.o)', ...