gpt4 book ai didi

c++11 - CFLAGS 在 Makefile 中被忽略

转载 作者:行者123 更新时间:2023-12-04 02:04:53 24 4
gpt4 key购买 nike

我正在使用以下 makefile 来构建我的项目:

CC      = /usr/bin/g++
CFLAGS = -Wall -pedantic -std=c++0x
LDFLAGS =

OBJ = main.o pnmhandler.o pixmap.o color.o

pnm: $(OBJ)
$(CC) $(CFLAGS) -o pnm $(OBJ) $(LDFLAGS)

%.o: %.c
$(CC) $(CFLAGS) -c $<

当我运行 make 时,出现以下错误:

/usr/include/c++/4.9.1/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.



正如我从以下行中读到的,CFLAGS 没有正确包含,但我不知道我做错了什么:

g++ -c -o main.o main.cpp



也试过 -std=c++11-std=gnu++11 ,没有任何结果。有任何想法吗?

如果我运行 make -Bn ,我会得到:
g++    -c -o main.o main.cpp
g++ -c -o pnmhandler.o pnmhandler.cpp
g++ -c -o pixmap.o pixmap.cpp
g++ -c -o color.o color.cpp
/usr/bin/g++ -Wall -pedantic -std=c++0x -o pnm main.o pnmhandler.o pixmap.o color.o

编辑:%.o: %.c 替换规则 %.o: %.cpp 解决了我的问题。

最佳答案

你看到的原因

g++    -c -o main.o main.cpp

是 Make 调用其标准规则来创建目标文件:
%.o: %.cpp
# recipe to execute (built-in):
$(COMPILE.cpp) $(OUTPUT_OPTION) $<

该命令扩展为
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<

你应该设置 CCCFLAGS ,而不是在你的 makefile 中设置 CXXCXXFLAGS ,它们是为 C++ 而不是 C 设计的。这允许上面的内置规则为你工作,然后你只需要确保正确的链接器使用,例如和
pnm: LINK.o=$(LINK.cc)
pnm: $(OBJ)

您也不需要 %.o: %.c 规则,因为您没有 C 源代码。

完整的 Makefile:
CXX      = /usr/bin/g++
CXXFLAGS = -Wall -pedantic -std=c++0x

OBJ = main.o pnmhandler.o pixmap.o color.o

pnm: LINK.o=$(LINK.cc)
pnm: $(OBJ)

clean::
$(RM) pnm

.PHONY: clean

关于c++11 - CFLAGS 在 Makefile 中被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26679441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com