LLVM:“导出”班级

发布于 2024-11-10 01:25:23 字数 1821 浏览 2 评论 0原文

我想使用 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;
  };
};

我使用 clang++ -emit-llvm -c -o hello.bc hello.cpp 将此代码编译为 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 类的新实例? 当我调用 Hello::Hello() 时遇到分段错误

感谢您的任何提示!

曼努埃尔

I'd like to call this code from my program using 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;
  };
};

I compiled this code to llvm byte code using clang++ -emit-llvm -c -o hello.bc hello.cpp and then I want to call it from this program:

#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");
}

(compiled using g++ -g main.cpp 'llvm-config --cppflags --ldflags --libs core jit native bitreader')

I can call the hello() function without any problems.
My question is: how can I create a new instance of the Hello class using LLVM?
I'm getting a segmentation fault when I call Hello::Hello()

Thanks for any hints!!

Manuel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-11-17 01:25:23

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

通常不建议尝试直接从 LLVM JIT 调用不是 extern "C" 的函数...我建议编写如下所示的包装器,并使用 clang 编译它(或使用clang API,取决于你在做什么):

extern "C" Hello* Hello_construct() {
  return new Hello;
}

Running clang++ -emit-llvm on the given source won't emit Hello::Hello, and m->getFunction("Hello::Hello") wouldn't find it even if it were emitted. I would guess it's crashing because func is null.

Trying to directly call functions which aren't extern "C" from the LLVM JIT is generally not recommended... I'd suggest writing a wrapper like the following, and compiling it with clang (or using the clang API, depending on what you're doing):

extern "C" Hello* Hello_construct() {
  return new Hello;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文