我正在上一本编译器设计类(class),并且试图学习有关C++目标的ANTLR4。

好吧,这就是我所经历的:

  • 我将语法写在名为“ArrayInit.g4”的文件中。
  • 使用ANTLR4工具和命令“antlr4 -Dlanguage = Cpp -o build”对其进行编译,并在“build /”目录中获得了一堆文件,在其中我还添加了一个包含主要功能int main(){return 0;}
  • 的简单文件
  • 下载了C++运行时,并将其放在目录“runtime /”中
  • 在这一阶段,我陷入了困境,无法使用命令“g++ -std = c++ 11 -I ../runtime -o exec * .cpp”编译.cpp文件,并且收到以下错误消息:
  • /tmp/cc8VNGfx.o: In function `ArrayInitLexer::ArrayInitLexer(antlr4::CharStream*)':
    ArrayInitLexer.cpp:(.text+0xc8): undefined reference to `antlr4::Lexer::Lexer(antlr4::CharStream*)'
    ArrayInitLexer.cpp:(.text+0x116): undefined reference to `antlr4::atn::LexerATNSimulator::LexerATNSimulator(antlr4::Lexer*, antlr4::atn::ATN const&, std::vector<antlr4::dfa::DFA, std::allocator<antlr4::dfa::DFA> >&, std::unordered_set<std::shared_ptr<antlr4::atn::PredictionContext>, antlr4::atn::PredictionContextHasher, antlr4::atn::PredictionContextComparer, std::allocator<std::shared_ptr<antlr4::atn::PredictionContext> > >&)'
    /tmp/cc8VNGfx.o: In function `ArrayInitLexer::Initializer::Initializer()':
    ArrayInitLexer.cpp:(.text+0x376): undefined reference to `antlr4::dfa::Vocabulary::getLiteralName[abi:cxx11](unsigned long) const'
    ArrayInitLexer.cpp:(.text+0x3a6): undefined reference to `antlr4::dfa::Vocabulary::getSymbolicName[abi:cxx11](unsigned long) const'
    ArrayInitLexer.cpp:(.text+0x4b2): undefined reference to `antlr4::atn::ATNDeserializer::ATNDeserializer()'
    ArrayInitLexer.cpp:(.text+0x4d2): undefined reference to `antlr4::atn::ATNDeserializer::deserialize(std::vector<unsigned short, std::allocator<unsigned short> > const&)'
    ArrayInitLexer.cpp:(.text+0x4e8): undefined reference to `antlr4::atn::ATN::operator=(antlr4::atn::ATN&&)'
    ArrayInitLexer.cpp:(.text+0x4f7): undefined reference to `antlr4::atn::ATN::~ATN()'
    ArrayInitLexer.cpp:(.text+0x503): undefined reference to `antlr4::atn::ATN::getNumberOfDecisions() const'
    ArrayInitLexer.cpp:(.text+0x551): undefined reference to `antlr4::atn::ATN::getDecisionState(unsigned long) const'
    ArrayInitLexer.cpp:(.text+0x598): undefined reference to `antlr4::atn::ATNDeserializer::~ATNDeserializer()'
    ArrayInitLexer.cpp:(.text+0x601): undefined reference to `antlr4::atn::ATNDeserializer::~ATNDeserializer()'
    /tmp/cc8VNGfx.o: In function `__static_initialization_and_destruction_0(int, int)':
    ArrayInitLexer.cpp:(.text+0x6ec): undefined reference to `antlr4::atn::ATN::ATN()'
    ArrayInitLexer.cpp:(.text+0x701): undefined reference to `antlr4::atn::ATN::~ATN()'
    ArrayInitLexer.cpp:(.text+0xee6): undefined reference to `antlr4::dfa::Vocabulary::Vocabulary(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)'
    ArrayInitLexer.cpp:(.text+0xefb): undefined reference to `antlr4::dfa::Vocabulary::~Vocabulary()'
    /tmp/cc8VNGfx.o: In function `antlr4::Lexer::~Lexer()':
    ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0xf): undefined reference to `vtable for antlr4::Lexer'
    ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x1d): undefined reference to `vtable for antlr4::Lexer'
    ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x7d): undefined reference to `antlr4::TokenSource::~TokenSource()'
    ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x89): undefined reference to `antlr4::Recognizer::~Recognizer()'
    /tmp/cc8VNGfx.o: In function `void __gnu_cxx::new_allocator<antlr4::dfa::DFA>::construct<antlr4::dfa::DFA, antlr4::atn::DecisionState*, unsigned long&>(antlr4::dfa::DFA*, antlr4::atn::DecisionState*&&, unsigned long&)':
    ArrayInitLexer.cpp:(.text._ZN9__gnu_cxx13new_allocatorIN6antlr43dfa3DFAEE9constructIS3_JPNS1_3atn13DecisionStateERmEEEvPT_DpOT0_[_ZN9__gnu_cxx13new_allocatorIN6antlr43dfa3DFAEE9constructIS3_JPNS1_3atn13DecisionStateERmEEEvPT_DpOT0_]+0x60): undefined reference to `antlr4::dfa::DFA::DFA(antlr4::atn::DecisionState*, unsigned long)'
    /tmp/cc8VNGfx.o: In function `void std::_Construct<antlr4::dfa::DFA, antlr4::dfa::DFA>(antlr4::dfa::DFA*, antlr4::dfa::DFA&&)':
    ArrayInitLexer.cpp:(.text._ZSt10_ConstructIN6antlr43dfa3DFAEJS2_EEvPT_DpOT0_[_ZSt10_ConstructIN6antlr43dfa3DFAEJS2_EEvPT_DpOT0_]+0x44): undefined reference to `antlr4::dfa::DFA::DFA(antlr4::dfa::DFA&&)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x38): undefined reference to `antlr4::Recognizer::getTokenTypeMap[abi:cxx11]()'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x40): undefined reference to `antlr4::Recognizer::getRuleIndexMap[abi:cxx11]()'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x48): undefined reference to `antlr4::Recognizer::getTokenType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x60): undefined reference to `antlr4::Recognizer::getErrorHeader[abi:cxx11](antlr4::RecognitionException*)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x68): undefined reference to `antlr4::Recognizer::getTokenErrorDisplay[abi:cxx11](antlr4::Token*)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x70): undefined reference to `antlr4::Recognizer::addErrorListener(antlr4::ANTLRErrorListener*)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x78): undefined reference to `antlr4::Recognizer::removeErrorListener(antlr4::ANTLRErrorListener*)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x80): undefined reference to `antlr4::Recognizer::removeErrorListeners()'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x88): undefined reference to `antlr4::Recognizer::getErrorListenerDispatch()'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x90): undefined reference to `antlr4::Recognizer::sempred(antlr4::RuleContext*, unsigned long, unsigned long)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x98): undefined reference to `antlr4::Recognizer::precpred(antlr4::RuleContext*, int)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xa0): undefined reference to `antlr4::Recognizer::action(antlr4::RuleContext*, unsigned long, unsigned long)'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xa8): undefined reference to `antlr4::Recognizer::getState() const'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xb8): undefined reference to `antlr4::Lexer::getInputStream()'
    /tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xc0): undefined reference to `antlr4::Lexer::setInputStream(antlr4::IntStream*)'
    
    

    我对C++完全陌生,也许我跳过了一个基本概念。

    Edit1:我正在使用Linux

    编辑2:
    当我执行命令antlr4 -Dlanguage=Cpp -o build时,我得到了一堆.cpp和.h文件,它们是:ArrayInitLexer.h,其中许多包含antlr4-runtime.h。此头文件存在于runtime/文件夹中,该文件夹本身包含同一目录中的许多其他头文件.ie runtime/),其中包含.h和.cpp文件(我看不到.a或.so文件)。
    您可以在C++目标部分下找到运行时here或直接从here中找到

    最佳答案

    您的编译命令看起来不太正确。

    请注意,g++使用一个大写的-L开关来指定查找库的位置,并使用一个小写的-l来指定要链接的库的文件名,但不包括lib前缀和文件后缀。由于至少在我看到的版本中,antlr提供了一个“libantlr.a”,因此该文件为-l antlr

    另外,应该在程序源文件或目标文件之后给出该库,因为在大多数情况下,链接器根据它们是否提供了到目前为止所看到的所需的任何符号来决定要包括哪些库。

    因此,我不确定这是否可以解决所有问题,但前进的一步是:

    g++ -std=c++11 -Wall -Wextra *.cpp -L ../runtime -l antlr -o exec
    

    我还随意添加了-W标志以启用默认的编译器警告集,无论您是否是该语言的新手,编写新的C++代码时都必须这样做。

    关于c++ - Antlr4 C++目标的未定义引用问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54621811/

    10-11 00:33