【问题标题】:C++ Makefile error - No rule to make target '%.cpp'C++ Makefile 错误 - 没有规则来制作目标“%.cpp”
【发布时间】:2015-07-24 11:41:49
【问题描述】:

美好的一天,我正在学习编写自己的 makefile 的教程,我在这里找到了教程:

http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/

我知道本教程适用于 C makefile,但是我相信 C 和 C++ 之间的相似之处意味着 makefile 的功能几乎没有区别(例如,我使用的是 g++ 而不是 gcc)。我希望我对这一点的假设不是一个因素,因为前面 4 个教程中的每一个似乎都运行良好。

在教程中运行 Makefile 5 时,我收到关于从 .cpp 文件构建目标文件的错误:

make: *** No rule to make target '%.cpp', needed by 'obj'. Stop.

我似乎无法弄清楚为什么会发生这种情况,非常困惑和沮丧,因为我觉得这不应该发生。我在下面包含了我的完整 Makefile,任何帮助将不胜感激:

 # Example Makefile                                                                                                                                                                                             
 # ----------------
 # Please remember to turn off the vim option: 'expandtab' so that tabs are 
 # actually displayed as tabs (do so like this - :set noexpandtab )
 #

 # This file specifies dependencies, which means that the two c++ files must
 # be compiled before the executable is built

 # ------------------
 # Makefile Constants
 # ------------------

 # Directory constants
 IDIR =../include  # Specifies location of include directory
 ODIR =obj        # Specifies location of object directory
 LDIR =../lib      # Specifies location of library directory

 LIBS=-lm  # ?

 # Options constants
 CC=g++            # Specifies the specific C compiler to use, g++ specifies C++ compiler
 CFLAGS=-I$(IDIR)  # List of flags to pass to compilation command

 # Dependency Constants
 DEP_FILES=helloMake.h                      # Specifies dependency files
 DEPS=$(patsubst %,$(IDIR)/%,$(DEP_FILES))  # Specifies path to dependencies and dependency files

 # Object constants
 OBJ_FILES=helloMake.o helloFunc.o         # Specify object files
 OBJ=$(patsubst %,$(ODIR)/%,$(OBJ_FILES))  # Specifies path to objects and object files


 # -----------
 # Compilation
 # -----------

 # Specify rules to make object files
 $(ODIR)/%.o: %.cpp $(DEPS)       # Specifies that .o files depend on the .cpp version of the file and the .h files included in the DEPS macro
     $(CC) -c -o $@ $< $(CFLAGS)  # The -c flag says to generate the object file, the -o $@ says to put the output of the compilation in the
                                  # file named on the left side of the : the $< is the first item in the dependencies list

 # Specify rules to make target executable
 helloMake: $(OBJ)                     # Target : Dependencies
     $(CC) -o $@ $^ $(CFLAGS) $(LIBS)  # This is the actual compilation command

 .PHONY: clean  # Prevent the make command from attempting to do something with a file named 'clean'

 # Specify rules to clean the object files
 clean:
     rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~  # Removes all compiled object files

