如何在使用llvm-config进行编译时修复缺少的库?

时间:2014-06-27 11:54:16

标签: clang llvm

我试图编译使用LLVM / Clang API编译的代码' hello_world'到LLVM IR:

#include <iostream>

#include <clang/Driver/Compilation.h>
#include <clang/Driver/Driver.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/Program.h>
#include <llvm/Support/raw_ostream.h>

using namespace std;
using namespace clang;
using namespace clang::driver;

int main(int argc, char** argv)
{
    std::cout << "Starting ----" << std::endl;

    // [clang -S -emit-llvm ./test/hello_world.cpp]

    // Arguments to pass to the clang driver:
    //    clang getinmemory.c -lcurl -v
    // Path to the C file
    string clangPath = "clang";

    string inputPath = "./test/hello_world.cpp";
    string outputPath = "hello_world.ll";

    vector<const char *> args;
    args.push_back(clangPath.c_str());
    args.push_back("-S");
    args.push_back("-emit-llvm");
    args.push_back(inputPath.c_str());

    // The clang driver needs a DiagnosticsEngine so it can report problems
    clang::DiagnosticOptions *Options = new clang::DiagnosticOptions();
    //clang::TextDiagnosticPrinter *DiagClient = new clang::TextDiagnosticPrinter(llvm::errs(), Options);
    clang::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(new clang::DiagnosticIDs());
    clang::DiagnosticsEngine Diags(DiagID, Options);

    // Create the clang driver
    clang::driver::Driver TheDriver(args[0], llvm::sys::getDefaultTargetTriple(), outputPath, Diags);

    // C++ code
    //TheDriver.CCCIsCXX = true;

    // Create the set of actions to perform
    clang::OwningPtr<clang::driver::Compilation> Compilation(TheDriver.BuildCompilation(args));

    // Print the set of actions
    TheDriver.PrintActions(*Compilation);

    std::cout << "Done ----" << std::endl;

    // Carry out the actions
    int Res = 0;
    SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
    if (Compilation)
        Res = TheDriver.ExecuteCompilation(*Compilation, FailingCommands);

    // Report problems
        /*
    if (Res < 0)
        TheDriver.generateCompilationDiagnostics(*Compilation, FailingCommands);
        */
    for (SmallVectorImpl< std::pair<int,
        const Command *> >::iterator it = FailingCommands.begin(),
        ie = FailingCommands.end(); it != ie; ++it) {
            int CommandRes = it->first;
            const Command *FailingCommand = it->second;
            if (!Res)
              Res = CommandRes;

            if (CommandRes < 0 || CommandRes == 70) {
              TheDriver.generateCompilationDiagnostics(*Compilation, FailingCommand);
              break;
            }
      }

    return Res;
}

我决定使用llvm-config来获取正确的编译/链接参数,但我发现链接错误(似乎-l中仍然缺少某些库):

MBA-Anton:build asmirnov$ clang++ `llvm-config --cxxflags` `llvm-config --ldflags` `llvm-config --libs all` ../clang_ir.cpp 
Undefined symbols for architecture x86_64:
  "clang::DiagnosticIDs::DiagnosticIDs()", referenced from:
      _main in clang_ir-fb4166.o
  "clang::DiagnosticIDs::~DiagnosticIDs()", referenced from:
      llvm::RefCountedBase<clang::DiagnosticIDs>::Release() const in clang_ir-fb4166.o
  "clang::DiagnosticsEngine::DiagnosticsEngine(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> const&, clang::DiagnosticOptions*, clang::DiagnosticConsumer*, bool)", referenced from:
      _main in clang_ir-fb4166.o
  "clang::DiagnosticsEngine::~DiagnosticsEngine()", referenced from:
      _main in clang_ir-fb4166.o
  "clang::driver::Compilation::~Compilation()", referenced from:
      llvm::OwningPtr<clang::driver::Compilation>::~OwningPtr() in clang_ir-fb4166.o
  "clang::driver::Driver::BuildCompilation(llvm::ArrayRef<char const*>)", referenced from:
      _main in clang_ir-fb4166.o
  "clang::driver::Driver::generateCompilationDiagnostics(clang::driver::Compilation&, clang::driver::Command const*)", referenced from:
      _main in clang_ir-fb4166.o
  "clang::driver::Driver::Driver(llvm::StringRef, llvm::StringRef, llvm::StringRef, clang::DiagnosticsEngine&)", referenced from:
      _main in clang_ir-fb4166.o
  "clang::driver::Driver::~Driver()", referenced from:
      _main in clang_ir-fb4166.o
  "clang::driver::Driver::PrintActions(clang::driver::Compilation const&) const", referenced from:
      _main in clang_ir-fb4166.o
  "clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation const&, llvm::SmallVectorImpl<std::__1::pair<int, clang::driver::Command const*> >&) const", referenced from:
      _main in clang_ir-fb4166.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
MBA-Anton:build asmirnov$ 

1 个答案:

答案 0 :(得分:0)

哦,我忘了添加clang libclang...a libs,因为llvm-config仅与llvm有关,而且它对clang一无所知。