我正在调整我的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))
答案 0 :(得分: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)))