简体   繁体   中英

Load LLVM bitcode into module: cannot convert ‘std::unique_ptr<llvm::Module>’ to ‘llvm::Module*’

I try to compile the minimal example from here

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

using namespace llvm;
int main()
{
  LLVMContext context;
  SMDiagnostic error;
  Module *m = parseIRFile("hello.bc", error, context);

  if(m)
  {
    m->dump();
  } 

  return 0;
}

using

g++ myFile.cpp `llvm-config --cxxflags --ldflags --libs all --system-libs` -std=c++11 -ldl -lpthread

and get

error: cannot convert 'std::unique_ptr' to 'llvm::Module*' in initialization

All examples and the llvm source itself everywhere uses llvm::Module *; so why do I get this error?

Note I use: LLVMVersion=3.6.0svn LLVM_CONFIGTIME= Thu Dec 18 10:51:37 CET 2014 Is it a problem with the 3.6 trunk? Should I opt for 3.5 branch?

Thx Alex

The issue is that parseIRFile gives you back a unique_ptr<Module> and there is no implicit conversion from unique_ptr<Module> to Module* (which is good!!). To fix, just use the correct type:

std::unique_ptr<Module> m = parseIRFile(..);
auto m = parseIRFile(..); // avoid all future type issues

Using unique_ptr for memory management is much smarter than using raw pointers - and this interface makes clear that you are responsible for ownership of m . This way, you don't have to remember to delete it.

If you really really want to use a raw pointer, just call release on the returned object so that it no longer owns it:

Module* m = parseIRFile(..).release();

I only present that for completeness though - really prefer to keep your object a unique_ptr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM