通过命令行参数获取llvm ir文件时面临问题
Facing issue in taking llvm ir file via command line argument
我正在编写程序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 worldn");
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;
}
终于我得到了答案。我需要在编译时指定库和编译器标志。
编译: g++ -std=c++11 -I/tmp/llvm/include/
llvm-config --cxxflags
hello.cpp -o hellollvm-config --ldflags --libs --system-libs
要运行: ./hello input.bc
相关文章:
- 使用mongocxx驱动程序时包含头文件问题
- 生成文件问题
- 从文件问题加载纹理 Android NDK 和 OpenGL
- Xcode 11 头文件问题
- 使用 c++ ifstream 读取文本文件问题
- 单独的类声明和方法定义文件问题
- C++生成文件问题
- 生成文件问题(体系结构x86_64的未定义符号:"_main",引用自:主可执行文件的隐式入口/启动)
- C++删除未使用的库的生成文件问题
- C++ 使文件问题
- 阅读矩阵市场文件C++问题
- 查找第一个文件问题无法使任何示例正常工作。
- "Recursive Makefile Considered Harmful"样式生成文件问题
- 标题文件问题中的类定义
- 嵌套 while 循环,包含文件 c++ 问题
- 几个制作文件问题
- 生成文件问题,获取"目标'rgbapixel.o'的配方失败"
- 从yaml文件问题opencv读取数据
- 类成员C++标头和 .cc 文件问题
- 添加选项以显示百分比时出现生成文件问题