CC=g++ # Flags for the C compiler CXX_FLAGS= \ -Wall \ -std=c++11 \ -O2 # Linker Flags LD_FLAGS= # Sources to compile SOURCES=main.cpp OBJECTS=$(SOURCES:.cpp=.o) EXE=HolidayLights.out all: $(SOURCES) $(EXE) # Primary build target $(EXE): $(OBJECTS) $(CC) $(LD_FLAGS) $(OBJECTS) -o $@ .o: $(CC) -c $(CXX_FLAGS) -o $@ $<
此makefile的构建命令的输出是:
g++ -c -o main.o main.cpp
我不明白为什么它没有在输出中列出我的标志.此外,我知道它忽略了-std = c 11标志,因为它抱怨非静态常量成员,并且它不应该启用该标志.
Edit0:符号更改
解决方法
默认情况下(来自GNU make doco):
n.o
is made automatically fromn.cc
,n.cpp
,orn.C
with a recipe of the form'$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c'
您的规则在第一种情况下不起作用的原因是单后缀隐式规则应该是源类型而不是目标类型.因此规则将是.cpp :(再次,来自make doco):
A double-suffix rule is defined by a pair of suffixes: the target suffix and the source suffix. It matches any file whose name ends with the target suffix. The corresponding implicit prerequisite is made by replacing the target suffix with the source suffix in the file name. A two-suffix rule whose target and source suffixes are
'.o'
and'.c'
is equivalent to the pattern rule'%.o : %.c'
.A single-suffix rule is defined by a single suffix,which is the source suffix. It matches any file name,and the corresponding implicit prerequisite name is made by appending the source suffix. A single-suffix rule whose source suffix is
'.c'
is equivalent to the pattern rule'% : %.c'
.
但是,仔细阅读最后一句话.只有在想要将XYZZY.cpp转换为XYZZY时,.cpp:规则才有效.由于您的目标是XYZZY.o形式,因此不会使用它.
当然,另一种选择是根本不触及默认规则,而只是修改它们使用的变量:
CXX = g++ CPPFLAGS = -Wall -std=c++11 -O2 LD_FLAGS = SOURCES = main.cpp OBJECTS = $(SOURCES:.cpp=.o) EXE = HolidayLights.out all: $(SOURCES) $(EXE) $(EXE): $(OBJECTS) $(CXX) $(LD_FLAGS) $(OBJECTS) -o $@
而且,如果您想使用隐式规则,您应该使用首选表单,这是较新的模式规则.它们比后缀更强大.