linux C ++ gmake"没有制定目标的规则"

时间:2018-05-28 16:35:18

标签: c++ linux makefile gnu-make makefile-errors

自从我不得不管理makefile以来已经有几十年了。我在这里检查过现有的答案,并在其他地方搜索。当我使用目标时:$(BIN)/$(EXECUTABLE): $(OBJECTS) 我难以理解为什么我的makefile失败了:

$ make make: *** No rule to make target 'src/SRecord.o', needed by 'bin/main'. Stop.

这里是Makefile:

# execute `make D='-g'` to make with debug symbols
DEFS := $(D)
CC := gcc $(DEFS)
CFLAGS := -std=c99 -Wall -Wextra
CXX := g++ $(DEFS)
CXXFLAGS := -std=c++17 -Wall -Wextra

BIN := bin
SRC := src
INCLUDE := include
LIB := lib

#LIBSOURCES := $(wildcard $(LIB)/*.cpp)
#LIBOBJECTS := $(LIBSOURCES:%.cpp=%.o)
#OBJECTS := $(subst $(LIB)/,,$(LIBOBJECTS))

SOURCES := $(wildcard $(SRC)/*.cpp)
OBJECTS := $(SOURCES:%.cpp=%.o)

# SOURCES := \
#   $(SRC)/main.cpp \
#   $(SRC)/SerialPort.cpp \
#   $(SRC)/ShadowUpdate.cpp \
#   $(SRC)/SRecord.cpp

# OBJECTS := \
#   $(SRC)/SerialPort.o \
#   $(SRC)/ShadowUpdate.o \
#   $(SRC)/SRecord.o

EXECUTABLE := main

#
# Targets
#
.PHONY: clean all
all: $(BIN)/$(EXECUTABLE)

clean:
    $(RM) $(BIN)/$(EXECUTABLE)
    $(RM) $(OBJECTS)

run: all
    ./$(BIN)/$(EXECUTABLE)

# $(BIN)/$(EXECUTABLE): $(SRC)/*
$(BIN)/$(EXECUTABLE): $(OBJECTS)
    $(CXX) $(CXXFLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ 

#
# Dependencies - must be at end of makefile
#
DEPDIR := .d
$(shell mkdir -p $(DEPDIR) >/dev/null)
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td

POSTCOMPILE = @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $@

%.o: %.c
%.o: %.c $(DEPDIR)/%.d
    $(CC) -c -o $*.o $(DEPFLAGS) $(CFLAGS) $<
    $(POSTCOMPILE)

%.o: %.cpp
%.o: %.cpp $(DEPDIR)/%.d
    $(CXX) -c -o $*.o $(DEPFLAGS) $(CXXFLAGS) $<
    $(POSTCOMPILE)

$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d

include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SOURCES))))

它适用于已注释的目标:# $(BIN)/$(EXECUTABLE): $(SRC)/*

但是,我希望能够拥有一个更复杂的项目而不是简单地将所有源文件列为可执行文件的依赖项。

这是我的文件树:

src/
src/SerialPort.cpp
src/SRecord.cpp
src/ShadowUpdate.cpp
src/main.cpp
include
include/ShadowUpdate.h
include/SerialPort.h
include/SRecord.h
include/the_exo.h
bin/
bin/main
Makefile
lib/
.d/

自动依赖关系生成也不起作用,即使它直接来自GNU的网站。

2 个答案:

答案 0 :(得分:2)

考虑%扩展到

的内容
%.o: %.cpp $(DEPDIR)/%.d

插入src/SRecord.o

src/SRecord.o: src/SRecord.cpp .d/src/SRecord.d

你能看到这个问题吗?请尝试以下

$(SRC)/%.o: $(SRC)/%.cpp $(SRC)/$(DEPDIR)/%.d

你不需要覆盖内置食谱(%.o: %.cpp)的行,去除它们。

答案 1 :(得分:0)

For clarity to anyone looking for the answer in the future, I'm posting my revised Makefile:

BIN := bin
SRC := src
INCLUDE := include
LIB := lib

# execute `make D='-g'` to make with debug symbols
DEFS := $(D)
CC := gcc $(DEFS)
CFLAGS := -I$(INCLUDE) -std=c99 -Wall -Wextra
CXX := g++ $(DEFS)
CXXFLAGS := -I$(INCLUDE) -std=c++17 -Wall -Wextra

#LIBSOURCES := $(wildcard $(LIB)/*.cpp)
#LIBOBJECTS := $(LIBSOURCES:%.cpp=%.o)
#OBJECTS := $(subst $(LIB)/,,$(LIBOBJECTS))

SOURCES := $(wildcard $(SRC)/*.cpp)
OBJECTS := $(SOURCES:%.cpp=%.o)

EXECUTABLE := main

#
# Targets
#
.PHONY: clean all
all: $(BIN)/$(EXECUTABLE)

clean:
    $(RM) $(BIN)/$(EXECUTABLE)
    $(RM) $(OBJECTS)

run: all
    ./$(BIN)/$(EXECUTABLE)

$(BIN)/$(EXECUTABLE): $(OBJECTS)
    $(CXX) $(CXXFLAGS) -L$(LIB) $^ -o $@ 

#
# Dependencies - must be at end of makefile
#
DEPDIR := $(SRC)/.d
$(shell mkdir -p $(DEPDIR) >/dev/null)
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td

POSTCOMPILE = @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $@

%.o: %.c
%.o: %.c $(DEPDIR)/%.d
    $(CC) -c -o $@ $(DEPFLAGS) $(CFLAGS) $<
    $(POSTCOMPILE)

%.o: %.cpp
$(SRC)/%.o: $(SRC)/%.cpp $(DEPDIR)/%.d
    $(CXX) -c -o $@ $(DEPFLAGS) $(CXXFLAGS) $<
    $(POSTCOMPILE)

$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d

include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SOURCES))))

To clarify my comment to the above answer, if I comment out the "override" target (%.o: %.cpp), here's the output:

$ make clean; make
rm -f bin/main
rm -f src/SRecord.o src/SerialPort.o src/ShadowUpdate.o src/main.o
g++  -Iinclude -std=c++17 -Wall -Wextra   -c -o src/SRecord.o src/SRecord.cpp
g++  -Iinclude -std=c++17 -Wall -Wextra   -c -o src/SerialPort.o src/SerialPort.cpp
g++  -Iinclude -std=c++17 -Wall -Wextra   -c -o src/ShadowUpdate.o src/ShadowUpdate.cpp
g++  -Iinclude -std=c++17 -Wall -Wextra   -c -o src/main.o src/main.cpp
g++  -Iinclude -std=c++17 -Wall -Wextra -Llib src/SRecord.o src/SerialPort.o src/ShadowUpdate.o src/main.o -o bin/main 

Clearly, the dependencies are not being made. My rule was not executed.

But, with the empty %.o: %.cpp target in place, the make performs as expected:

$ make clean; make
rm -f bin/main
rm -f src/SRecord.o src/SerialPort.o src/ShadowUpdate.o src/main.o
g++  -c -o src/SRecord.o -MT src/SRecord.o -MMD -MP -MF src/.d/SRecord.Td -Iinclude -std=c++17 -Wall -Wextra src/SRecord.cpp
g++  -c -o src/SerialPort.o -MT src/SerialPort.o -MMD -MP -MF src/.d/SerialPort.Td -Iinclude -std=c++17 -Wall -Wextra src/SerialPort.cpp
g++  -c -o src/ShadowUpdate.o -MT src/ShadowUpdate.o -MMD -MP -MF src/.d/ShadowUpdate.Td -Iinclude -std=c++17 -Wall -Wextra src/ShadowUpdate.cpp
g++  -c -o src/main.o -MT src/main.o -MMD -MP -MF src/.d/main.Td -Iinclude -std=c++17 -Wall -Wextra src/main.cpp
g++  -Iinclude -std=c++17 -Wall -Wextra -Llib src/SRecord.o src/SerialPort.o src/ShadowUpdate.o src/main.o -o bin/main