【问题标题】:Why is GNU make ignoring or changing my g++ option (in command part)?为什么 GNU make 忽略或更改我的 g++ 选项(在命令部分)?
【发布时间】:2013-11-22 18:21:13
【问题描述】:

在使用 g++ 4.4.7 编译我的 C++ 代码时,我使用 -std=c++0x 选项,因为有一些代码如下:

typedef enum Qualifier {AUTO, CONST, REG, STATIC, VOLATILE} Qualifier;
...
Qualifier q = Qualifier::AUTO;

我可以使用带有此选项的 g++ 来编译我的源文件。但是当我将 g++ 命令放入我的 makefile 中时,它不起作用! GNU make 在执行 g++ 命令时只是忽略了这个选项(它也忽略了 .o 文件的输出文件夹并将它们放在当前文件夹中)!

我的 GNU make 的版本是 3.81,我的 makefile 的一部分是:

VPATH = src:obj

all : CCompiler 

CCompiler : main.o  CodeGenerator.o  FileWraper.o  ir.tab.o \
            lex.ir.o  lex.yy.o  Symbol.o  SymbolTable.o  yacc.tab.o
    g++ -std=c++0x -o bin/CCompiler  obj/*.o

    main.o : src/main.cpp \
        src/FileWraper.h  src/Log.h  src/SymbolTable.h  src/Symbol.h  src/CodeGenerator.h
        g++ -std=c++0x -c -o ./obj/main.o src/main.cpp

    CodeGenerator.o : src/CodeGenerator.cpp \
        src/CodeGenerator.h  src/Symbol.h  src/SymbolTable.h
        g++ -std=c++0x -c -o ./obj/CodeGenerator.o src/CodeGenerator.cpp

我的项目目录是这样组织的:

    CCompiler/
        Makefile
        src/
        bin/
        obj/

当我执行 make 时,shell 向我显示了这个:

[root@cn CCompiler]$  make
g++    -c -o main.o src/main.cpp
In file included from src/SymbolTable.h:6,
                 from src/main.cpp:4:
src/Symbol.h:112: error: ‘Qualifier’ is not a class or namespace
make: *** [main.o] Error 1

为什么会这样,我该如何解决?

非常感谢!

【问题讨论】:

  • 尝试将-std=c++0x 更改为-std=c++11。这给我带来了麻烦。
  • 部分问题是您为main.o 指定了规则,而不是obj/main.o。同样,您的依赖是main.o,而不是obj/main.o。您应该考虑为obj/%.o: src/%.c 编写模式规则。单独指定特定于对象的依赖项。还可以考虑将 C++ 编译器标志拆分为 CXXFLAGS 变量。
  • @KepaniHaole 谢谢~ 但是把 0x 改成 11 后显示的内容一样...
  • @JoeZ 感谢您的建议,我会试一试。希望能解决输出目录的问题。

标签: c++ gcc c++11 enums makefile


【解决方案1】:

你想要一个enum class Qualifier { ... }。此外,typedef 在 C++ 中是多余的。

没有enum class,枚举器将以AUTO 的形式提供(即,没有Qualifier:: 前缀)。

【讨论】:

    猜你喜欢
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2012-03-16
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多