Class LLVM:“;“出口”;班

Class LLVM:“;“出口”;班,class,export,llvm,Class,Export,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; }

我想使用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;
  };
};
(使用
g++-g main.cpp'llvm config--cppflags--ldflags--libs core jit native bitreader'
编译)

我可以毫无问题地调用
hello()
函数。 我的问题是:如何使用LLVM创建
Hello
类的新实例? 当我调用
Hello::Hello()

谢谢你的提示


Manuel

在给定源上运行
clang++-emit llvm
不会发出Hello::Hello,并且
m->getFunction(“Hello::Hello”)
即使发出也不会找到它。我猜它崩溃了,因为
func
为空

通常不建议直接从LLVM JIT调用不是外部“C”的函数。。。我建议编写一个如下所示的包装器,并使用clang进行编译(或者使用clangapi,具体取决于您所做的工作):

#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");
}
extern "C" Hello* Hello_construct() {
  return new Hello;
}