多个子目录中源的单个 Makefile

Single Makefile for sources in multiple sub directories

在我的 C++ 项目中,源代码组织在 src 目录中。在 src 目录中是子目录,它们都包含头文件和源文件,例如

project
├── Makefile
│
├── MyBinary
│
├── src
│    │
│    ├── main.cpp
│    │
│    ├── Application
│    │      │
│    │      ├── Application.h
│    │      └── Application.cpp
│    │      
│    │
│    └── Tasks
│           ├── BackgroundWorker.h
│           └── BackgroundWorker.cpp
│
└── obj
     ├── Application.o
     └── BackgroungWorker.o

我正在尝试创建一个 Makefile,以便在 obj 目录中创建所有目标文件,并在上面创建可执行文件 MyBinary src 与 Makefile 同级的目录。

它不必太复杂或太自动化。我不介意在 Makefile 中手动指定每个 .cpp 和 .h 文件。

但我是 Makefiles 的新手,不幸的是我正在努力尝试:

CXX=c++
CXXFLAGS=-Wall -Os -g0

# Name of the output binary
APPNAME=MyBinary

# Source root directory
SRC_ROOT=src

# Object file directory
OBJ_DIR=obj

DEPS=$(SRC_ROOT)/Application/Application.h \
     $(SRC_ROOT)/Tasks/BackgroundWorker.h

_OBJ=$(SRC_ROOT)/Application/Application.o \
    $(SRC_ROOT)/Tasks/BackgroundWorker.o\
    $(SRC_ROOT)/main.o

OBJ=$(patsubst %,$(OBJ_DIR)/%,$(_OBJ))

# This rule says that the .o file depends upon the .c version of the 
# file and the .h files included in the DEPS macro.
$(OBJ_DIR)/%.o: %.cpp $(DEPS)
  $(CXX) -c -o $@ $< $(CXXFLAGS)

# Build the application.
# NOTE: The $@ represents the left side of the colon, here $(APPNAME)
#       The $^ represents the right side of the colon, here $(OBJ)
$(APPNAME): $(OBJ)
  $(CXX) -o $@ $^ $(CXXFLAGS)

clean:
  rm -f $(OBJ_DIR)/*.o $(APPNAME)

调用make时的错误是:致命错误:无法创建obj/src/Application.o:找不到文件或目录。

有人可以帮忙吗?

OBJ=$(patsubst %,$(OBJ_DIR)/%,$(_OBJ))obj/ 添加到 _OBJ 的单词前面。您想用 obj 替换 src 可以用

做的事情
OBJ=$(patsubst $(SRC_ROOT)/%,$(OBJ_DIR)/%,$(_OBJ))

请注意,您获得的目录结构需要子目录 ApplicationTasksobj,您必须在调用 make 或更新 Makefile 之前手动创建它们创建它们。

当目录结构为 pre-created.

时,这里的行为符合我的预期
APPNAME=MyBinary
SRC_ROOT=src
OBJ_DIR=obj

DEPS=$(SRC_ROOT)/Application/Application.h \
     $(SRC_ROOT)/Tasks/BackgroundWorker.h

_OBJ=$(SRC_ROOT)/Application/Application.o \
    $(SRC_ROOT)/Tasks/BackgroundWorker.o\
    $(SRC_ROOT)/main.o

OBJ=$(patsubst $(SRC_ROOT)/%,$(OBJ_DIR)/%,$(_OBJ))

$(OBJ_DIR)/%.o: $(SRC_ROOT)/%.cpp $(DEPS)
    echo Making $@ from $<
    touch $@ 

$(APPNAME): $(OBJ)
    echo Making $@ from $^
    touch $@

请注意,在实践中,您必须更好地处理依赖项,并且可能让它们由编译器生成(请参阅 -MM 和 g++ 的类似选项),在这里,当您更改 header.