[cfe-dev] linker error with ASTConsumer

jignesh vasoya jigneshvasoya at gmail.com
Mon Jan 31 11:19:14 PST 2011


Hi,
when i am trying to compile given source it gives linker error :

*tutorial4.o:(.rodata._ZTIN5clang13MyASTConsumerE[typeinfo for
clang::MyASTConsumer]+0x10): undefined reference to `typeinfo for
clang::ASTConsumer'
collect2: ld returned 1 exit status
make: *** [tutorial4] Error 1
*

Source code is: all necessary header files are included and there is no
syntax error in this source code, required libraries are also specified in
make file.
----------------------

*class MyASTConsumer : public clang::ASTConsumer
{
public:
    MyASTConsumer() : clang::ASTConsumer() { }
    virtual ~MyASTConsumer() { }

    virtual void HandleTopLevelDecl( clang::DeclGroupRef d)
    {
        static int count = 0;
        clang::DeclGroupRef::iterator it;
        for( it = d.begin(); it != d.end(); it++)
        {
            count++;
            clang::VarDecl *vd = dyn_cast<clang::VarDecl>(*it);
            if(!vd)
            {
                continue;
            }
            std::cout << vd << std::endl;
            if( vd->isFileVarDecl() && vd->hasExternalStorage() )
            {
                std::cerr << "Read top-level variable decl: '";
                std::cerr << vd->getDeclName().getAsString() ;
                std::cerr << std::endl;
            }
        }
    }
};


int main()
{
    clang::DiagnosticOptions diagnosticOptions;
    clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
        new clang::TextDiagnosticPrinter(llvm::outs(),diagnosticOptions);
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
    clang::Diagnostic diagnostic(pDiagIDs, pTextDiagnosticPrinter);

    clang::LangOptions languageOptions;
    clang::FileSystemOptions fileSystemOptions;
    clang::FileManager fileManager(fileSystemOptions);

    clang::SourceManager sourceManager( diagnostic, fileManager);
    clang::HeaderSearch headerSearch(fileManager);

    clang::HeaderSearchOptions headerSearchOptions;

    headerSearchOptions.AddPath("/usr/include/linux",
clang::frontend::Angled,
            false,    false,    false);
    headerSearchOptions.AddPath("/usr/include/c++/4.4/tr1",
clang::frontend::Angled,
            false,    false,    false);

headerSearchOptions.AddPath("/usr/include/c++/4.4",clang::frontend::Angled,
            false,false,    false);


    clang::TargetOptions targetOptions;
    targetOptions.Triple = llvm::sys::getHostTriple();

    clang::TargetInfo *pTargetInfo =
        clang::TargetInfo::CreateTargetInfo(diagnostic,targetOptions);

    clang::ApplyHeaderSearchOptions(headerSearch,headerSearchOptions,
        languageOptions,pTargetInfo->getTriple());

    clang::Preprocessor preprocessor(diagnostic,    languageOptions,
        *pTargetInfo,sourceManager,headerSearch);

    clang::PreprocessorOptions preprocessorOptions;
    clang::FrontendOptions frontendOptions;
    clang::InitializePreprocessor(preprocessor,preprocessorOptions,
        headerSearchOptions,frontendOptions);

    const clang::FileEntry *pFile = fileManager.getFile( "test.c");
    sourceManager.createMainFileID(pFile);
    preprocessor.EnterMainSourceFile();

    const clang::TargetInfo &targetInfo = *pTargetInfo;

    clang::IdentifierTable identifierTable(languageOptions);
    clang::SelectorTable selectorTable;

    clang::Builtin::Context builtinContext(targetInfo);
    clang::ASTContext astContext( languageOptions, sourceManager,
targetInfo, identifierTable,
        selectorTable, builtinContext,   0 /* size_reserve*/);
    clang::MyASTConsumer astConsumer;

    clang::Sema sema(preprocessor, astContext,  astConsumer);


     pTextDiagnosticPrinter->BeginSourceFile(languageOptions,
&preprocessor);
    clang::ParseAST(preprocessor, &astConsumer, astContext);
    pTextDiagnosticPrinter->EndSourceFile();


    return 0;
}*

*can anybody tell me what is wrong with this.

___________________________________________________________________________*
-----------------------------------------------------------------------------------------------------------------------------
*Jignesh Vasoya
CSE - IIT MADRAS
9962248684*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110201/68877537/attachment.html>


More information about the cfe-dev mailing list