这是我生命中的第一个makefile!我有一个项目,其中我有 src 文件夹(我保存我的.cpp文件),一个 include 文件夹(我保存我的.hpp文件)和一个 build 文件夹,我想在其中存储我的目标文件。
# 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.
答案 0 :(得分:5)
您的Makefile
存在一些问题。
1)当您的文件使用.c
时,您正在使用.cpp
扩展程序。
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