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

Ric Klaren ric.klaren at gmail.com
Tue Sep 6 11:37:27 PDT 2005


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


More information about the antlr-interest mailing list