【问题讨论】:

    标签: c++ makefile


    【解决方案1】:

    不幸的是,这个“简单”的教程宣扬了不良做法。

    基本上,您首先需要一个正确的 C Makefile:

    # Specify the final target name
    EXE := helloMake
    
    # Specify the source files
    # Effectively list all source files in the current directory
    SRC := $(wildcard *.c)
    
    # From the source file list, get the corresponding object file list
    # This is a clearer syntax for $(patsubst %.c,%.o,$(SRC))
    OBJ := $(SRC:.c=.o)
    
    # From the object file list, get the dependency file list to handle automatic
    # recompilation when a header file is modified
    DEP := $(OBJ:.o=.d)
    
    # Specify preprocessor flags (this is a built-in variable)
    CPPFLAGS := -I../include
    # Required flags to enable the automatic dependency generation by the compiler
    CPPFLAGS += -MMD -MP
    
    # Specify compiler flags (this is a built-in variable)
    # Here some basic warning flags
    CFLAGS := -Wall -W -pedantic
    
    # Specify linker flags (this is a built-in variable)
    LDFLAGS := -L../lib
    
    # Specify linker libraries (this is a built-in variable)
    # m is the maths library
    LDLIBS := -lm
    
    # Tell make that these target are not real files
    .PHONY: all clean
    
    # Now the standard primary rule
    all: $(EXE)
    
    # How do we make $(EXE) ? Remember the recipe describe the linking phase
    $(EXE): $(OBJ)
        $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
    
    # Let's clean up the mess
    clean:
        $(RM) $(EXE) $(OBJ) $(DEP)
    
    # Don't forget to include the dependency files to let make know when to recompile
    -include $(DEP)
    

    那你需要知道C Makefile和C++ Makefile的区别:

    • 使用$(CXX) 而不是$(CC)
    • 使用$(CXXFLAGS) 而不是$(CFLAGS)
    • 使用.cpp 而不是.c

    你已经完成了。

    # Specify the final target name
    EXE := helloMake
    
    # Specify the source files
    # Effectively list all source files in the current directory
    SRC := $(wildcard *.cpp)
    
    # From the source file list, get the corresponding object file list
    # This is a clearer syntax for $(patsubst %.cpp,%.o,$(SRC))
    OBJ := $(SRC:.cpp=.o)
    
    # From the object file list, get the dependency file list to handle automatic
    # recompilation when a header file is modified
    DEP := $(OBJ:.o=.d)
    
    # Specify preprocessor flags (this is a built-in variable)
    CPPFLAGS := -I../include
    # Required flags to enable the automatic dependency generation by the compiler
    CPPFLAGS += -MMD -MP
    
    # Specify compiler flags (this is a built-in variable)
    # Here some basic warning flags
    CXXFLAGS := -Wall -W -pedantic
    
    # Specify linker flags (this is a built-in variable)
    LDFLAGS := -L../lib
    
    # Specify linker libraries (this is a built-in variable)
    # m is the maths library
    LDLIBS := -lm
    
    # Tell make that these target are not real files
    .PHONY: all clean
    
    # Now the standard primary rule
    all: $(EXE)
    
    # How do we make $(EXE) ? Remember the recipe describe the linking phase
    $(EXE): $(OBJ)
        $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
    
    # Let's clean up the mess
    clean:
        $(RM) $(EXE) $(OBJ) $(DEP)
    
    # Don't forget to include the dependency files to let make know when to recompile
    -include $(DEP)
    

    编辑:为了能够重新定位构建文件(.o.d 文件),您需要进行一些调整:

    # Specify the final target name
    EXE := helloMake
    
    # Specify the source files
    # Effectively list all source files in the current directory
    SRC := $(wildcard *.cpp)
    
    # Specify where to put the build temporary files
    BLD := obj
    
    # From the source file list, get the corresponding object file list
    # This is a clearer syntax for $(patsubst %.cpp,$(BLD)/%.o,$(SRC))
    OBJ := $(SRC:%.cpp=$(BLD)/%.o)
    
    # From the object file list, get the dependency file list to handle automatic
    # recompilation when a header file is modified
    DEP := $(OBJ:.o=.d)
    
    # Specify preprocessor flags (this is a built-in variable)
    CPPFLAGS := -I../include
    # Required flags to enable the automatic dependency generation by the compiler
    CPPFLAGS += -MMD -MP
    
    # Specify compiler flags (this is a built-in variable)
    # Here some basic warning flags
    CXXFLAGS := -Wall -W -pedantic
    
    # Specify linker flags (this is a built-in variable)
    LDFLAGS := -L../lib
    
    # Specify linker libraries (this is a built-in variable)
    # m is the maths library
    LDLIBS := -lm
    
    # Tell make that these target are not real files
    .PHONY: all clean
    
    # Now the standard primary rule
    all: $(EXE)
    
    # How do we make $(EXE) ? Remember the recipe describe the linking phase
    $(EXE): $(OBJ)
        $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
    
    # The build directory is custom so we need to tell make how to do it
    # The build directory must exist before trying to compile
    $(BLD)/%.o: %.cpp | $(BLD)
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
    
    # Simple rule to create the build directory if needed
    $(BLD):
        mkdir $@
    
    # Let's clean up the mess, we can just remove the build directory alonside the executable
    clean:
        $(RM) -r $(BLD) $(EXE)
    
    # Don't forget to include the dependency files to let make know when to recompile
    -include $(DEP)
    

    【讨论】:

    • 谢谢你的回答,我已经尝试过你建议的 Makefile 但是当我尝试制作它时,我现在得到一个不同的错误:g++:错误:arm-linux-androideabi:没有这样的文件或目录
    • 您的编译器设置似乎不正确,您的环境是什么(操作系统、编译器版本、make 版本)?
    • 操作系统:Linux(使用 VMWare vSphere 客户端虚拟机)编译器版本:g++ (GCC) 4.9.3 20141101 for GNAT Pro 7.3.1 (20150118) 制作版本:不完全确定您的意思!
    • 嗯,我不知道那个 GNAT Pro 版本的 GCC ......你能通过编辑你的问题来发布整个 make 输出吗?我的意思是您这边出了点问题,因为我在这里编写的 makefile 功能齐全且经过测试。
    • 我已经尝试过多次发布我的错误,但由于某种原因堆栈溢出似乎不想让我...请在下面找到完整的错误:g++ -Wall -W -pedantic -I../include -MMD -MP arm-linux-androideabi -c -o helloFunc.o helloFunc.cpp g++: error: arm-linux-androideabi: No such file or directory make: *** [helloFunc.o]错误 1
    【解决方案2】:

    % 语法不是标准的make,而是由例如提供的扩展功能。 GNU make(否则,您必须为每个源文件编写特定的规则,或者使用类似 autotools 的东西来生成 huge(但可移植)Makefiles)。因此,您应该确保使用支持此功能的 make 实用程序。如果这不是问题,您确定您的当前目录中确实有 *.cpp 文件吗?

    【讨论】:

    • 我确实在正确的目录中有 .cpp 文件,我还确保将我的 .h 文件放在 inlcude 目录中。我开始认为我使用的教程网页对我来说可能不是最好的......
    • 您是否有任何不支持模式规则(% 语法)的常用 make 实现示例?
    • 例如nmake 不支持这一点,并以不太灵活的 推理规则 作为替代。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    相关资源
    最近更新 更多