Ratnesh Tiwari via llvm-dev
2018-Aug-12 06:12 UTC
[llvm-dev] Problem in taking llvm ir file via command line argument
Hi, I am writing a program here hello.cpp for taking LLVM IR as a command line argument. My IR file is "input.bc" . But I dont understand how it make it via command line argument. I am running this hello.cpp as: g++ hello.cpp -I /tmp/llvm/include/ -std=c++11 input.bc But it shows error. Please suggest me correct way of taking llvm ir file as CLA. Here, is mine program: ---------- #include "llvm/Bitcode/BitcodeReader.h" #include "llvm/IR/Function.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; static cl::opt<std::string> FileName(cl::Positional, cl::desc("Bitcode file"), cl::Required); int main(int argc, char** argv) { cl::ParseCommandLineOptions(argc, argv, "LLVM hello world\n"); LLVMContext context; ErrorOr<std::unique_ptr<MemoryBuffer>> mb MemoryBuffer::getFile(FileName); if (std::error_code ec = mb.getError()) { errs() << ec.message(); return -1; } // ErrorOr<Module *> m = parseBitcodeFile(mb->get(), context); // if (std::error_code ec = m.getError()) { Expected<std::unique_ptr<Module>> m parseBitcodeFile(mb->get()->getMemBufferRef(), context); if (std::error_code ec = errorToErrorCode(m.takeError())) { errs() << "Error reading bitcode: " << ec.message() << "\n"; return -1; } for (Module::const_iterator I = (*m)->getFunctionList().begin(), E = (*m)->getFunctionList().end(); I != E; ++I) { if (!I->isDeclaration()) { outs() << I->getName() << " has " << I->size() << " basic blocks.\n"; } } return 0; } -------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180812/98325dd2/attachment.html>
David Wiberg via llvm-dev
2018-Aug-12 12:26 UTC
[llvm-dev] Problem in taking llvm ir file via command line argument
Hello, Den sön 12 aug. 2018 kl 08:12 skrev Ratnesh Tiwari via llvm-dev <llvm-dev at lists.llvm.org>:> > Hi, I am writing a program here hello.cpp for taking LLVM IR as a command line argument. My IR file is "input.bc" . But I dont understand how it make it via command line argument. > > I am running this hello.cpp as: > g++ hello.cpp -I /tmp/llvm/include/ -std=c++11 input.bc > > But it shows error. Please suggest me correct way of taking llvm ir file as CLA. Here, is mine program: > > ----------When asking questions, please include any error messages you get as it makes it easier for others to understand the problem. The first problem I see is that you need to separate the steps compiling "hello.cpp" and running the compiled file with "input.bc" as input argument. The second problem is that you haven't specified any libraries to link your source file to. In order to specify libraries and compiler flags the tool llvm-config can be used. See example below: $ g++ -std=c++11 `llvm-config --cxxflags` hello.cpp -o hello `llvm-config --ldflags --libs --system-libs` $ echo 123 > input.bc $ ./hello input.bc Error reading bitcode: Corrupted bitcode Best regards David