c++ - LLVM-5.0 Makefile undefined reference 失败

标签 c++ makefile llvm

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

main_module->dump();//main_module 的类型为 llvm::Module*

导致以下链接器错误:

对“llvm::Module::dump() const”的 undefined reference

转储方法驻留在/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

感谢任何帮助。

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

我尝试运行的代码: `

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

最佳答案

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

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

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

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

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

关于c++ - LLVM-5.0 Makefile undefined reference 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43723127/

相关文章:

LLVM:指令或值类中 "uses"和 "user"之间的区别

c++ - Gtkmm:将 RefPtr 与保存在 std::vector 中的小部件一起使用

c++ - 枚举返回类型无效 C [错误 C2440]

c++ - 'this' 指针存储在计算机内存中的什么位置?

c++ - 如何使函数的 LLVM IR 可用于我的程序?

c++ - 我应该在 Windows 上为 clang 使用哪个链接器

c++ - 在 Ubuntu 上使用 Eclipse C++ 时的 makefile 问题

c - Makefile编译多个C程序?

makefile - Makefile在数组中添加和添加所有元素

linux - "Make"子目录首选方法