[antlr-interest] Problem linking C++ generated parser

Mark Kattenbelt mark.kattenbelt at gmail.com
Tue Sep 6 12:40:50 PDT 2005


Ric Klaren wrote:

>Mark Kattenbelt wrote:
>  
>
>>I get pretty much the same errors when building in mingw (via dev-cpp) I
>>cannot seem to build the library in the same program, but have managed
>>in another mingw install, though a slightly different version.
>>
>>If you manage to solve it perhaps you could help me out with explaining
>>how, or if someone knows a step by step guide of how to build antlr
>>solutions in dev-cpp that would be lovely too.
>>    
>>
>
>First make sure you have no old .o or .a files around.
>
>cd <antlr-distro>/lib/cpp
>g++ -I. -c src/*.cpp
>rm dll.o
>ar ruv libantlr.a *.o
>ranlib libantlr.a
>
>This should get you a library that's compatible with your compiler.
>
>cd <antlr-distro>/examples/cpp/calc
>antlr calc.g
>
>(or java -classpath <path-to-antlr.jar>/antlr.jar antlr.Tool calc.g add
>extra classpath entries were needed sometimes rt.jar from the jdk/jre is
>necessary)
>
>g++ -o calc Calc*.cpp Main.cpp -I. -I../../../lib/cpp -L../../../lib/cpp
>-lantlr
>
>calc.exe < test.in
> ( + 3 ( * 4 5 ) )
>value is 23
>
>If this doesn't work then your mingw/dev-cpp/gcc/g++ installation is
>broken. Above all make sure to remove any old .o files. Due to name
>mangling changes in various g++'s you can't link objects of certain
>g++'s together. e.g. you get link errors.
>
>You can track stuff like it down with tools like
>nm -C <objectfile|libraryfile>
>
>That should get you a listing of all the defined symbols in an object
>file using the default demangling scheme for you g++ installation. If
>you get discrepancies between your object files (from the example) and
>the antlr library then you know you're in trouble. (check the manpages)
>
>Some gotchas:
>- stale .o files in your build
>- mix objects of different g++ versions.
>- linking order is significant
>- link with g++ *not* with ld
>- don't specify -lstdc++ g++ does this automatically if it's correctly
>installed. Depending on architecture and installation you may grab an
>incorrect library. Check the g++ manpage for the various -print-xx
>options that give you the spots where gcc expects to get files/linkers from.
>
>HTH,
>
>Ric
>

Cheers for that Ric!

I've managed to compile the libantlr.a with dev-cpp by just making a new 
project (static lib) including the antlr-2.7.5/lib/cpp/ in the include 
dirs, and changing the output file. For some reason this didn't work 
before. Also it didn't won't work unless I did recompile, but I only 
need it compiled once.
My project compiled fine with this library. So finally I'm on my way here!

I do think perhaps a bit more explanation in the documentation of antlr 
would have helped! I'm not at all a c++ guru so it's easy to get stuck.

Cheers though,

Mark


More information about the antlr-interest mailing list