簡體   English   中英

Antlr4 C ++目標的未定義參考問題

[英]Undefined reference problem with antlr4 c++ target

我正在上一個編譯器設計課程,並且試圖學習有關C ++目標的ANTLR4。

好吧,這就是我經歷的:

  1. 我在名為“ ArrayInit.g4”的文件中編寫了語法

  2. 使用ANTLR4工具和命令“ antlr4 -Dlanguage = Cpp -o build”對其進行編譯,並在“ build /”目錄中獲得了一堆文件,在其中我還添加了一個包含主要功能int main(){return 0;}

  3. 下載了C ++運行時,並將其放在目錄“ runtime /”中

  4. 在這一階段,我陷入了困境,無法使用命令“ 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/文件夾中它本身在包含.h和.cpp文件(我看不到.a或.so文件)的同一目錄(即runtime/ )中包括許多其他頭文件。
您可以在此處的C ++目標部分下找到運行時或直接在此處找到運行時

您的編譯命令看起來不太正確。

請注意,g ++使用大寫的-L開關來指定查找庫的位置,並使用小寫的-l來指定要鏈接的庫的文件名,但不包括lib前綴和文件后綴。 由於至少在我看到的版本中,antlr提供了一個“ libantlr.a”,因此它將是-l antlr

另外,應該在程序源文件或目標文件之后提供該庫,因為在大多數情況下,鏈接器會根據它們是否提供了到目前為止已知的所需符號來決定要包括哪些庫。

因此,我不確定這是否可以解決所有問題,但前進的一步是:

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

我還隨意添加了-W標志以啟用一組默認的編譯器警告,無論您是否是該語言的新手,編寫新的C ++代碼時都必須這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM