LLVM 执行引擎找不到我的函数

LLVM execution engine cannot find my function

我正在使用 LLVM 的 ExecutionEngine 到 运行 模块。该模块包含一个名为 blub 的函数,即 returns 5。在 C 中:

int blub() {
  int x = 5;
  return x;
}

这是我执行 "blub" 的 C++ 代码:

// Print out all of the functions, just to see
for (auto& function : M->functions()) {
  std::cout << function.getName().str() << std::endl;
}

auto engine = EngineBuilder(std::move(M)).create();

engine->finalizeObject();

using MyFunc = int();
auto func = (MyFunc*)engine->getPointerToNamedFunction("blub");

auto result = func();

std::cout << "result is " << result << std::endl;

它应该打印出所有函数的名称(只是 "blub"),然后是结果“5”。

但是,我得到了这个错误:

blub
LLVM ERROR: Program used external function 'blub' which could not be resolved!

所以该函数确实在模块中,但无法通过ExecutionEngine解析。我错过了一步吗?

来自 the documentation of getPointerToNamedFunction(强调我的):

getPointerToNamedFunction - This method returns the address of the specified function by using the dlsym function call.

As such it is only useful for resolving library symbols, not code generated symbols.

您应该对结果调用 findFunctionNamed and then runFunction