【问题标题】:llvm 5.0 linking error with llvm::Module::dump()llvm 5.0 与 llvm::Module::dump() 的链接错误
【发布时间】:2017-09-22 15:10:08
【问题描述】:

过去两天我试图将 LLVM 与我的 C++ 项目链接,它终于可以工作了,但问题是当我使用 dump() 方法时,它给出了链接器错误,我认为问题出在我的库上链接,所以我已经将我的可执行文件链接到所有 LLVM 库(模块)但没有成功。所以是 LLVM5.0 代码库中的错误还是我做错了什么以及我专门谈论 LLVM5.0 的原因是因为我在评论部分中读过 else where (LLVM-5.0 Makefile undefined reference fail) 没有使用 LLVM4.0 编译相同代码的问题,当然我已经搜索过其他解决方案,但什么都没有

llvm_test.cpp:

#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"

llvm::LLVMContext context;

int main(){
    llvm::Module*module = new llvm::Module("llvm-module",context);
    module->dump();
}

命令:

clang++ -O3  -Wall -std=c++11 `llvm-config --cppflags --ldflags` `llvm-config --libs core --system-libs` toy.cpp 

关于我已经链接到所有模块:

clang++ -O3 -Wall -std=c++11 `llvm-config --cxxflags --ldflags` `llvm-config --libs all --system-libs` toy.cpp 

编译器: Apple Clang 8.0.0 x86_64

操作系统: mac OS 10.12.5

提前感谢您的帮助

【问题讨论】:

  • 不幸的是,LLVM 5 不包括发布模式下的转储实现。您必须使用调试版本或使用替代功能(例如打印)。

标签: c++ llvm llvm-clang llvm-c++-api


【解决方案1】:

好的,我查看了 llvm 的代码,实际上你可以更容易地做到这一点。您所要做的就是停止使用转储,而是:

module->print(llvm::errs(), nullptr);

这正是转储内部所做的。

【讨论】:

    【解决方案2】:

    因此,在解决问题之后,它终于让我对问题进行了简单的破解我需要提醒您注意这个解决方案适用于 LLVM5.0 的问题,所以我不知道它是否适用于其他版本。

    -first: 我已经从 AsmWriter.cpp 文件中复制了llvm::LLVMContext::dump 的定义,您可以在以下位置找到各种llvm::Dump 方法的定义这个文件的总结尾我已经复制了它们以备将来使用我将写入我使用过的 LLVMDump.cpp 的内容

    LLVMDump.cpp

    #include "llvm/ADT/DenseMap.h"
    #include "llvm/ADT/STLExtras.h"
    #include "llvm/ADT/SetVector.h"
    #include "llvm/ADT/SmallString.h"
    #include "llvm/ADT/StringExtras.h"
    #include "llvm/IR/AssemblyAnnotationWriter.h"
    #include "llvm/IR/CFG.h"
    #include "llvm/IR/CallingConv.h"
    #include "llvm/IR/Constants.h"
    #include "llvm/IR/DebugInfo.h"
    #include "llvm/IR/DerivedTypes.h"
    #include "llvm/IR/IRPrintingPasses.h"
    #include "llvm/IR/InlineAsm.h"
    #include "llvm/IR/IntrinsicInst.h"
    #include "llvm/IR/LLVMContext.h"
    #include "llvm/IR/Module.h"
    #include "llvm/IR/ModuleSlotTracker.h"
    #include "llvm/IR/Operator.h"
    #include "llvm/IR/Statepoint.h"
    #include "llvm/IR/TypeFinder.h"
    #include "llvm/IR/UseListOrder.h"
    #include "llvm/IR/ValueSymbolTable.h"
    #include "llvm/Support/Debug.h"
    #include "llvm/Support/ErrorHandling.h"
    #include "llvm/Support/Format.h"
    #include "llvm/Support/FormattedStream.h"
    #include "llvm/Support/MathExtras.h"
    #include "llvm/Support/raw_ostream.h"
    #include "llvm/BinaryFormat/Dwarf.h"
    #include <algorithm>
    #include <cctype>
    using namespace llvm;
    LLVM_DUMP_METHOD
    void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
    
    // Type::dump - allow easy printing of Types from the debugger.
    LLVM_DUMP_METHOD
    void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
    
    // Module::dump() - Allow printing of Modules from the debugger.
    LLVM_DUMP_METHOD
    void Module::dump() const {
        print(dbgs(), nullptr,
                /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
    }
    
    // \brief Allow printing of Comdats from the debugger.
    LLVM_DUMP_METHOD
    void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
    
    // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
    LLVM_DUMP_METHOD
    void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
    
    LLVM_DUMP_METHOD
    void Metadata::dump() const { dump(nullptr); }
    
    LLVM_DUMP_METHOD
    void Metadata::dump(const Module *M) const {
        print(dbgs(), M, /*IsForDebug=*/true);
        dbgs() << '\n';
    }
    

    请注意,我已将旧的包含目录 #include "llvm/Support/Dwarf.h" 更改为 #include "llvm/BinaryFormat/Dwarf.h"

    -second:我已将 LLVMDUmp.cpp 编译为静态库,以便稍后在需要时链接它。

    如果您不知道如何在 mac 上创建静态链接库,因为 -static 标志不适用于 mac 上的 clang 或 gcc

    clang++ -std=c++1z -O3 -c LLVMDump.cpp -o LLVMDump.o 
    

    然后用于创建静态链接库

    ar -rv libLLVMDump.a LLVMDump.o 
    

    现在只需将 libLLVMDump.a 移动到您计算机上的 lib 目录中,以便在需要时使用它(如果您不知道应该从 llvm 安装中设置 lib 目录的位置)使用llvm配置

    llvm-config --lib-dir
    

    它会显示你应该把你的 llvm/lib 目录放在哪里。

    测试: 如果需要,您可以使用问题中的代码

    clang++ -O3 -Wall -std=c++1z -L /path/lib -lLLVMCore -lLLVMBinaryFormat -lLLVMSupport -lLLVMDemangle -lLLVMDump  llvmtest.cpp
    

    请注意我们刚刚创建的最后的 LLVMDump。

    我希望对任何人都有帮助,祝编码愉快?。

    【讨论】:

    • 这看起来很复杂。我遇到了同样的问题,我正在用 bazel 构建,我希望有更好的方法来处理这个问题:-/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多