#error “Must #define __STDC_LIMIT_MACROS before #i

2019-01-15 07:00发布

I have been trying to follow the tutorial at http://gnuu.org/2009/09/18/writing-your-own-toy-compiler/5/ (using flex, bison and llvm) but when typing the line

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

I get the following errors:

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"

I have seen lots of posts like this on the internet, and most of the answers include defining these constants on the command line or by using gcc Makefile.

I don't understand how to do that, could someone help me out?

2条回答
Explosion°爆炸
2楼-- · 2019-01-15 07:35

Based on the docs here, you should be able to fix the problem by adding the following command line options:

-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS

Afterwards, there might be some other errors:

parser.o: In function NInteger::NInteger(long long)': parser.cpp:(.text._ZN8NIntegerC2Ex[_ZN8NIntegerC5Ex]+0x23): undefined reference tovtable for NInteger' parser.o: In function NDouble::NDouble(double)': parser.cpp:(.text._ZN7NDoubleC2Ed[_ZN7NDoubleC5Ed]+0x24): undefined reference tovtable for NDouble'

Try to implement every codeGen in every class without llvm stuff (i.e., change node.h). Then you will be able to compile and run the tutorial.

By the way, when you compile the code, you might want to use llvm-config command the get the option instead of using -D option:

g++ -c `llvm-config --cppflags`  xxxx.cpp
查看更多
Melony?
3楼-- · 2019-01-15 07:40

Append this to your command line:

-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS

For more information about the -D command line option, refer to gcc's documentation on preprocessor options.

查看更多
登录 后发表回答