【问题标题】:LLVM-5.0 Makefile undefined reference failLLVM-5.0 Makefile 未定义引用失败
【发布时间】:2017-05-01 17:04:39
【问题描述】:

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

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;
}

【问题讨论】:

  • 不应将“llvm-config-5.0 --cxxflags --ldflags --system-libs --libs”放在反引号中——例如"`llvm-config-5.0 --cxxflags --ldflags --system-libs --libs`"?
  • 链接器与头文件无关。编译器有,并且它将您使用的函数和方法的名称输出到目标文件中,以便链接器可以链接它们 - 或者告诉您哪些未定义,当它不能时。您的目标文件包含对llvm::Module::dump() const 的调用。这就是链接器所知道的。因此,链接器需要该方法的定义,以完成该调用,但找不到。
  • @G.M.它应该是反引号,只是`也代表堆栈溢出中的代码块,而\并没有真正用作转义键。
  • @MikeKinghan 你想把你的评论变成一个答案,这样我就可以把它变成接受的答案吗?
  • 仅供参考。这不是一个答案,因为它没有解释您的链接失败或如何解决它。

标签: c++ makefile llvm


【解决方案1】:

除了Subrat 提供的解决方案,您还可以调整您的代码以避免调用dump。你可以通过调用来达到同样的目的:

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

同样,如果你想转储一个 LLVM 函数,你可以这样写:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 2020-12-03
      • 2018-05-25
      • 2011-11-18
      相关资源
      最近更新 更多