c++ - LLVM JIT 编译的程序找不到外部函数

标签 c++ shared-libraries llvm unresolved-external llvm-ir

如果 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++ 标准库中的外部定义函数(例如,printfmalloc、& free 没问题)。我做错了什么?

最佳答案

确保 glutInit 链接到 a.out。如果您的主机代码(执行 JIT 的东西)没有调用它,它可能已被链接器修复。如果是这种情况,您必须向它添加一个虚拟引用或使用链接描述文件/标志。

关于c++ - LLVM JIT 编译的程序找不到外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21173414/

相关文章:

Java/Tesseract-OCR : Unsatisfied LinkError libtesseract302

c++ - _i64toa_s 的等效项是什么?

c++ - stringstream 中的自定义字符串输入

c++ - 为什么bazel不搜索系统包含路径

c++ - Qt 无法将 OCR Tesseract 与 OpenCV 3.2 Ubuntu 一起使用

linux - 创建一个包含其链接时库依赖项的共享库

python - llvm-py问题

llvm - 我如何解析 LLVM IR

c++ - 2个并行线程运行,每个线程控制一个设备。我可以用什么来实现它?

c++ - 如何将指针值转换为 QString?