没有规则来制作目标.o,为什么?

No rule to make target .o, why?

本文关键字:为什么 目标 有规则      更新时间:2023-10-16

这是我生命中的第一个制作文件!我有一个项目,其中有src文件夹(我在其中保存我的 .cpp 文件(、一个包含文件夹(我在其中保存我的 .hpp 文件(和一个我想在其中存储目标文件的构建文件夹。

# define the C compiler to use
CCXX = g++ -std=c++11
# define any compile-time flags
CXXFLAGS = -g -Wall
# define any directories containing header files other than /usr/include
INCLUDES = -I./include
#define the directory for src files
SRCDIR = ./src/
#define the directive for object files
OBJDIR = ./build/
# define the C source files
SRCS = action.cpp conditionedBT.cpp control_flow_node.cpp execution_node.cpp main.cpp
# define the C object files 
OBJS = $(OBJDIR)$(SRCS:.cpp=.o)
# define the executable file 
MAIN = out
.PHONY: depend 
all: $(MAIN)
@echo Program compiled
$(MAIN): $(OBJS) 
$(CCXX) $(CXXFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS)
$(OBJDIR)/%.o: ($SRCDIR)/%.c
$(CCXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<
#.c.o:
#   $(CC) $(CFLAGS) $(INCLUDES) -c $<  -o $@
#clean:
#   $(RM) *.o *~ $(MAIN)
depend: $(addprefix $(SRCDIR),$(SRCS))
makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it

鉴于上述情况,当我尝试执行make时,我收到以下错误:

make: *** No rule to make target `build/action.o', needed by `out'.  Stop.

您的Makefile存在一些问题。

1(当您的文件使用.cpp时,您正在使用.c扩展名。

2(你的替换指令OBJS = $(SRCS:.c=.o)没有考虑你的源和对象的子目录。

3(由于这些原因,您没有调用创建对象的一般规则,但也因为您没有指定源的子目录。

正因为如此,make正在制定自己的规则来编译你的对象,而忽略你制定的规则。

此外,我建议使用正确的隐式变量进行C++这将使隐式规则更好地工作。

它们详见下文:https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html

所以我会推荐更多类似的东西:

# define the C compiler to use
CXX = g++ 
# define any compile-time flags
CXXFLAGS = -std=c++11 -g -Wall
# define any directories containing header files other than /usr/include
CPPFLAGS = -I./include
#define the directive for object files
OBJDIR = ./build
SRCDIR = ./src
# define the C source files
SRCS = $(SRCDIR)/action.cpp $(SRCDIR)/main.cpp
# define the C object files 
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
# define the executable file 
MAIN = out
.PHONY: depend 
all: $(MAIN)
@echo Program compiled
$(MAIN): $(OBJS) 
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(MAIN) $(OBJS)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
@echo "Compiling: " $@
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
clean:
$(RM) $(OBJDIR)/*.o *~ $(MAIN)
depend: $(SRCS)
makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it
相关文章: