簡體   English   中英

沒有規則來確定目標.o,為什么?

[英]No rule to make target .o, why?

這是我生命中的第一個文件! 我有一個項目,其中有src文件夾(其中保存我的.cpp文件), include文件夾(其中保存我的.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM