LLVM JIT编译的程序找不到外部函数
LLVM JIT-compiled program cannot find external functions
如果foo
使用外部定义的函数,JIT编译LLVM IR模块并调用其中定义的函数foo
的程序在运行时失败
LLVM ERROR: Program used external function 'glutInit' which could not be resolved!
我的程序:
// foo1.cpp
#include <GL/glut.h>
extern "C" void foo()
{
glutInit(0,0);
}
// foo2.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <llvm/Support/raw_ostream.h>
#include <llvm/LLVMContext.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/IRReader.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/ExecutionEngine/RuntimeDyld.h>
int main(int argc, char **argv)
{
using namespace llvm;
InitializeNativeTarget();
LLVMContext context;
SMDiagnostic error;
std::ifstream ir_file("foo1.s");
std::string ir((std::istreambuf_iterator<char>(ir_file)),
(std::istreambuf_iterator<char>()));
Module *m = ParseIR(MemoryBuffer::getMemBuffer(StringRef(ir)), error, context);
if(!m)
{
error.print(argv[0], errs());
}
ExecutionEngine *ee = ExecutionEngine::create(m);
Function *func = ee->FindFunctionNamed("foo");
if(func == 0)
{
std::cerr << "Couldn't find Function foo" << std::endl;
std::exit(-1);
}
typedef void (*fcn_ptr)();
fcn_ptr foo = reinterpret_cast<fcn_ptr>(ee->getPointerToFunction(func));
foo();
delete ee;
return 0;
}
以下是我如何构建我的程序:
$ clang -S -emit-llvm foo1.cpp
$ g++ -rdynamic foo2.cpp `llvm-config --cxxflags` `llvm-config --libs` `llvm-config --ldflags` -lglut
输出:
$ ./a.out
LLVM ERROR: Program used external function 'glutInit' which could not be resolved!
每当我尝试使用不在C++标准库中的外部定义函数时,它都会失败,并出现类似的错误(例如,printf
、malloc
和free
没有问题)。我做错了什么?
确保glutInit
已链接到a.out
。如果你的主机代码(执行JIT的东西)没有调用它,它可能已经被链接器否决了。如果是这种情况,则必须添加一个伪引用或使用链接器脚本/标志。
在-lglut
之前添加命令行选项-Wl,-no-as-needed
将防止链接器丢弃glut
库,否则它认为不需要:
$ g++ -rdynamic foo2.cpp `llvm-config --cxxflags` `llvm-config --libs` `llvm-config --ldflags` -Wl,-no-as-needed -lglut
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- C++ 在标头中找不到外部
- cmake找不到Gurobi外部库
- 找不到 CPP 中来自外部源的函数 (Qt)
- Visual Studio 2010 C++ /w Google Protocol Buffers.找不到 60 个外部
- LLVM JIT编译的程序找不到外部函数
- 视觉对象找不到未解析的外部符号 (c++)
- 链接器读取库,但在其中找不到符号?未解析的外部符号,但仅适用于 Win32 而不是 x64
- 外部 VS2013 生成错误"错误 MSB4019:找不到导入的项目<path>"
- CMake 找不到外部库
- 视觉C++ - LNK2019未解析的外部符号 - 似乎找不到我缺少的内容
- 错误 LNK2019:未解析的外部符号链接器找不到 DLL
- 崇高文本找不到外部C++库
- 链接器未解析的外部符号 - 在 dll 中找不到函数
- c++链接器找不到外部库
- 链接器找不到函数定义,LNK2001未解析的外部符号
- GCC编译器找不到外部库