Makefile 似乎忽略了标志。为什么?
Makefile seems to ignore flags. Why?
make 似乎忽略了我的 cflag 选项,因为编译器抱怨我需要 -std=c++11
作为标志,但该选项包含在我的 makefile 中。
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:符号更改
您的 .o:
规则未被选中,请改为 .cpp.o:
。
默认(来自 GNU make
doco):
n.o
is made automatically from n.cc
, n.cpp
, or n.C
with a recipe of the form '$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c'
您的规则在第一种情况下不起作用的原因是单后缀隐式规则应该是 source 类型而不是目标类型。因此,规则将是 .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'
.
但是,请仔细阅读最后一句话。 .cpp:
规则只有在你想把XYZZY.cpp
变成XYZZY
时才有效。因为你的目标是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 $@
而且,如果您想使用隐式规则,您应该真正使用首选形式,即较新的模式规则。它们比后缀的更强大。
make 似乎忽略了我的 cflag 选项,因为编译器抱怨我需要 -std=c++11
作为标志,但该选项包含在我的 makefile 中。
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:符号更改
您的 .o:
规则未被选中,请改为 .cpp.o:
。
默认(来自 GNU make
doco):
n.o
is made automatically fromn.cc
,n.cpp
, orn.C
with a recipe of the form'$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c'
您的规则在第一种情况下不起作用的原因是单后缀隐式规则应该是 source 类型而不是目标类型。因此,规则将是 .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'
.
但是,请仔细阅读最后一句话。 .cpp:
规则只有在你想把XYZZY.cpp
变成XYZZY
时才有效。因为你的目标是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 $@
而且,如果您想使用隐式规则,您应该真正使用首选形式,即较新的模式规则。它们比后缀的更强大。