【问题标题】:Makefile for a project项目的 Makefile
【发布时间】:2014-06-20 16:55:47
【问题描述】:

我有一个使用正则表达式的项目的 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);
                                    ^

所以我不明白出了什么问题,请您帮帮我吗?

【问题讨论】:

    标签: c++ regex c++11 g++


    【解决方案1】:

    问题是您的compile 目标已经依赖于目标文件!但是它们还没有构建,所以 make 尝试找到构建它们的规则。因为您还没有定义自定义规则,所以它使用implicit rule:

    编译 C++ 程序 n.o 是自动从 n.cc、n.cpp 或 n.C 使用“$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c”形式的配方。我们 鼓励您对 C++ 源文件使用后缀“.cc”,而不是 ‘.C’。

    此规则当然不会使用您在CC 中设置的标志。这就解释了为什么make 打印的gcc 命令行与您的CC 不匹配。您可以通过使用--no-builtin-rules 运行 make 来测试它,因为这应该禁用所有隐式规则。

    在这种情况下,一个好主意是使用-d--debug 运行make,我认为这应该会显示规则评估。

    【讨论】:

    • 旁注,您应该始终使用 --no-builtin-rules IMO。
    【解决方案2】:

    不要使用c 宏,而是使用c++

    CXX= g++-4.9
    CXXFLAGS=  -std=c++11 -Wall -pedantic -Wno-long-long -O0 -ggdb
    

    然后默认规则应该会选择它们,您需要在 makefile 中键入 less。

    另见 Make 使用的complete list of variables(宏)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-21
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2015-03-22
      • 2020-04-25
      相关资源
      最近更新 更多