我有一个使用正则表达式的项目的 Makefile。因此我需要使用 g++-4.9 和 c++11。
BIN = bin
OBJ = src/main.o src/time2.o src/except.o src/except2.o src/struct.o src/index.o
FLAGS = -lncurses
CC = g++-4.9 -std=c++11 -Wall -pedantic -Wno-long-long -O0 -ggdb
all: compile
run: compile
./$(BIN)
clean:
rm -f $(BIN) $(OBJ)
compile: $(OBJ)
$(CC) -o $(BIN) $(OBJ) $(FLAGS)
但是当我尝试进行编译时:
g++ -c -o src/main.o src/main.cpp In file included from /usr/include/c++/4.8/regex:35:0, from src/time2.h:16, from src/main.cpp:3: /usr/include/c++/4.8/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. #error This file requires compiler and library support for the \ ^ In file included from src/main.cpp:5:0: src/struct.h:99:18: error: ‘regex’ has not been declared bool isnamegood(regex x,const string& name); ^
所以我不明白哪里出了问题,你能帮帮我吗?
最佳答案
问题是您的compile
目标已经依赖于目标文件!但是它们还没有构建,所以 make 尝试找到一个规则来构建它们。因为您尚未定义自定义规则,所以它使用 implicit rule :
Compiling C++ programs n.o is made automatically from n.cc, n.cpp, or n.C with a recipe of the form ‘$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’. We encourage you to use the suffix ‘.cc’ for C++ source files instead of ‘.C’.
此规则当然不会使用您在CC
中设置的标志。这解释了为什么 make
打印的 gcc
命令行与您的 CC
不匹配。您可以通过使用 --no-builtin-rules
运行 make 来测试它,因为这应该禁用所有隐式规则。
在这种情况下,一个好主意是使用 -d
或 --debug
运行 make,我认为这应该显示规则评估。
关于c++ - 项目的 Makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24332189/