链接使用g ++与clang / llvm失败

时间:2011-12-22 17:10:19

标签: c++ macos g++ llvm clang

我正在尝试编译此.ccp文件。我是从https://github.com/loarabia/Clang-tutorial得到的。

// This code is licensed under the New BSD license.
// See LICENSE.txt for more details.
#include <iostream>

#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Host.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"

#include "clang/Frontend/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"

#include "clang/Basic/LangOptions.h"
#include "clang/Basic/FileSystemOptions.h"

#include "clang/Basic/SourceManager.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Basic/FileManager.h"

#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"

#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/CompilerInstance.h"


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

    clang::DiagnosticsEngine *pDiagnosticsEngine =
        new clang::DiagnosticsEngine(pDiagIDs, pTextDiagnosticPrinter);

    clang::LangOptions languageOptions;
    clang::FileSystemOptions fileSystemOptions;
    clang::FileManager fileManager(fileSystemOptions);
    clang::SourceManager sourceManager(
        *pDiagnosticsEngine,
        fileManager);
    clang::HeaderSearch headerSearch(fileManager, *pDiagnosticsEngine);

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

    clang::TargetInfo *pTargetInfo = 
        clang::TargetInfo::CreateTargetInfo(
            *pDiagnosticsEngine,
            targetOptions);
    clang::CompilerInstance compInst;

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

    return 0;
}

我使用这样的g ++编译行:

  g++ tutorial1.cpp -g -fno-rtti `llvm-config --cxxflags --ldflags --libs` -lclangBasic
 -lclangLex -lclangDriver -lclangFrontend -lclangParse -lclangAST -lclangSema
 -lclangAnalysis  -I ../llvm/tools/clang/include/ 
-I/Users/scher/Documents/Projects/task/C/last_clang/llvm/include 
-I/Users/scher/Documents/Projects/task/C/last_clang/build/include  
-I/Users/scher/Documents/Projects/task/C/last_clang/build/tools/clang/include

但它失败了以下报告:

