使用llvm库构建时如何链接? [英] How do I link when building with llvm libraries?

查看:439
本文介绍了使用llvm库构建时如何链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析LLVM-IR文件(.ll)并进行静态分析.

I am trying to parse a LLVM-IR file(.ll) and do a static analysis..

我在下面找到了此示例代码,并尝试构建它,但是我不知道要链接哪个库.

I found this sample code below, and I tried to build it, but I don't know which library to link.

#include <iostream>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

int main(int argc, char** argv)
{
    if (argc < 2) {
        errs() << "Expected an argument - IR file name\n";
        exit(1);
    }

    LLVMContext &Context = getGlobalContext();
    SMDiagnostic Err;
    std::unique_ptr<Module> Mod = parseIRFile(argv[1], Err, Context);

    if (Mod) {
        std::cout << "Mod is not null" << std::endl;
    }
    else {
       std::cout << "Mod is null" << std::endl;
    }
    return 0;
}

我给了下面的命令来构建,它给了我一些未定义的参考错误,我认为这是一个链接错误.

I gave the below command to build, and it gives me some undefined reference error, which that I think is a linking error.

g++ -I~/llvm/build/include -I~/llvm/llvm/include  -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -std=c++11 test.cpp

为了构建此示例代码,我必须将哪个库文件与-L选项链接?我希望它可以作为独立的二进制文件而不是整个编译过程中的传递文件.

Which library file should I have to link with -L option in order to build this sample code? I want this to work as a standalone binary, not as a pass within the whole compile procedure.

推荐答案

如果将项目设置为llvm的子项目,则可以阅读本教程示例.

If you setup your project as a subproject of llvm, you may read this tutorial and examples.

但是由于您提到了独立",所以我想您尝试从llvm源项目中构建项目. llvm-config是你的朋友.

But since you mentioned "standalone" I guess you tried to build your project out of llvm source project. llvm-config is your friend.

例如,您可以在 tools/llvm-链接Makefile内容为:

For example, you can find that inside tools/llvm-link's Makefile reads:

LINK_COMPONENTS := linker bitreader bitwriter asmparser irreader

或在CMakeLists.txt中:

set(LLVM_LINK_COMPONENTS
  BitWriter
  Core
  IRReader
  Linker
  Support
  )

然后,您可以使用llvm-config查看如何链接这些库.

Then you can use llvm-config to see how to link these libs.

$ llvm-config --libs linker bitreader bitwriter asmparser irreader

$ llvm-config --libs BitWriter Core IRReader Linker Support

他们将输出链接选项,例如:

They will output the link options like:

-lLLVMLinker -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMMC -lLLVMIRReader -lLLVMBitReader -lLLVMAsmParser -lLLVMBitWriter -lLLVMCore -lLLVMSupport

llvm-config --components可用于查看所有官方组件;如果您对指定组件感到厌倦,只需使用llvm-config --libs,它将发出 all 可链接库.

llvm-config --components can be used to see all the official components; if you are tired with specifying components, simply use llvm-config --libs and it will emit all linkable libraries.

当然,您首先应该确保库目录位于链接路径中,这就是结果llvm-config --libdir.

Of course you should firstly make sure the library directory is in your link path, which is the result llvm-config --libdir.

您可以将llvm-config --help用于其他有用的选项.

You can use llvm-config --help for other useful options.

这篇关于使用llvm库构建时如何链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