简体   繁体   English

构建LLVM示例:体系结构x86_64的未定义符号

[英]build LLVM example: Undefined symbols for architecture x86_64

I've installed LLVM on my laptop but having problem compiling the Fibonacci example in llvm/examples/Fibonacci : 我已经在笔记本电脑上安装了LLVM,但是在llvm/examples/Fibonacci编译Fibonacci示例时遇到了问题:

clang++ -std=c++11 -I ../../include fibonacci.cpp -o fib clang ++ -std = c ++ 11 -I ../../include fibonacci.cpp -o fib

/usr/local/include/llvm/Support/DataTypes.h:57:3: error: "Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h"

According to this question , I add some driver flags: 根据这个问题 ,我添加了一些驱动程序标志:

clang++ fibonacci.cpp -std=c++11 -I ../../include -o fib -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS clang ++ fibonacci.cpp -std = c ++ 11 -I ../../include -o fib -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS

Different errors produced: 产生的不同错误:

Undefined symbols for architecture x86_64:
  "_LLVMInitializeX86Target", referenced from:
      llvm::InitializeNativeTarget() in fibonacci-1b0cd0.o
  "_LLVMInitializeX86TargetInfo", referenced from:
      llvm::InitializeNativeTarget() in fibonacci-1b0cd0.o
  "_LLVMInitializeX86TargetMC", referenced from:
      llvm::InitializeNativeTarget() in fibonacci-1b0cd0.o
  "_LLVMLinkInInterpreter", referenced from:
      (anonymous namespace)::ForceInterpreterLinking::ForceInterpreterLinking() in fibonacci-1b0cd0.o
  "llvm::BasicBlock::BasicBlock(llvm::LLVMContext&, llvm::Twine const&, llvm::Function*, llvm::BasicBlock*)", referenced from:
      llvm::BasicBlock::Create(llvm::LLVMContext&, llvm::Twine const&, llvm::Function*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
  "llvm::BranchInst::BranchInst(llvm::BasicBlock*, llvm::BasicBlock*, llvm::Value*, llvm::BasicBlock*)", referenced from:
      llvm::BranchInst::Create(llvm::BasicBlock*, llvm::BasicBlock*, llvm::Value*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
  "llvm::ReturnInst::ReturnInst(llvm::LLVMContext&, llvm::Value*, llvm::BasicBlock*)", referenced from:
      llvm::ReturnInst::Create(llvm::LLVMContext&, llvm::Value*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
  "llvm::VectorType::get(llvm::Type*, unsigned int)", referenced from:
      llvm::CmpInst::makeCmpResultType(llvm::Type*) in fibonacci-1b0cd0.o
  "llvm::ConstantInt::get(llvm::IntegerType*, unsigned long long, bool)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-1b0cd0.o
  "llvm::Instruction::Instruction(llvm::Type*, unsigned int, llvm::Use*, unsigned int, llvm::BasicBlock*)", referenced from:
      llvm::CallInst::CallInst(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-1b0cd0.o
  "llvm::Instruction::~Instruction()", referenced from:
      llvm::CmpInst::~CmpInst() in fibonacci-1b0cd0.o
      llvm::CallInst::CallInst(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-1b0cd0.o
  "llvm::LLVMContext::LLVMContext()", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::LLVMContext::~LLVMContext()", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::raw_ostream::write(char const*, unsigned long)", referenced from:
      llvm::raw_ostream::operator<<(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in fibonacci-1b0cd0.o
      llvm::raw_ostream::operator<<(llvm::StringRef) in fibonacci-1b0cd0.o
  "llvm::raw_ostream::operator<<(long)", referenced from:
      llvm::raw_ostream::operator<<(int) in fibonacci-1b0cd0.o
  "llvm::verifyModule(llvm::Module const&, llvm::raw_ostream*)", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::EngineBuilder::selectTarget()", referenced from:
      llvm::EngineBuilder::create() in fibonacci-1b0cd0.o
  "llvm::EngineBuilder::create(llvm::TargetMachine*)", referenced from:
      llvm::EngineBuilder::create() in fibonacci-1b0cd0.o
  "llvm::EngineBuilder::EngineBuilder(std::__1::unique_ptr<llvm::Module, std::__1::default_delete<llvm::Module> >)", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::EngineBuilder::~EngineBuilder()", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::BinaryOperator::Create(llvm::Instruction::BinaryOps, llvm::Value*, llvm::Value*, llvm::Twine const&, llvm::BasicBlock*)", referenced from:
      llvm::BinaryOperator::CreateSub(llvm::Value*, llvm::Value*, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-1b0cd0.o
      llvm::BinaryOperator::CreateAdd(llvm::Value*, llvm::Value*, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-1b0cd0.o
  "llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)", referenced from:
      llvm::User::operator delete(void*, unsigned int) in fibonacci-1b0cd0.o
  "llvm::Type::getInt32Ty(llvm::LLVMContext&)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-1b0cd0.o
  "llvm::Type::getInt1Ty(llvm::LLVMContext&)", referenced from:
      llvm::CmpInst::makeCmpResultType(llvm::Type*) in fibonacci-1b0cd0.o
  "llvm::User::operator delete(void*)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-1b0cd0.o
      llvm::ICmpInst::~ICmpInst() in fibonacci-1b0cd0.o
     (maybe you meant: __ZN4llvm4UserdlEPvj)
  "llvm::User::operator new(unsigned long, unsigned int)", referenced from:
      llvm::CmpInst::operator new(unsigned long) in fibonacci-1b0cd0.o
      llvm::BranchInst::Create(llvm::BasicBlock*, llvm::BasicBlock*, llvm::Value*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
      llvm::ReturnInst::Create(llvm::LLVMContext&, llvm::Value*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
      llvm::CallInst::Create(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-1b0cd0.o
  "llvm::errs()", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::outs()", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::APInt::initSlowCase(unsigned int, unsigned long long, bool)", referenced from:
      llvm::APInt::APInt(unsigned int, unsigned long long, bool) in fibonacci-1b0cd0.o
  "llvm::Value::setName(llvm::Twine const&)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-1b0cd0.o
  "llvm::Module::getOrInsertFunction(llvm::StringRef, llvm::Type*, ...)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-1b0cd0.o
  "llvm::Module::Module(llvm::StringRef, llvm::LLVMContext&)", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::Module::~Module()", referenced from:
      _main in fibonacci-1b0cd0.o
  "llvm::CmpInst::anchor()", referenced from:
      vtable for llvm::ICmpInst in fibonacci-1b0cd0.o
  "llvm::CmpInst::CmpInst(llvm::Type*, llvm::Instruction::OtherOps, unsigned short, llvm::Value*, llvm::Value*, llvm::Twine const&, llvm::BasicBlock*)", referenced from:
      llvm::ICmpInst::ICmpInst(llvm::BasicBlock&, llvm::CmpInst::Predicate, llvm::Value*, llvm::Value*, llvm::Twine const&) in fibonacci-1b0cd0.o
  "llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&)", referenced from:
      llvm::CallInst::init(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&) in fibonacci-1b0cd0.o
  "llvm::Type::getScalarType() const", referenced from:
      llvm::Type::isIntOrIntVectorTy() const in fibonacci-1b0cd0.o
      llvm::Type::isPtrOrPtrVectorTy() const in fibonacci-1b0cd0.o
  "llvm::APInt::print(llvm::raw_ostream&, bool) const", referenced from:
      llvm::operator<<(llvm::raw_ostream&, llvm::APInt const&) in fibonacci-1b0cd0.o
  "llvm::Module::print(llvm::raw_ostream&, llvm::AssemblyAnnotationWriter*, bool, bool) const", referenced from:
      llvm::operator<<(llvm::raw_ostream&, llvm::Module const&) in fibonacci-1b0cd0.o
  "llvm::Function::BuildLazyArguments() const", referenced from:
      llvm::Function::CheckLazyArguments() const in fibonacci-1b0cd0.o
  "typeinfo for llvm::CmpInst", referenced from:
      typeinfo for llvm::ICmpInst in fibonacci-1b0cd0.o
  "vtable for llvm::CallInst", referenced from:
      llvm::CallInst::CallInst(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-1b0cd0.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)

I keep searching and find a similar question . 我不断寻找并发现类似的问题 Another shot: 另一个镜头:

clang++ -g -O3 fibonacci.cpp llvm-config --cxxflags --ldflags --system-libs --libs core -o fib clang ++ -g -O3 fibonacci.cpp llvm-config --cxxflags --ldflags --system-libs --libs core -o fib

Much shorter errors: 更短的错误:

Undefined symbols for architecture x86_64:
  "_LLVMInitializeX86Target", referenced from:
      _main in fibonacci-21f462.o
  "_LLVMInitializeX86TargetInfo", referenced from:
      _main in fibonacci-21f462.o
  "_LLVMInitializeX86TargetMC", referenced from:
      _main in fibonacci-21f462.o
  "_LLVMLinkInInterpreter", referenced from:
      __GLOBAL__sub_I_fibonacci.cpp in fibonacci-21f462.o
  "llvm::EngineBuilder::selectTarget()", referenced from:
      _main in fibonacci-21f462.o
  "llvm::EngineBuilder::create(llvm::TargetMachine*)", referenced from:
      _main in fibonacci-21f462.o
  "llvm::EngineBuilder::EngineBuilder(std::__1::unique_ptr<llvm::Module, std::__1::default_delete<llvm::Module> >)", referenced from:
      _main in fibonacci-21f462.o
  "llvm::EngineBuilder::~EngineBuilder()", referenced from:
      _main in fibonacci-21f462.o
ld: symbol(s) not found for architecture x86_64
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)

The executables of llvm/build/bin generated by CMake auto-build are good, which might prove the tool chain is working. CMake自动生成生成的llvm/build/bin可执行文件很好,这可能证明工具链正在运行。

But I still want to go into the code and observe. 但是我仍然想进入代码并观察。

Could someone help me out? 有人可以帮我吗?

LLVM version 3.8.0svn
Default target: x86_64-apple-darwin15.0.0
Host CPU: has well

clang version 3.8.0 
Target: x86_64-apple-darwin15.0.0
Thread model: posix

update: 更新:

I build a do-nothing a-test.cpp : 我建立了什么都不做的a-test.cpp

#include "llvm/IR/Verifier.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"

int main(int argc, char const *argv[])
{
    printf("hello world\n");
    return 0;
}

and successfully compile: 并成功编译:

clang++ -g -O3 -std=c++11 a-test.cpp llvm-config --cppflags --ldflags --system-libs --libs core executionengine interpreter mc support nativecodegen -o a-test clang ++ -g -O3 -std = c ++ 11 a-test.cpp llvm-config --cppflags --ldflags --system-libs --libs core executionengine interpreter mc support nativecodegen -o a-test

./a-test 。/一个测试

hello world

but not so good for fibonacci.cpp : 但是对于fibonacci.cpp不是很好:

clang++ -g -O3 -std=c++11 fibonacci.cpp llvm-config --cppflags --ldflags --system-libs --libs core executionengine interpreter mc support nativecodegen -o fib clang ++ -g -O3 -std = c ++ 11 fibonacci.cpp llvm-config --cppflags --ldflags --system-libs --libs core executionengine interpreter mc support nativecodegen -o fib

Undefined symbols for architecture x86_64:
  "typeinfo for llvm::CmpInst", referenced from:
      typeinfo for llvm::ICmpInst in fibonacci-009ddf.o
ld: symbol(s) not found for architecture x86_64
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)

If you look in the CMakeFile that builds Fibonacci, you'll see that it requires more than just the Core library. 如果查看构建Fibonacci的CMakeFile,您会发现它不仅需要Core库。

It requires Core , ExecutionEngine , Interpreter , MC , Support , nativecodege . 它需要CoreExecutionEngineInterpreterMCSupportnativecodege The correct --libs line would be --libs core executionengine interpreter mc support nativecodegen 正确的--libs行应是--libs core executionengine interpreter mc support nativecodegen

The file you're looking for is build.ninja . 您要查找的文件是build.ninja If you search in there for Fibonacci, you'll in all the information you need. 如果在此搜索斐波那契,您将获得所需的所有信息。

Edit: due to the OP struggling here, I've included the edited command line as well 编辑:由于OP在这里苦苦挣扎,我也包括了编辑后的命令行

clang++ -g -O3 -std=c++11 -fno-exceptions -fno-rtti fibonacci.cpp `llvm-config --cppflags --ldflags --system-libs --libs core executionengine interpreter mc support nativecodegen` -o fib

It's generally easier to use llvm-config --ldflags --libs rather than worrying about exactly which subset you need as well, but meh. 通常,使用llvm-config --ldflags --libs要容易llvm-config --ldflags --libs而不用担心您也需要确切地确定哪个子集。

This is not the right way do this anyway, use the build system! 无论如何,使用构建系统并不是正确的方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM