【问题标题】:Handling Makefile dependencies in multiple subdirectories and outputting to seperate subdirectory处理多个子目录中的 Makefile 依赖关系并输出到单独的子目录
【发布时间】:2017-08-24 09:04:34
【问题描述】:

我一直在阅读有关使用子目录的 Makefile 的帖子,但似乎无法将所有部分正确组合在一起。我有以下文件结构:

program
    |
    - src
    |
    - include
    | 
    - build
    |
    - bin
    |
    - Makefile

我所有的源文件 (.cpp) 都在 program/src/ 中,我所有的头文件 (.hpp) 都在 program/include 中,我希望将所有目标文件和依赖文件放入 program/build 中,并且我想要我的二进制文件放入程序/bin。以下是我目前在 Makefile 中的内容:

CXX = g++
BIN_DIR = bin
TARGET = $(BIN_DIR)/coreint
BUILD_DIR = build
SRC_DIR = src
INC_DIR = include
CXXFLAGS = -Wall -g

# Get all source files
SRCS := $(shell find $(SRC_DIR) -name *.cpp)
# Get all object files by replacing src directory with build directory    and replace extension
OBJS := $(subst $(SRC_DIR), $(BUILD_DIR), $(SRCS:%.cpp=%.o))
# Get all depend files by replacing extensions
DEPS := $(OBJS:.o=.d)
# Get all includes by searching include directory
INCS := $(shell find $(INC_DIR) -name *.hpp)
# Append -I to the front of each include
INCS := $(foreach d, $(INCS), -I$d)
# Using VPATH to find files in src/ include/ and build/
VPATH = $(SRC_DIR):$(INC_DIR):$(BUILD_DIR)

all: $(TARGET)

$(TARGET): $(OBJS)
    $(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJS) -o $(TARGET)

# Build object files and put in build directory
$(BUILD_DIR)%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

# Place dependency files in build directory
# automatically generate dependency rules
$(BUILD_DIR)%.d: %.cpp
    $(CXX) $(CXXFLAGS) -MF"$@" -MG -MM -MD -MP -MT"$@" -MT"$(OBJS)" "$<"
# -MF  write the generated dependency rule to a file
# -MG  assume missing headers will be generated and don't stop with an error
# -MM  generate dependency rule for prerequisite, skipping system headers
# -MP  add phony target for each header to prevent errors when header is missing
# -MT  add a target to the generated dependency

.PHONY: clean all

clean:
    rm -f $(OBJS) $(DEPS) $(TARGET)

-include $(DEPS)

当我运行 make 时,我收到以下错误:

make: *** No rule to make target `build/assign.o', needed by `bin/coreint'.  Stop.

其中assign.o 是构建目录中的第一个文件。提前谢谢你。

【问题讨论】:

  • $(BUILD_DIR) 后面不是缺少斜线吗? AFAIK,当前代码会生成类似buildassign.o: assign.cpp 的规则
  • /我的脸。就是这样。今天早上我一直在看这个东西超过 4 个小时,你马上就看到了。谢谢!

标签: c++ makefile


【解决方案1】:

我只是在我的 BUILD_DIR 之后缺少一个“/”:

# Build object files and put in build directory
$(BUILD_DIR)/%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多