llvm 5.0 与 llvm::Module::dump() 的链接错误
llvm 5.0 linking error with llvm::Module::dump()
过去两天我一直在尝试 link LLVM 与我的 C++ 项目,它终于工作了,但问题是当我使用 dump()
方法时它给出了 linker错误 我认为问题出在我 link 反对的库上,所以我 link 针对所有 LLVM 库(模块)编辑了我的可执行文件,但没有成功。那么这是 LLVM5.0 代码库中的错误还是我做错了什么以及我特别谈论 LLVM5.0 的原因是因为我在评论部分的其他地方 () 看到了使用 LLVM4.0 编译相同的代码没有问题,当然我已经搜索了其他解决方案,但什么都没有
llvm_test.cpp:
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
llvm::LLVMContext context;
int main(){
llvm::Module*module = new llvm::Module("llvm-module",context);
module->dump();
}
命令:
clang++ -O3 -Wall -std=c++11 `llvm-config --cppflags --ldflags` `llvm-config --libs core --system-libs` toy.cpp
我已经link针对所有模块进行了编辑:
clang++ -O3 -Wall -std=c++11 `llvm-config --cxxflags --ldflags` `llvm-config --libs all --system-libs` toy.cpp
编译器: Apple Clang 8.0.0 x86_64
OS: mac OS 10.12.5
提前感谢您的帮助
因此,在解决问题之后,我终于找到了简单的破解方法,因为问题部分的解决方案又回到了上面 link 中回答问题的人,但我会给出完整的解决方案问题,但在我回答这个问题之前,我需要引起你的注意,这个解决方案适用于 LLVM5.0,所以我不知道它是否适用于其他版本。
-first: 我从文件 AsmWriter.cpp 中复制了 llvm::LLVMContext::dump
的定义 你可以在这个文件的最后找到各种llvm::Dump
方法的定义我已经把它们都复制了以备将来使用我会写LLVMDump.cpp的内容我用过
LLVMDump.cpp
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/AssemblyAnnotationWriter.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Statepoint.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/IR/UseListOrder.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include <algorithm>
#include <cctype>
using namespace llvm;
LLVM_DUMP_METHOD
void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
// Type::dump - allow easy printing of Types from the debugger.
LLVM_DUMP_METHOD
void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
// Module::dump() - Allow printing of Modules from the debugger.
LLVM_DUMP_METHOD
void Module::dump() const {
print(dbgs(), nullptr,
/*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
}
// \brief Allow printing of Comdats from the debugger.
LLVM_DUMP_METHOD
void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
LLVM_DUMP_METHOD
void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
LLVM_DUMP_METHOD
void Metadata::dump() const { dump(nullptr); }
LLVM_DUMP_METHOD
void Metadata::dump(const Module *M) const {
print(dbgs(), M, /*IsForDebug=*/true);
dbgs() << '\n';
}
请注意,我已将旧的包含目录 #include "llvm/Support/Dwarf.h"
更改为 #include "llvm/BinaryFormat/Dwarf.h"
-second: 我已经将 LLVMDUmp.cpp 作为静态库编译到 link 稍后当我需要它。
如果您不知道如何在 mac 上创建静态 linked 库,因为 -static 标志在 [=73 上不能与 clang 或 gcc 一起使用=]
clang++ -std=c++1z -O3 -c LLVMDump.cpp -o LLVMDump.o
然后创建静态 linked 库
ar -rv libLLVMDump.a LLVMDump.o
现在只需将libLLVMDump.a移动到您计算机上的lib目录中,以便在需要时使用它(如果您不知道应该在哪里设置lib来自 llvm 安装的目录)使用 llvm-config
llvm-config --lib-dir
它会显示你应该把你的 llvm/lib 安装目录放在哪里。
测试:
如果需要,可以使用问题中的代码
clang++ -O3 -Wall -std=c++1z -L /path/lib -lLLVMCore -lLLVMBinaryFormat -lLLVMSupport -lLLVMDemangle -lLLVMDump llvmtest.cpp
请注意我们刚刚创建的末尾的 LLVMDump。
希望对大家有所帮助,祝编码愉快。
好的,我查看了 llvm 的代码,您实际上可以轻松得多。您所要做的就是停止使用转储,而是:
module->print(llvm::errs(), nullptr);
这正是 dump 在内部所做的。
过去两天我一直在尝试 link LLVM 与我的 C++ 项目,它终于工作了,但问题是当我使用 dump()
方法时它给出了 linker错误 我认为问题出在我 link 反对的库上,所以我 link 针对所有 LLVM 库(模块)编辑了我的可执行文件,但没有成功。那么这是 LLVM5.0 代码库中的错误还是我做错了什么以及我特别谈论 LLVM5.0 的原因是因为我在评论部分的其他地方 (
llvm_test.cpp:
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
llvm::LLVMContext context;
int main(){
llvm::Module*module = new llvm::Module("llvm-module",context);
module->dump();
}
命令:
clang++ -O3 -Wall -std=c++11 `llvm-config --cppflags --ldflags` `llvm-config --libs core --system-libs` toy.cpp
我已经link针对所有模块进行了编辑:
clang++ -O3 -Wall -std=c++11 `llvm-config --cxxflags --ldflags` `llvm-config --libs all --system-libs` toy.cpp
编译器: Apple Clang 8.0.0 x86_64
OS: mac OS 10.12.5
提前感谢您的帮助
因此,在解决问题之后,我终于找到了简单的破解方法,因为问题部分的解决方案又回到了上面 link 中回答问题的人,但我会给出完整的解决方案问题,但在我回答这个问题之前,我需要引起你的注意,这个解决方案适用于 LLVM5.0,所以我不知道它是否适用于其他版本。
-first: 我从文件 AsmWriter.cpp 中复制了 llvm::LLVMContext::dump
的定义 你可以在这个文件的最后找到各种llvm::Dump
方法的定义我已经把它们都复制了以备将来使用我会写LLVMDump.cpp的内容我用过
LLVMDump.cpp
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/AssemblyAnnotationWriter.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Statepoint.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/IR/UseListOrder.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include <algorithm>
#include <cctype>
using namespace llvm;
LLVM_DUMP_METHOD
void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
// Type::dump - allow easy printing of Types from the debugger.
LLVM_DUMP_METHOD
void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
// Module::dump() - Allow printing of Modules from the debugger.
LLVM_DUMP_METHOD
void Module::dump() const {
print(dbgs(), nullptr,
/*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
}
// \brief Allow printing of Comdats from the debugger.
LLVM_DUMP_METHOD
void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
LLVM_DUMP_METHOD
void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
LLVM_DUMP_METHOD
void Metadata::dump() const { dump(nullptr); }
LLVM_DUMP_METHOD
void Metadata::dump(const Module *M) const {
print(dbgs(), M, /*IsForDebug=*/true);
dbgs() << '\n';
}
请注意,我已将旧的包含目录 #include "llvm/Support/Dwarf.h"
更改为 #include "llvm/BinaryFormat/Dwarf.h"
-second: 我已经将 LLVMDUmp.cpp 作为静态库编译到 link 稍后当我需要它。
如果您不知道如何在 mac 上创建静态 linked 库,因为 -static 标志在 [=73 上不能与 clang 或 gcc 一起使用=]
clang++ -std=c++1z -O3 -c LLVMDump.cpp -o LLVMDump.o
然后创建静态 linked 库
ar -rv libLLVMDump.a LLVMDump.o
现在只需将libLLVMDump.a移动到您计算机上的lib目录中,以便在需要时使用它(如果您不知道应该在哪里设置lib来自 llvm 安装的目录)使用 llvm-config
llvm-config --lib-dir
它会显示你应该把你的 llvm/lib 安装目录放在哪里。
测试: 如果需要,可以使用问题中的代码
clang++ -O3 -Wall -std=c++1z -L /path/lib -lLLVMCore -lLLVMBinaryFormat -lLLVMSupport -lLLVMDemangle -lLLVMDump llvmtest.cpp
请注意我们刚刚创建的末尾的 LLVMDump。
希望对大家有所帮助,祝编码愉快。
好的,我查看了 llvm 的代码,您实际上可以轻松得多。您所要做的就是停止使用转储,而是:
module->print(llvm::errs(), nullptr);
这正是 dump 在内部所做的。