gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:26:58 26 4
gpt4 key购买 nike

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

最佳答案

您的 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

关于c++ - 没有规则来制作目标.o,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50657831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com