Undefined symbols for architecture x86_64:
  "clang::serialization::ModuleManager::lookup(llvm::StringRef)", referenced from:
      clang::ASTUnit::TranslateStoredDiagnostics(clang::ASTReader*, llvm::StringRef, clang::SourceManager&, llvm::SmallVectorImpl<clang::StoredDiagnostic> const&, llvm::SmallVectorImpl<clang::StoredDiagnostic>&)in libclangFrontend.a(ASTUnit.o)
  "clang::ASTWriter::ASTWriter(llvm::BitstreamWriter&)", referenced from:
      clang::ASTUnit::serialize(llvm::raw_ostream&)      in libclangFrontend.a(ASTUnit.o)
  "clang::ASTWriter::WriteAST(clang::Sema&, clang::MemorizeStatCalls*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, clang::Module*, llvm::StringRef)", referenced from:
      clang::ASTUnit::serialize(llvm::raw_ostream&)      in libclangFrontend.a(ASTUnit.o)
  "clang::ASTWriter::~ASTWriter()", referenced from:
      clang::ASTUnit::serialize(llvm::raw_ostream&)      in libclangFrontend.a(ASTUnit.o)
  "clang::PCHGenerator::PCHGenerator(clang::Preprocessor const&, llvm::StringRef, clang::Module*, llvm::StringRef, llvm::raw_ostream*)", referenced from:
      (anonymous namespace)::PrecompilePreambleConsumer::PrecompilePreambleConsumer(clang::ASTUnit&, clang::Preprocessor const&, llvm::StringRef, llvm::raw_ostream*)in libclangFrontend.a(ASTUnit.o)
  "clang::PCHGenerator::HandleTranslationUnit(clang::ASTContext&)", referenced from:
      (anonymous namespace)::PrecompilePreambleConsumer::HandleTranslationUnit(clang::ASTContext&)in libclangFrontend.a(ASTUnit.o)
  "clang::ASTWriter::getDeclID(clang::Decl const*)", referenced from:
      (anonymous namespace)::PrecompilePreambleConsumer::HandleTranslationUnit(clang::ASTContext&)in libclangFrontend.a(ASTUnit.o)
  "clang::PCHGenerator::~PCHGenerator()", referenced from:
      (anonymous namespace)::PrecompilePreambleConsumer::~PrecompilePreambleConsumer()in libclangFrontend.a(ASTUnit.o)
  "vtable for clang::ASTReaderListener", referenced from:
      clang::ASTReaderListener::ASTReaderListener()in libclangFrontend.a(ASTUnit.o)
  "clang::ASTReaderListener::~ASTReaderListener()", referenced from:
      (anonymous namespace)::ASTInfoCollector::~ASTInfoCollector()in libclangFrontend.a(ASTUnit.o)
  "clang::PCHGenerator::GetASTMutationListener()", referenced from:
      vtable for (anonymous namespace)::PrecompilePreambleConsumerin libclangFrontend.a(ASTUnit.o)
  "clang::PCHGenerator::GetASTDeserializationListener()", referenced from:
      vtable for (anonymous namespace)::PrecompilePreambleConsumerin libclangFrontend.a(ASTUnit.o)
  "clang::serialization::ModuleManager::addInMemoryBuffer(llvm::StringRef, llvm::MemoryBuffer*)", referenced from:
      clang::ASTReader::addInMemoryBuffer(llvm::StringRef&, llvm::MemoryBuffer*)     in libclangFrontend.a(ChainedIncludesSource.o)
  "clang::ASTReader::ASTReader(clang::Preprocessor&, clang::ASTContext&, llvm::StringRef, bool, bool)", referenced from:
      clang::CompilerInstance::createPCHExternalASTSource(llvm::StringRef, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, bool, clang::Preprocessor&, clang::ASTContext&, void*, bool)in libclangFrontend.a(CompilerInstance.o)
      clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation> >, clang::Module::NameVisibilityKind, bool)in libclangFrontend.a(CompilerInstance.o)
      clang::ASTUnit::LoadFromASTFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>, clang::FileSystemOptions const&, bool, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, llvm::PointerUnion<char const*, llvm::MemoryBuffer const*> >*, unsigned int, bool)in libclangFrontend.a(ASTUnit.o)
      createASTReader(clang::CompilerInstance&, llvm::StringRef, llvm::SmallVector<llvm::MemoryBuffer*, 4u>&, llvm::SmallVector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 4u>&, clang::ASTDeserializationListener*)in libclangFrontend.a(ChainedIncludesSource.o)
  "clang::ASTReader::setDeserializationListener(clang::ASTDeserializationListener*)", referenced from:
      clang::CompilerInstance::createPCHExternalASTSource(llvm::StringRef, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, bool, clang::Preprocessor&, clang::ASTContext&, void*, bool)in libclangFrontend.a(CompilerInstance.o)
      clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation> >, clang::Module::NameVisibilityKind, bool)in libclangFrontend.a(CompilerInstance.o)
      createASTReader(clang::CompilerInstance&, llvm::StringRef, llvm::SmallVector<llvm::MemoryBuffer*, 4u>&, llvm::SmallVector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 4u>&, clang::ASTDeserializationListener*)in libclangFrontend.a(ChainedIncludesSource.o)
  "clang::ASTReader::ReadAST(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, clang::serialization::ModuleKind)", referenced from:
      clang::CompilerInstance::createPCHExternalASTSource(llvm::StringRef, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, bool, clang::Preprocessor&, clang::ASTContext&, void*, bool)in libclangFrontend.a(CompilerInstance.o)
      clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation> >, clang::Module::NameVisibilityKind, bool)in libclangFrontend.a(CompilerInstance.o)
      clang::ASTUnit::LoadFromASTFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>, clang::FileSystemOptions const&, bool, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, llvm::PointerUnion<char const*, llvm::MemoryBuffer const*> >*, unsigned int, bool)in libclangFrontend.a(ASTUnit.o)
      createASTReader(clang::CompilerInstance&, llvm::StringRef, llvm::SmallVector<llvm::MemoryBuffer*, 4u>&, llvm::SmallVector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 4u>&, clang::ASTDeserializationListener*)in libclangFrontend.a(ChainedIncludesSource.o)
  "clang::ASTReader::makeModuleVisible(clang::Module*, clang::Module::NameVisibilityKind)", referenced from:
      clang::CompilerInstance::loadModule(clang::SourceLocation, llvm::ArrayRef<std::pair<clang::IdentifierInfo*, clang::SourceLocation> >, clang::Module::NameVisibilityKind, bool)in libclangFrontend.a(CompilerInstance.o)
  "clang::ASTReader::getOriginalSourceFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, clang::FileManager&, clang::DiagnosticsEngine&)", referenced from:
      ParsePreprocessorArgs(clang::PreprocessorOptions&, clang::driver::ArgList&, clang::FileManager&, clang::DiagnosticsEngine&)in libclangFrontend.a(CompilerInvocation.o)
  "clang::ASTDeserializationListener::~ASTDeserializationListener()", referenced from:
      (anonymous namespace)::DelegatingDeserializationListener::~DelegatingDeserializationListener()in libclangFrontend.a(FrontendAction.o)
      clang::MultiplexASTDeserializationListener::~MultiplexASTDeserializationListener()in libclangFrontend.a(MultiplexConsumer.o)
  "vtable for clang::ASTDeserializationListener", referenced from:
      clang::ASTDeserializationListener::ASTDeserializationListener()in libclangFrontend.a(FrontendAction.o)
      clang::ASTDeserializationListener::ASTDeserializationListener()in libclangFrontend.a(MultiplexConsumer.o)
  "clang::PCHGenerator::PCHGenerator(clang::Preprocessor const&, llvm::StringRef, clang::Module*, llvm::StringRef, llvm::raw_ostream*)", referenced from:
      clang::GeneratePCHAction::CreateASTConsumer(clang::CompilerInstance&, llvm::StringRef)    in libclangFrontend.a(FrontendActions.o)
      clang::GenerateModuleAction::CreateASTConsumer(clang::CompilerInstance&, llvm::StringRef)    in libclangFrontend.a(FrontendActions.o)
      clang::ChainedIncludesSource::create(clang::CompilerInstance&)   in libclangFrontend.a(ChainedIncludesSource.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

我想补充一点,当您添加此clang::CompilerInstance compInst;代码行时,就会发生这种情况。如果没有这条线,它就会正常编译。

我也使用clang / llvm 3.0,它是使用本教程构建的http://clang.llvm.org/get_started.html

为什么会失败的任何建议?

2 个答案:

答案 0 :(得分:2)

您可能没有按正确的顺序列出正确的库。我不知道正确的列表是什么,但至少你错过了-lclangSerialization

答案 1 :(得分:0)

命令行中库的顺序可能有误。尝试使用项目中提供的makefile - https://github.com/loarabia/Clang-tutorial/blob/master/makefile。只需将makefile复制到您的目录并运行make。