LLVM-5.0 Makefile 未定义引用失败

LLVM-5.0 Makefile undefined reference fail

在我的代码中包含以下语句

main_module->dump(); // main_module is of type llvm::Module*

导致以下链接器错误:

undefined reference to 'llvm::Module::dump() const'

转储方法驻留在/usr/lib/llvm-5.0/include/llvm/IR/Module.h

我检查了堆栈溢出 (Using llvm::Function::dump(), linker gives "undefined reference to `llvm::Value::dump() const'"),当链接器未按正确顺序提供库时,我们似乎会收到此错误。但是,我的编译命令中显然有最后的库:

clang++-5.0 -g -O3 main.cpp -o main llvm-config-5.0 --cxxflags --ldflags --system-libs --libs core mcjit native

感谢任何帮助。

奇怪的是,链接器弄清楚了转储方法的类型。它显然包含在包含文件中。那么为什么它会称它为未定义的引用呢?

我正在尝试的代码运行: `

# include "llvm/IR/LLVMContext.h"
# include "llvm/IR/Module.h"
# include "llvm/IR/IRBuilder.h"
# include <iostream>

using namespace llvm;

static LLVMContext ctxt;
static IRBuilder<> builder(ctxt);

int main(int argc, char** argv) {

    Module* main_module = new Module("main_module", ctxt);
    std::cout << main_module->getModuleIdentifier() << "\n";

    FunctionType* func_type = FunctionType::get(builder.getInt32Ty(), false);
    Function* main_func = Function::Create(func_type,Function::ExternalLinkage, "main", main_module);

    if (main_module->getFunction("main")) {
        std::cout << "Found function!\n";
    }

    main_module->dump();  // need this for debugging and testing reasons with LLVM

    return 0;
}

似乎 dump 的定义在 ASMWriter.cpp 中,这似乎被贬低了。 另外,ASMWrite.cpp的调试方法是指dbgs(),在debug.cpp

我通过复制 debug.cppModule::dump()(来自 ASMWriter.cpp——因为我不需要整个代码,只需要这个文件中的一个特定子程序来解决这个问题) 例程并将其放入我的 cpp 文件中。

除了 Subrat 提供的解决方案之外,您还可以调整代码以避免调用 dump。您可以通过调用来实现相同的目的:

main_module->print(llvm::outs(), nullptr);

同理,如果要dump一个LLVM函数,可以这样写:

main_func->print(llvm::outs());

实际上,从 LLVM 5.0.0 开始,这就是 dump() 函数的实现方式。