【发布时间】: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
【问题讨论】: