3

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;
}
Yolo Voe
  • 495
  • 6
  • 22
  • 1
    Shouldn't "llvm-config-5.0 --cxxflags --ldflags --system-libs --libs" be in backquotes -- e.g. "\`llvm-config-5.0 --cxxflags --ldflags --system-libs --libs\`"? – G.M. May 01 '17 at 17:33
  • The linker has nothing to do with header files. The compiler has, and it outputs the names of the functions and methods you use into the object file so that linker can link them - or tell you which ones are undefined, when it can't. Your object file contains a call to `llvm::Module::dump() const`. That's what the linker knows. So the linker needs a definition of that method, to fulfill that call, and can't find one. – Mike Kinghan May 01 '17 at 17:46
  • @G.M. It is supposed to be backquotes, just that ` also stands for code block in stack overflow, and \ didn't really work as an escape key. – Yolo Voe May 01 '17 at 18:00
  • @MikeKinghan Do you want to turn your comment into an answer so I can make it the accepted answer? – Yolo Voe May 01 '17 at 18:01
  • It's just a comment for your information. It's not an answer because it doesn't explain your linkage failure or how to fix it. – Mike Kinghan May 01 '17 at 18:08
  • To get effective help with this you will need to provide an [mcve] - a *reproducible* specimen of the problem. – Mike Kinghan May 01 '17 at 18:11
  • Ok, with hindsight I should probably have guessed that it was a formatting issue. The symbol that's undefined should, I think, be in libLLVMCore.so. Can you check your link line to make sure you're pulling that in. – G.M. May 01 '17 at 18:12
  • @G.M. I don't think libLLVMCore.so is there. Can you tell me how I am supposed to get it there please? – Yolo Voe May 01 '17 at 18:25
  • @MikeKinghan I updated my question with code. – Yolo Voe May 01 '17 at 18:26
  • Your program compiles and links for me with LLVM/clang++ 4.0 rather than 5.0. Otherwise identical commandline. [LLVM 5.0 is not yet officially released](http://llvm.org/). You may be better of with 4.0 than a pre-release build. – Mike Kinghan May 01 '17 at 18:43
  • @MikeKinghan Yeah. I found a hacky workaround. – Yolo Voe May 01 '17 at 18:47

2 Answers2

4

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.

3

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.

Yolo Voe
  • 495
  • 6
  • 22