LLVM:“导出”类别

问题描述

| 我想使用LLVM从我的程序中调用以下代码:
#include <string>
#include <iostream>
extern \"C\" void hello() {
        std::cout << \"hello\" << std::endl;
}

class Hello {
public:
  Hello() {
    std::cout <<\"Hello::Hello()\" << std::endl;
  };

  int hello() {
    std::cout<< \"Hello::hello()\" << std::endl;
    return 99;
  };
};
我使用ѭ1compiled将此代码编译为llvm字节代码,然后从此程序中调用它:
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/IRReader.h>

#include <string>
#include <iostream>
#include <vector>
using namespace std;
using namespace llvm;

void callFunction(string file,string function) {
  InitializeNativeTarget();
  LLVMContext context;
  string error;

  MemoryBuffer* buff = MemoryBuffer::getFile(file);
  assert(buff);
  Module* m = getLazyBitcodeModule(buff,context,&error);
  ExecutionEngine* engine = ExecutionEngine::create(m);    
  Function* func = m->getFunction(function);

  vector<GenericValue> args(0);    
  engine->runFunction(func,args);

  func = m->getFunction(\"Hello::Hello\");
  engine->runFunction(func,args);
}

int main() {
  callFunction(\"hello.bc\",\"hello\");
}
(使用
g++ -g main.cpp \'llvm-config --cppflags --ldflags --libs core jit native bitreader\'
编译) 我可以毫无问题地调用
hello()
函数。 我的问题是:如何使用LLVM创建
Hello
类的新实例? 致电ѭ6时出现细分错误 感谢您的任何提示! 曼努埃尔     

解决方法

        在给定的源上运行ѭ7won不会发出Hello :: Hello,即使发出了
m->getFunction(\"Hello::Hello\")
也找不到。我想它会崩溃是因为
func
为空。 通常不建议尝试直接从LLVM JIT中调用不是10的函数...我建议编写如下包装程序,并使用clang进行编译(或使用clang API,具体取决于您的目标)正在执行):
extern \"C\" Hello* Hello_construct() {
  return new Hello;
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...