LLVM-5.0 Makefile undefined reference fail

我是研究僧i 提交于 2019-12-23 17:17:46

问题


Including the following statement in my code

main_module->dump(); // main_module is of type llvm::Module*

causes the following linker error:

undefined reference to 'llvm::Module::dump() const'

The dump method resides in /usr/lib/llvm-5.0/include/llvm/IR/Module.h

I checked stack overflow (Using llvm::Function::dump(), linker gives "undefined reference to `llvm::Value::dump() const'"), and it seems we get this error when the linker isn't fed the libraries in correct order. However, I clearly have the libraries in the end in my compilation command:

clang++-5.0 -g -O3 main.cpp -o main llvm-config-5.0 --cxxflags --ldflags --system-libs --libs core mcjit native

Any help is appreciated.

The weird thing is, the linker figured out that the type of the dump method. It clearly went in the include file. So why would it call it an undefined reference?

Code I am trying to run: `

# include "llvm/IR/LLVMContext.h"
# include "llvm/IR/Module.h"
# include "llvm/IR/IRBuilder.h"
# include <iostream>

using namespace llvm;

static LLVMContext ctxt;
static IRBuilder<> builder(ctxt);

int main(int argc, char** argv) {

    Module* main_module = new Module("main_module", ctxt);
    std::cout << main_module->getModuleIdentifier() << "\n";

    FunctionType* func_type = FunctionType::get(builder.getInt32Ty(), false);
    Function* main_func = Function::Create(func_type,Function::ExternalLinkage, "main", main_module);

    if (main_module->getFunction("main")) {
        std::cout << "Found function!\n";
    }

    main_module->dump();  // need this for debugging and testing reasons with LLVM

    return 0;
}

回答1:


In addition to the solution that Subrat provided, you can adjust your code to avoid calling dump. You can achieve the same thing by calling:

main_module->print(llvm::outs(), nullptr);

Similarly, if you want to dump a LLVM function, you can write:

main_func->print(llvm::outs());

Actually, as of LLVM 5.0.0, this is how the dump() function is implemented.




回答2:


Seems like the definition for dump is in ASMWriter.cpp, which seems to be depracated. Also, ASMWrite.cpp's debug method refers to dbgs() which is in debug.cpp

I fixed the problem by copying over debug.cpp and the Module::dump() (from ASMWriter.cpp--since I don't need the whole code, only a specific subroutine from this file) routine and putting it in my cpp file.



来源:https://stackoverflow.com/questions/43723127/llvm-5-0-makefile-undefined-reference-fail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!