我正在编写程序hello.cpp
,该程序参考了LLVM核心库入门指南第3章中的示例。
我想将LLVM IR文件input.bc
用作命令行参数。但是我不知道该怎么做。我正在尝试:g++ hello.cpp -I /tmp/llvm/include/ -std=c++11 input.bc
显示错误:
input.bc: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
这是我的hello.cpp
#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;
}
答案 0 :(得分:0)
最后我得到了答案。编译时需要指定库和编译器标志。
要编译:g ++ -std = c ++ 11 -I / tmp / llvm / include /
llvm-config --cxxflags
hello.cpp -o hellollvm-config --ldflags --libs --system-libs
要运行:./ hello input.bc