15

我一直在尝试遵循http://gnuu.org/2009/09/18/writing-your-own-toy-compiler/5/上的教程(使用 flex、bison 和 llvm),但是在输入该行时

g++ -o parser parser.cpp tokens.cpp main.cpp

我收到以下错误:

In file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,
                 from /usr/local/include/llvm/ADT/PointerIntPair.h:17,
                 from /usr/local/include/llvm/IR/Use.h:28,
                 from /usr/local/include/llvm/IR/Value.h:17,
                 from node.h:3,
                 from parser.y:2:
/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error "Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h"
/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error "Must #define __STDC_CONSTANT_MACROS before " "#including Support/DataTypes.h"
parser.y: In function ‘void yyerror(const char*)’:
parser.y:6:58: erreur: ‘printf’ was not declared in this scope
In file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,
                 from /usr/local/include/llvm/ADT/PointerIntPair.h:17,
                 from /usr/local/include/llvm/IR/Use.h:28,
                 from /usr/local/include/llvm/IR/Value.h:17,
                 from node.h:3,
                 from tokens.l:3:
/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error "Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h"
/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error "Must #define __STDC_CONSTANT_MACROS before " "#including Support/DataTypes.h"
In file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,
                 from /usr/local/include/llvm/ADT/PointerIntPair.h:17,
                 from /usr/local/include/llvm/IR/Use.h:28,
                 from /usr/local/include/llvm/IR/Value.h:17,
                 from node.h:3,
                 from main.cpp:2:
/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error "Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h"
/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error "Must #define __STDC_CONSTANT_MACROS before " "#including Support/DataTypes.h"

我在互联网上看到过很多这样的帖子,大多数答案都包括在命令行上定义这些常量或使用 gcc Makefile。

我不明白该怎么做,有人可以帮我吗?

4

2 回答 2

13

将此附加到您的命令行:

-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS

有关-D命令行选项的更多信息,请参阅gcc 关于预处理器选项的文档

于 2013-09-23T18:36:11.327 回答
8

根据此处的文档,您应该能够通过添加以下命令行选项来解决问题:

-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS

之后,可能还有其他一些错误:

parser.o:在NInteger::NInteger(long long)': parser.cpp:(.text._ZN8NIntegerC2Ex[_ZN8NIntegerC5Ex]+0x23): undefined reference toNInteger 的函数 vtable 中 parser.o:在 NDouble 的函数NDouble::NDouble(double)': parser.cpp:(.text._ZN7NDoubleC2Ed[_ZN7NDoubleC5Ed]+0x24): undefined reference tovtable 中

尝试在没有任何东西的情况下在每个类中实现每个codeGenllvm(即 change node.h)。然后您将能够编译和运行本教程。

顺便说一句,当您编译代码时,您可能希望使用llvm-config命令获取选项而不是使用-D选项:

g++ -c `llvm-config --cppflags`  xxxx.cpp
于 2013-12-23T03:00:59.490 回答