编译LLVM Kaleidoscope教程第8章遇llvm::Function等编译错误求助
解决LLVM Kaleidoscope第8章编译错误的方法
错误1:llvm::Function无insert成员
你的编译环境使用的是LLVM 14,但Function::insert是LLVM 15及以上版本才新增的接口,LLVM 14并不支持。你在Function.h中找到的方法属于新版本LLVM的定义,实际编译时链接的是LLVM 14的库,因此报错。
解决方法:将Function::insert调用替换为LLVM 14支持的BasicBlock::insertInto方法:
- 原代码:
修改为:TheFunction->insert(TheFunction->end(), ElseBB);ElseBB->insertInto(TheFunction, TheFunction->end()); - 另一处报错代码同理修改:
修改为:TheFunction->insert(TheFunction->end(), MergeBB);MergeBB->insertInto(TheFunction, TheFunction->end());
错误2:std::optional未定义
LLVM 14的适配代码中,默认使用自身实现的llvm::Optional模板,而非C17标准库的std::optional。报错原因是代码中使用了std::optional,但编译环境未启用C17,或LLVM 14不兼容直接调用标准库版本。
解决方法二选一:
- 替换为LLVM自带的
Optional:auto RM = llvm::Optional<Reloc::Model>(); - 编译时添加C++17标准支持参数,更新编译命令:
clang -g -O3 -std=c++17 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs all` -o toy
内容的提问来源于stack exchange,提问作者rd142857
相关产品推荐
相关产品推荐

