TL;DR(EN):

  1. Open CMakeLists.txt
  2. Find this line, then add the required component:

    llvm_map_components_to_libnames(LLVM_LIBS core <Component>)
  3. If you don't know what component is required, then check this:

    llvm-config-<LLVM_Version> --components

2024-02-26

本人于昨日为编译器前端添加 ORC JIT 的时候遇到这个问题, 初步判断是链接器问题, 可能的原因如下:

  1. LLVM 部分模块缺失, 需要安装 LLVM 框架中对应的模块.
  2. LLVM 链接器出错, 需要降级版本或重新安装
  3. CMake 链接器出错

因为使用了 LLVM 提供的安装脚本, 所以应该是已经安装了全部的模块, 非常可惜, 我不知道 ORC JIT 对应的是哪个模块, 甚至无从查起.

wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh <version number>

在网络搜索的过程中, 发现 llvm-config 的链接:
https://llvm.org/docs/CommandGuide/llvm-config.html

/usr/bin 目录下确定 llvm-config-18 的存在, 且当前使用的 LLVM 版本为 18 . 然后使用 llvm-config-18 –components, 获得如下结果:

image.png

查看所有的component 的名字, 其中有一个很不起眼的东西: orcjit, 这就是我今天的目标.

正常情况下, clang 的命令行选项 (Command-Line Options) 应该如下方式使用:

clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native` -O3 -o toy

但是我们用的是 CMake, 所以还没结束. 查找 CMakeLists.txt 文件, 猜测 llvm-config 的链接通过如下方式完成:

llvm_map_components_to_libnames(LLVM_LIBS core native)

因此修改为:

llvm_map_components_to_libnames(LLVM_LIBS core orcjit native)

成功修复!

不过我们还剩下一些问题, 比如说, 我们现在的命令只能对应上如下这部分

`llvm-config --libs core orcjit native`

但是 --system-lib 也是很重要的后缀, 这个可以把我们的系统库都链接进来, 加上之后, 在命令行里长这个样子:

`llvm-config --system-lib --libs core orcjit native`

我们现在用的是 CMake, 该怎么完成这个后缀的添加呢? 我在 stackoverflow 里面找到了相似的问题:

https://stackoverflow.com/questions/52311181/how-to-add-the-option-llvm-config-cxxflags-ldflags-libs-in-cmake

遗憾的是, 我们并没有一个简单有效的解决方案. 我猜测是需要使用某些方式导入 system-lib 并且使用 target_link_libraries 链接. 也有可能在 llvm_map_components_to_libnames 里面有预设好的给 system-lib 使用的接口. 但无论如何, 我们今天对于 orcjit 链接方式的讨论到此为止, 如果有解决方案, 我会更新在新的文章中.


unka_malloc
7 声望3 粉丝