关于cmake:CMake-LLVM-JIT-build-usrbinld-undefined-reference-to

14次阅读

共计 1521 个字符,预计需要花费 4 分钟才能阅读完成。

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, 取得如下后果:

查看所有的 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 链接形式的探讨到此为止, 如果有解决方案, 我会更新在新的文章中.

正文完
 0