0

我正在调整我的 Makefile 以查看 4 个目录,而不是 2 个(它有一个用于源文件,一个用于头文件,但我为公共源和包含添加了一个新文件夹)。我有如下内容:

CC = g++
FLAGS = -g -c

BUILDDIR = build
INCLUDEDIR = -Icode/inc -I../common/code/inc -I/usr/include/libxml2

SOURCEDIR = code/src ../common/code/src
SOURCES = $(wildcard $(SOURCEDIR)/*.cpp)
OBJECTS = $(patsubst $(SOURCEDIR)/%.cpp,$(BUILDDIR)/%.o,$(SOURCES))

EXECUTABLE = Exec

all: $(BUILDDIR)/$(EXECUTABLE)

$(BUILDDIR)/$(EXECUTABLE): $(OBJECTS)
    $(CC) $^ -o $@ -lpthread -lxml2

$(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp
    $(CC) $(FLAGS) $< $(INCLUDEDIR) -o $@ -Wno-write-strings

我尝试向 INCLUDEDIR 添加一个条目,如下所示:

-I../common/code/inc

并将 ../common/code/src 添加到 SOURCEDIR:

SOURCEDIR = code/src ../common/code/src

这目前不起作用,我想知道如何解决它。我收到错误消息:

Makefile:27: target `code/src' doesn't match the target pattern

但到目前为止我找不到如何解决它。任何帮助,将不胜感激。

编辑:在下面的 MadScientist 响应之后,我得到以下输出:

g++    -c -o code/src/Client.o code/src/Client.cpp
code/src/Client.cpp:1:20: fatal error: Client.h: No such file or directory
compilation terminated.
make: *** [code/src/Client.o] Error 1

更新的 Makefile:

SOURCEDIR = code/src ../common/code/src
SOURCES = $(wildcard $(addsuffix /*.cpp,$(SOURCEDIR)))
OBJECTS = $(SOURCES:%.cpp=%.o)

$(BUILDDIR)/$(EXECUTABLE): $(OBJECTS)
    $(CC) $^ -o $@ -lpthread -lxml2

$(BUILDDIR)/%.o : ../common/code/src/%.cpp
    $(CC) $(FLAGS) $< $(INCLUDEDIR) -o $@ -Wno-write-strings

$(BUILDDIR)/%.o : code/src/%.cpp
    $(CC) $(FLAGS) $< $(INCLUDEDIR) -o $@ -Wno-write-strings

PS:

我能够使用以下方法修复它:

SOURCEDIR = code/src ../common/code/src
SOURCES = $(wildcard $(addsuffix /*.cpp,$(SOURCEDIR)))
TEMP_OBJ = $(SOURCES:%.cpp=%.o)
NOT_DIR = $(notdir $(TEMP_OBJ))
OBJECTS = $(addprefix $(BUILDDIR)/, $(NOT_DIR))
4

1 回答 1

1

当然,因为现在您的静态模式规则扩展为:

$(OBJECTS): build/%.o : code/src ../common/code/src/%.cpp

这是非法的语法。如果您避免使用静态模式规则,而是使用模式规则,那么它将正常工作。用两个模式规则替换您的单个静态模式规则:

$(BUILDDIR)/%.o : code/src/%.cpp
        $(CC) $(FLAGS) $< $(INCLUDEDIR) -o $@ -Wno-write-strings

$(BUILDDIR)/%.o : ../common/code/src/%.cpp
        $(CC) $(FLAGS) $< $(INCLUDEDIR) -o $@ -Wno-write-strings

编辑:您还需要更改 SOURCEDIR 的其他用途:

SOURCES = $(wildcard $(addsuffix /*.cpp,$(SOURCEDIR))
OBJECTS = $(patsubst %.cpp,$(BUILDDIR)/%.o,$(notdir $(SOURCES)))
于 2013-06-18T21:28:30.680 回答