C++ 没有制定目标的规则。哦,为什么?

C++ 没有制定目标的规则。哦,为什么?,c++,makefile,C++,Makefile,这是我生命中的第一个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 oth

这是我生命中的第一个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

您的
Makefile
存在一些问题

1)当文件使用
.cpp
时,您使用的是
.c
扩展名

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

3)创建对象的一般规则不会因为这些原因而被调用,但也因为您没有指定源的子目录

因为
make
正在制定自己的规则来编译对象,而忽略了您制定的规则

我还建议为
C++
使用正确的隐式变量,这将使隐式规则更好地工作

详情如下:

因此,我建议采取类似的措施:

# 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
#定义要使用的C编译器
CXX=g++
#定义任何编译时标志
CXXFLAGS=-std=c++11-g-Wall
#定义包含除/usr/include以外的头文件的任何目录
CPPFLAGS=-I./include
#定义对象文件的指令
OBJDIR=/构建
SRCDIR=/src
#定义C源文件
SRCS=$(SRCDIR)/action.cpp$(SRCDIR)/main.cpp
#定义C对象文件
OBJS=$(patsubst$(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
#定义可执行文件
主=输出
.骗子:看情况
全部:$(主)
@已编译的echo程序
$(主):$(OBJS)
$(CXX)$(CXXFLAGS)$(CPPFLAGS)-o$(主)$(OBJS)
$(OBJDIR)/%.o:$(SRCDIR)/%.cpp
@echo“编译:”$@
$(CXX)$(CXXFLAGS)$(CPPFLAGS)-c-o$@$<
清洁:
$(RM)$(OBJDIR)/*.o*~$(主)
依赖:$(SRC)
MakeDependent$(包括)$^
#不要删除这一行--使其成为所需

.c
.cpp
CC
不同,
CFLAGS
用于c,因此默认规则也不会拾取它。更好地使用
CXX
CXXFLAGS
# 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