0

I am trying to compile my own ANTLR4 parser in C++

#include "nibble.h"

using namespace antlr4;
using namespace nibble;

int main(int argc, const char* argv[]){

    const std::string file_name = argv[1];
    ANTLRInputStream input(file_name);

    return 0;
}

but when I compile it by typing g++ nibblec.cpp -o nibblec -I /usr/local/include/antlr4-runtime I get

/usr/bin/ld: /tmp/ccqG8KT4.o: warning: relocation against '_ZTVN6antlr416ANTLRInputStreamE' in read-only section '.text._ZN6antlr416ANTLRInputStreamD2Ev[_ZN6antlr416ANTLRInputStreamD5Ev]'
/usr/bin/ld: /tmp/ccqG8KT4.o: in function 'main':
nibblec.cpp:(.text+0x64): undefined reference to 'antlr4::ANTLRInputStream::ANTLRInputStream(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccqG8KT4.o: in function 'antlr4::ANTLRInputStream::~ANTLRInputStream()':
nibblec.cpp:(.text._ZN6antlr416ANTLRInputStreamD2Ev[_ZN6antlr416ANTLRInputStreamD5Ev]+0xf): undefined reference to `vtable for antlr4::ANTLRInputStream'
/usr/bin/ld: nibblec.cpp:(.text._ZN6antlr416ANTLRInputStreamD2Ev[_ZN6antlr416ANTLRInputStreamD5Ev]+0x42): undefined reference to 'antlr4::CharStream::~CharStream()'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

What is ld? And why I get all this warnings? Am I doing something wrong with ANTLR4?

EDIT: I am using ANTLR 4.9.1

orlp
  • 112,504
  • 36
  • 218
  • 315
Nullndr
  • 1,624
  • 1
  • 6
  • 24
  • 1
    See also: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix. – orlp Feb 04 '21 at 00:26

1 Answers1

1

You are not linking to ANTLR. Likely you need to add -lantlr to your compile flags (although the exact spelling might be different, and you may need to specify a library directory containing ANTLR).

orlp
  • 112,504
  • 36
  • 218
  • 315