gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:40 27 4
gpt4 key购买 nike

我正在使用 LLVM 的 ExecutionEngine 来运行一个模块。该模块包含一个名为 blub 的函数,该函数返回 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然后 runFunction关于结果。

关于c++ - LLVM 执行引擎找不到我的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40488145/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com