C++ 通过命令行参数获取llvm ir文件时面临的问题
我正在编写程序C++ 通过命令行参数获取llvm ir文件时面临的问题,c++,c++11,llvm,llvm-ir,C++,C++11,Llvm,Llvm Ir,我正在编写程序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
,参考《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;
}
#包括“llvm/Bitcode/BitcodeReader.h”
#包括“llvm/IR/Function.h”
#包括“llvm/IR/LLVMContext.h”
#包括“llvm/IR/Module.h”
#包括“llvm/Support/CommandLine.h”
#包括“llvm/Support/error.h”
#包括“llvm/Support/MemoryBuffer.h”
#包括“llvm/Support/raw_ostream.h”
使用名称空间llvm;
静态cl::opt
文件名(cl::Positional,cl::desc(“位码文件”),cl::Required);
int main(int argc,字符**argv)
{
cl::ParseCommandLineOptions(argc,argv,“LLVM hello world\n”);
语境;
ErrorOr mb=MemoryBuffer::getFile(文件名);
if(std::error_code ec=mb.getError()){
errs()get(),context);
//if(std::error_code ec=m.getError()){
应为m=parseBitcodeFile(mb->get()->getMemBufferRef(),上下文);
if(std::error_code ec=errorToErrorCode(m.takeError())){
errs()isDeclaration()){
outs()getName()最后我得到了答案。我需要在编译时指定库和编译器标志
要编译:g++-std=c++11-I/tmp/llvm/include/llvm-config-cxflags
hello.cpp-o hellollvm-config-ldflags-libs-system-libs
要运行:./hello input.bc
g++hello.cpp-I/tmp/llvm/include/-std=c++11 input.bc表示您正试图编译两个文件hello.cpp和input.bc,您有input.bc吗?另外.bc是位码文件,它在llvm位码的上下文中,而g++本机不支持它,可能再次检查它是否“叮当”并且我想缺少一些标志。@ChiragPatel,是的,我有input.bC++通过CLAN转换L.VC++,然后请告诉我编译Helo.CPP的方法是什么?把输入的命令作为BC命令行。也许在读了C++代码之后,它尝试了,G++Helo.CPP-O hello然后。/ ChiragPatel。我试过:<代码> G++MeN.CPP-I/TMP/LLVM/ICOR/-STD= C++ 11 B。但它显示了相同的错误:collect2:error:ld返回了1个退出状态
main.cpp???它是新文件吗?