tags:

views:

995

answers:

2

I've been trying to learn ANTLR and get it working with C output code using this tutorial (also referenced in this question). I successfully got ANTLR to generate the lexer and parser as C source, but I cannot get them to compile using gcc on Mac OS X Snow Leopard (i686-apple-darwin10-gcc-4.2.1). Below is the result when I try to compile the "SimpleCalcLexer.c".

dyn-72-33-132-199:Desktop bf$ gcc -o lexer SimpleCalcLexer.c
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
  "_antlr3LexerNewStream", referenced from:
      _SimpleCalcLexerNewSSD in ccjXa6NU.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

The SimpleCalcLexer.c file does not reference "main" anywhere (nor is it defined), but the parser does define it, so I tried to compile that:

dyn-72-33-132-199:Desktop bf$ gcc -o parser SimpleCalcParser.c
Undefined symbols:
  "_antlr3CommonTokenStreamSourceNew", referenced from:
      _main in ccn8ZVhk.o
  "_antlr3ParserNewStream", referenced from:
      _SimpleCalcParserNewSSD in ccn8ZVhk.o
  "_SimpleCalcLexerNew", referenced from:
      _main in ccn8ZVhk.o
  "_antlr3AsciiFileStreamNew", referenced from:
      _main in ccn8ZVhk.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

So several questions:
1) What am I doing wrong? I'm pretty sure the libraries are being found, as there are other antlr functions and definitions being found in the code. Am I calling gcc incorrectly? (I've never compiled anything this complex on the commandline before.)
2) What is ccn8ZVhk.o? I can tell that it is an object code file, but I can't find it on my system (both locate and mdfind).

+2  A: 

You need to compile the lexer and the parser into the same executable; they work together to create a single program. Try this:

gcc -o lexer SimpleCalcLexer.c SimpleCalcParser.c -lantlr3c

That command line will compile the lexer and the parser and then link the result with the ANTLR library (the "-lantlr3c" part).

The object file ccn8ZVhk.o is part of the runtime library and is what actually calls main(). It doesn't contain user-servicable parts.

janm
Thanks! The "-lantlr3c" flag is what I was missing. I also found I needed "-arch i386", as the library is 32-bit only, and Snow Leopard gcc wants to compile 64-bit on my machine.
CajunLuke
A: 

If you compile multiple times you'll see that the object code filenames change each time, so I'm guessing they are temporary object files used before the final target is compiled and linked. I am running into the same problem and I tried specifying the architecture as 386 and 686. I am trying to compile the output of this Python3 grammar file. CajunLuke, can you post the exact command you used to compile when it worked? Here's a sample of what I have done:

WOPR:plex pokstad$ gcc -arch i686 -o lexer python3Lexer.c python3Parser.c -lantlr3c
Undefined symbols:
"_main", referenced from:
  start in crt1.10.6.o
"_python3Lexer_syntetizeEmptyString", referenced from:
  _mLEADING_WS in ccGDusga.o
"_python3Lexer_createLexerToken", referenced from:
  _mCONTINUED_LINE in ccGDusga.o
  _mLEADING_WS in ccGDusga.o
"_python3Lexer_initLexer", referenced from:
  _python3LexerNewSSD in ccGDusga.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Also, did you compile the ANTLR3C runtime differently from the usual "configure;make;make install"? I tried compiling using the 64 bit option and I had the same problem.

pokstad
I'm pretty sure I use the standard configure;make;make install sequence (I don't really remember). The errors you're getting are because you're only compiling the lexer and parser. You need to add the other two files (your lexer utils and the test program with the main() function.) Here is the command you need: $ gcc -arch i686 -o lexer python3lexer.c python3parser.c python3lexerutils.c test_cpython3.c -lantlr3c What you had was essentially what I used, but mine included the main() function in the lexer. This is assuming all the files are in the same folder, too.
CajunLuke
Awesome, thanks a ton!
pokstad
BTW, using your provided command I was able to successfully compile without specifying an architecture. The trick is to configure the ANTLR3C runtime to be 64 bit enabled before compiling by using the following command: "./configure --enable-64bit" I had some warnings for some int data types being the wrong size but other than that it compiled successfully. Wondering how to make a 64 bit app like this work on 32 bit machines since Xcode lets you create 32bit/64bit Universal binaries, but I'll leave a new question for that when I get that far!
pokstad