c ++源代码的递归makefile

时间:2017-07-10 13:41:09

标签: c++ makefile

我已经在makefile上苦苦挣扎了一段时间,因为我真的不知道它们是如何工作的......

我的目录结构如下:

--bin/
--build/
--includes/
--src/
----classes/
------somefiles.hpp
------somefiles.cpp
----states/
------somestates.hpp
----main.cpp
----few-other-files.cpp
--Makefile
--.gitignore

我的makefile工作得很好,直到我开始添加这些子目录,classesstates

我似乎无法找到如何在我的makefile中包含这些子目录:

CC := g++
SRCDIR := src
BUILDDIR := build
TARGET := bin/game

SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -g -Wall
LIB := -lsfml-audio -lsfml-graphics -lsfml-network -lsfml-system -lsfml-window
INC := -I includes -I /usr/local/include -L /usr/local/lib

$(TARGET): $(OBJECTS)
    @echo "Linking..."
    @echo "$(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB)

$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
    @mkdir -p $(BUILDDIR)
    @echo "$(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $<

clean:
    @echo "Cleaning..."
    @echo "$(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)

.PHONY: clean

问题:Fatal error: can't create build/classes/stateManager.o: No such file or directory

通常,问题是它无法构建目录。但我认为mkdir -p会解决这个问题。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

替换:

mkdir -p $(@:D)

make

另一种方法是让mkdir -p将目录视为目标并自动为您创建目标,这样您就不必使用.SECONDEXPANSION: # Make the object file depend on its directory using order-only dependency. $(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT) | $$(dir $$@) $(CC) $(CFLAGS) $(INC) -c -o $@ $< # Let make create the directories for your targets. %/: mkdir -p $@ # Do not remove the directories because they are "intermediate" targets. .PRECIOUS : %/ 调用来混淆您的食谱:

{{1}}