[antlr-interest] ASTRefCount and Dynamic Libraries (c++).

Mark Kattenbelt mark.kattenbelt at gmail.com
Wed Mar 22 01:54:52 PST 2006

Hello all,

I most likely am wrong but I think I may have found an issue in Antlr
2.7.6 with MSVC 8.0 when compiling antlr into a dynamic library. The
issue is the following: Class templates can typically not be included in
dynamic libraries, as it is not known with which types they will be
instantiated. They should be defined totally in the headers, so that
projects using the library can include the instantations themselves.

Now there is a class in the antlr library defined as:

template<class T>
    class ANTLR_API ASTRefCount

Which is rightfully implemented in the header only. But when someone
compiles this into a DLL file this definition is typically not included
in the exported symbols.

But say you implement your custom AST class and include an instantiation
of ASTRefCount (like i did)

#include <antlr/CommonAST.hpp>
#include <antlr/ASTFactory.hpp>
#include <antlr/ASTRefCount.hpp>

class PromelaAST;

typedef antlr::ASTRefCount<PromelaAST> RefPromelaAST;
template class antlr::ASTRefCount<PromelaAST>;

Then there is a problem, because the ANTLR_IMPORTS directive means that
ANTLR_API is expanded to:


Meaning that the linker should look in the antlr library for the
antlr::ASTRefCount<PromelaAST> symbol, which is incorrect I think (it is
fine for all normal classes and functions, but not for class templates,
function templates or member templates).
Solution: All is well when ANTLR_API is removed from the ASTRefCount
class template definition.

Here in terms of code what goes wrong (just as an indication of the
errors received):


Feel free to prove me wrong and tell me what I should have done
different (for I am not an expert).


Mark Kattenbelt

More information about the antlr-interest mailing list