编译LLVM 示例代码报错:Undefined symbols for architecture x86_64:


下载LLVM代码并安装之后,尝试手动编译 llvm/examples/Fibonacci 中的代码:

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

显示大量错误:


 Undefined symbols for architecture x86_64:
  "_LLVMInitializeX86Target", referenced from:
      llvm::InitializeNativeTarget() in fibonacci-test-30f11d.o
  "_LLVMInitializeX86TargetInfo", referenced from:
      llvm::InitializeNativeTarget() in fibonacci-test-30f11d.o
  "_LLVMInitializeX86TargetMC", referenced from:
      llvm::InitializeNativeTarget() in fibonacci-test-30f11d.o
  "_LLVMLinkInInterpreter", referenced from:
      (anonymous namespace)::ForceInterpreterLinking::ForceInterpreterLinking() in fibonacci-test-30f11d.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-test-30f11d.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-test-30f11d.o
  "llvm::ReturnInst::ReturnInst(llvm::LLVMContext&, llvm::Value*, llvm::BasicBlock*)", referenced from:
      llvm::ReturnInst::Create(llvm::LLVMContext&, llvm::Value*, llvm::BasicBlock*) in fibonacci-test-30f11d.o
  "llvm::VectorType::get(llvm::Type*, unsigned int)", referenced from:
      llvm::CmpInst::makeCmpResultType(llvm::Type*) in fibonacci-test-30f11d.o
  "llvm::ConstantInt::get(llvm::IntegerType*, unsigned long long, bool)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-test-30f11d.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-test-30f11d.o
  "llvm::Instruction::~Instruction()", referenced from:
      llvm::CmpInst::~CmpInst() in fibonacci-test-30f11d.o
      llvm::CallInst::CallInst(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-test-30f11d.o
  "llvm::LLVMContext::LLVMContext()", referenced from:
      _main in fibonacci-test-30f11d.o
  "llvm::LLVMContext::~LLVMContext()", referenced from:
      _main in fibonacci-test-30f11d.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-test-30f11d.o
      llvm::raw_ostream::operator<<(llvm::StringRef) in fibonacci-test-30f11d.o
  "llvm::raw_ostream::operator<<(long)", referenced from:
      llvm::raw_ostream::operator<<(int) in fibonacci-test-30f11d.o
  "llvm::verifyModule(llvm::Module const&, llvm::raw_ostream*)", referenced from:
      _main in fibonacci-test-30f11d.o
  "llvm::EngineBuilder::selectTarget()", referenced from:
      llvm::EngineBuilder::create() in fibonacci-test-30f11d.o
  "llvm::EngineBuilder::create(llvm::TargetMachine*)", referenced from:
      llvm::EngineBuilder::create() in fibonacci-test-30f11d.o
  "llvm::EngineBuilder::EngineBuilder(std::__1::unique_ptr<llvm::Module, std::__1::default_delete<llvm::Module> >)", referenced from:
      _main in fibonacci-test-30f11d.o
  "llvm::EngineBuilder::~EngineBuilder()", referenced from:
      _main in fibonacci-test-30f11d.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-test-30f11d.o
      llvm::BinaryOperator::CreateAdd(llvm::Value*, llvm::Value*, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-test-30f11d.o
  "llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)", referenced from:
      llvm::User::operator delete(void*, unsigned int) in fibonacci-test-30f11d.o
  "llvm::Type::getInt32Ty(llvm::LLVMContext&)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-test-30f11d.o
  "llvm::Type::getInt1Ty(llvm::LLVMContext&)", referenced from:
      llvm::CmpInst::makeCmpResultType(llvm::Type*) in fibonacci-test-30f11d.o
  "llvm::User::operator delete(void*)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-test-30f11d.o
      llvm::ICmpInst::~ICmpInst() in fibonacci-test-30f11d.o
     (maybe you meant: __ZN4llvm4UserdlEPvj)
  "llvm::User::operator new(unsigned long, unsigned int)", referenced from:
      llvm::CmpInst::operator new(unsigned long) in fibonacci-test-30f11d.o
      llvm::BranchInst::Create(llvm::BasicBlock*, llvm::BasicBlock*, llvm::Value*, llvm::BasicBlock*) in fibonacci-test-30f11d.o
      llvm::ReturnInst::Create(llvm::LLVMContext&, llvm::Value*, llvm::BasicBlock*) in fibonacci-test-30f11d.o
      llvm::CallInst::Create(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::Twine const&, llvm::BasicBlock*) in fibonacci-test-30f11d.o
  "llvm::errs()", referenced from:
      _main in fibonacci-test-30f11d.o
  "llvm::outs()", referenced from:
      _main in fibonacci-test-30f11d.o
  "llvm::APInt::initSlowCase(unsigned int, unsigned long long, bool)", referenced from:
      llvm::APInt::APInt(unsigned int, unsigned long long, bool) in fibonacci-test-30f11d.o
  "llvm::Value::setName(llvm::Twine const&)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-test-30f11d.o
  "llvm::Module::getOrInsertFunction(llvm::StringRef, llvm::Type*, ...)", referenced from:
      CreateFibFunction(llvm::Module*, llvm::LLVMContext&) in fibonacci-test-30f11d.o
  "llvm::Module::Module(llvm::StringRef, llvm::LLVMContext&)", referenced from:
      _main in fibonacci-test-30f11d.o
  "llvm::Module::~Module()", referenced from:
      _main in fibonacci-test-30f11d.o
  "llvm::CmpInst::anchor()", referenced from:
      vtable for llvm::ICmpInst in fibonacci-test-30f11d.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-test-30f11d.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-test-30f11d.o
  "llvm::Type::getScalarType() const", referenced from:
      llvm::Type::isIntOrIntVectorTy() const in fibonacci-test-30f11d.o
      llvm::Type::isPtrOrPtrVectorTy() const in fibonacci-test-30f11d.o
  "llvm::APInt::print(llvm::raw_ostream&, bool) const", referenced from:
      llvm::operator<<(llvm::raw_ostream&, llvm::APInt const&) in fibonacci-test-30f11d.o
  "llvm::Module::print(llvm::raw_ostream&, llvm::AssemblyAnnotationWriter*, bool, bool) const", referenced from:
      llvm::operator<<(llvm::raw_ostream&, llvm::Module const&) in fibonacci-test-30f11d.o
  "llvm::Function::BuildLazyArguments() const", referenced from:
      llvm::Function::CheckLazyArguments() const in fibonacci-test-30f11d.o
  "typeinfo for llvm::CmpInst", referenced from:
      typeinfo for llvm::ICmpInst in fibonacci-test-30f11d.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-test-30f11d.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)

新建一个 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;
}

$ clang++ -std=c++11 -I LLVM-LOCATION a-test.cpp -o a-test -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS

仍然报错:


 Undefined symbols for architecture x86_64:
  "_LLVMLinkInInterpreter", referenced from:
      (anonymous namespace)::ForceInterpreterLinking::ForceInterpreterLinking() in a-test-222f96.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)

如何解决该问题?

LLVM 安装:

  1. llvm/build/bin 中的可执行程序全部可正常运行(通过CMake自动生成)

  2. lli 可以正常使用

版本:


 OS X 10.11.1

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

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

编译原理 gcc llvm clang

xanthe 9 years, 2 months ago

Your Answer