title: build LLVM example: Undefined symbols for architecture x86_64
body: <p>I've installed LLVM on my laptop but having problem compiling the <code>Fibonacci</code> example in <code>llvm/examples/Fibonacci</code>:</p>
  <blockquote>
  <p>clang++ -std=c++11 -I ../../include fibonacci.cpp -o fib</p>
  </blockquote>
  <pre><code>/usr/local/include/llvm/Support/DataTypes.h:57:3: error: &quot;Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h&quot;
  </code></pre>
  <p>According to <a href="https://stackoverflow.com/questions/18965870/error-must-define-stdc-limit-macros-before-including-support-datatypes-h">this question</a>, I add some driver flags:</p>
  <blockquote>
  <p>clang++ fibonacci.cpp -std=c++11 -I ../../include -o fib -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS</p>
  </blockquote>
  <p>Different errors produced:</p>
  <pre><code>Undefined symbols for architecture x86_64:
    &quot;_LLVMInitializeX86Target&quot;, referenced from:
        llvm::InitializeNativeTarget() in fibonacci-1b0cd0.o
    &quot;_LLVMInitializeX86TargetInfo&quot;, referenced from:
        llvm::InitializeNativeTarget() in fibonacci-1b0cd0.o
    &quot;_LLVMInitializeX86TargetMC&quot;, referenced from:
        llvm::InitializeNativeTarget() in fibonacci-1b0cd0.o
    &quot;_LLVMLinkInInterpreter&quot;, referenced from:
        (anonymous namespace)::ForceInterpreterLinking::ForceInterpreterLinking() in fibonacci-1b0cd0.o
    &quot;llvm::BasicBlock::BasicBlock(llvm::LLVMContext&amp;, llvm::Twine const&amp;, llvm::Function*, llvm::BasicBlock*)&quot;, referenced from:
        llvm::BasicBlock::Create(llvm::LLVMContext&amp;, llvm::Twine const&amp;, llvm::Function*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
    &quot;llvm::BranchInst::BranchInst(llvm::BasicBlock*, llvm::BasicBlock*, llvm::Value*, llvm::BasicBlock*)&quot;, referenced from:
        llvm::BranchInst::Create(llvm::BasicBlock*, llvm::BasicBlock*, llvm::Value*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
    &quot;llvm::ReturnInst::ReturnInst(llvm::LLVMContext&amp;, llvm::Value*, llvm::BasicBlock*)&quot;, referenced from:
        llvm::ReturnInst::Create(llvm::LLVMContext&amp;, llvm::Value*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
    &quot;llvm::VectorType::get(llvm::Type*, unsigned int)&quot;, referenced from:
        llvm::CmpInst::makeCmpResultType(llvm::Type*) in fibonacci-1b0cd0.o
    &quot;llvm::ConstantInt::get(llvm::IntegerType*, unsigned long long, bool)&quot;, referenced from:
        CreateFibFunction(llvm::Module*, llvm::LLVMContext&amp;) in fibonacci-1b0cd0.o
    &quot;llvm::Instruction::Instruction(llvm::Type*, unsigned int, llvm::Use*, unsigned int, llvm::BasicBlock*)&quot;, referenced from:
        llvm::CallInst::CallInst(llvm::Value*, llvm::ArrayRef&lt;llvm::Value*&gt;, llvm::ArrayRef&lt;llvm::OperandBundleDefT&lt;llvm::Value*&gt; &gt;, llvm::Twine const&amp;, llvm::BasicBlock*) in fibonacci-1b0cd0.o
    &quot;llvm::Instruction::~Instruction()&quot;, referenced from:
        llvm::CmpInst::~CmpInst() in fibonacci-1b0cd0.o
        llvm::CallInst::CallInst(llvm::Value*, llvm::ArrayRef&lt;llvm::Value*&gt;, llvm::ArrayRef&lt;llvm::OperandBundleDefT&lt;llvm::Value*&gt; &gt;, llvm::Twine const&amp;, llvm::BasicBlock*) in fibonacci-1b0cd0.o
    &quot;llvm::LLVMContext::LLVMContext()&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::LLVMContext::~LLVMContext()&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::raw_ostream::write(char const*, unsigned long)&quot;, referenced from:
        llvm::raw_ostream::operator&lt;&lt;(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;) in fibonacci-1b0cd0.o
        llvm::raw_ostream::operator&lt;&lt;(llvm::StringRef) in fibonacci-1b0cd0.o
    &quot;llvm::raw_ostream::operator&lt;&lt;(long)&quot;, referenced from:
        llvm::raw_ostream::operator&lt;&lt;(int) in fibonacci-1b0cd0.o
    &quot;llvm::verifyModule(llvm::Module const&amp;, llvm::raw_ostream*)&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::EngineBuilder::selectTarget()&quot;, referenced from:
        llvm::EngineBuilder::create() in fibonacci-1b0cd0.o
    &quot;llvm::EngineBuilder::create(llvm::TargetMachine*)&quot;, referenced from:
        llvm::EngineBuilder::create() in fibonacci-1b0cd0.o
    &quot;llvm::EngineBuilder::EngineBuilder(std::__1::unique_ptr&lt;llvm::Module, std::__1::default_delete&lt;llvm::Module&gt; &gt;)&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::EngineBuilder::~EngineBuilder()&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::BinaryOperator::Create(llvm::Instruction::BinaryOps, llvm::Value*, llvm::Value*, llvm::Twine const&amp;, llvm::BasicBlock*)&quot;, referenced from:
        llvm::BinaryOperator::CreateSub(llvm::Value*, llvm::Value*, llvm::Twine const&amp;, llvm::BasicBlock*) in fibonacci-1b0cd0.o
        llvm::BinaryOperator::CreateAdd(llvm::Value*, llvm::Value*, llvm::Twine const&amp;, llvm::BasicBlock*) in fibonacci-1b0cd0.o
    &quot;llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)&quot;, referenced from:
        llvm::User::operator delete(void*, unsigned int) in fibonacci-1b0cd0.o
    &quot;llvm::Type::getInt32Ty(llvm::LLVMContext&amp;)&quot;, referenced from:
        CreateFibFunction(llvm::Module*, llvm::LLVMContext&amp;) in fibonacci-1b0cd0.o
    &quot;llvm::Type::getInt1Ty(llvm::LLVMContext&amp;)&quot;, referenced from:
        llvm::CmpInst::makeCmpResultType(llvm::Type*) in fibonacci-1b0cd0.o
    &quot;llvm::User::operator delete(void*)&quot;, referenced from:
        CreateFibFunction(llvm::Module*, llvm::LLVMContext&amp;) in fibonacci-1b0cd0.o
        llvm::ICmpInst::~ICmpInst() in fibonacci-1b0cd0.o
       (maybe you meant: __ZN4llvm4UserdlEPvj)
    &quot;llvm::User::operator new(unsigned long, unsigned int)&quot;, 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&amp;, llvm::Value*, llvm::BasicBlock*) in fibonacci-1b0cd0.o
        llvm::CallInst::Create(llvm::Value*, llvm::ArrayRef&lt;llvm::Value*&gt;, llvm::Twine const&amp;, llvm::BasicBlock*) in fibonacci-1b0cd0.o
    &quot;llvm::errs()&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::outs()&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::APInt::initSlowCase(unsigned int, unsigned long long, bool)&quot;, referenced from:
        llvm::APInt::APInt(unsigned int, unsigned long long, bool) in fibonacci-1b0cd0.o
    &quot;llvm::Value::setName(llvm::Twine const&amp;)&quot;, referenced from:
        CreateFibFunction(llvm::Module*, llvm::LLVMContext&amp;) in fibonacci-1b0cd0.o
    &quot;llvm::Module::getOrInsertFunction(llvm::StringRef, llvm::Type*, ...)&quot;, referenced from:
        CreateFibFunction(llvm::Module*, llvm::LLVMContext&amp;) in fibonacci-1b0cd0.o
    &quot;llvm::Module::Module(llvm::StringRef, llvm::LLVMContext&amp;)&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::Module::~Module()&quot;, referenced from:
        _main in fibonacci-1b0cd0.o
    &quot;llvm::CmpInst::anchor()&quot;, referenced from:
        vtable for llvm::ICmpInst in fibonacci-1b0cd0.o
    &quot;llvm::CmpInst::CmpInst(llvm::Type*, llvm::Instruction::OtherOps, unsigned short, llvm::Value*, llvm::Value*, llvm::Twine const&amp;, llvm::BasicBlock*)&quot;, referenced from:
        llvm::ICmpInst::ICmpInst(llvm::BasicBlock&amp;, llvm::CmpInst::Predicate, llvm::Value*, llvm::Value*, llvm::Twine const&amp;) in fibonacci-1b0cd0.o
    &quot;llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef&lt;llvm::Value*&gt;, llvm::ArrayRef&lt;llvm::OperandBundleDefT&lt;llvm::Value*&gt; &gt;, llvm::Twine const&amp;)&quot;, referenced from:
        llvm::CallInst::init(llvm::Value*, llvm::ArrayRef&lt;llvm::Value*&gt;, llvm::ArrayRef&lt;llvm::OperandBundleDefT&lt;llvm::Value*&gt; &gt;, llvm::Twine const&amp;) in fibonacci-1b0cd0.o
    &quot;llvm::Type::getScalarType() const&quot;, referenced from:
        llvm::Type::isIntOrIntVectorTy() const in fibonacci-1b0cd0.o
        llvm::Type::isPtrOrPtrVectorTy() const in fibonacci-1b0cd0.o
    &quot;llvm::APInt::print(llvm::raw_ostream&amp;, bool) const&quot;, referenced from:
        llvm::operator&lt;&lt;(llvm::raw_ostream&amp;, llvm::APInt const&amp;) in fibonacci-1b0cd0.o
    &quot;llvm::Module::print(llvm::raw_ostream&amp;, llvm::AssemblyAnnotationWriter*, bool, bool) const&quot;, referenced from:
        llvm::operator&lt;&lt;(llvm::raw_ostream&amp;, llvm::Module const&amp;) in fibonacci-1b0cd0.o
    &quot;llvm::Function::BuildLazyArguments() const&quot;, referenced from:
        llvm::Function::CheckLazyArguments() const in fibonacci-1b0cd0.o
    &quot;typeinfo for llvm::CmpInst&quot;, referenced from:
        typeinfo for llvm::ICmpInst in fibonacci-1b0cd0.o
    &quot;vtable for llvm::CallInst&quot;, referenced from:
        llvm::CallInst::CallInst(llvm::Value*, llvm::ArrayRef&lt;llvm::Value*&gt;, llvm::ArrayRef&lt;llvm::OperandBundleDefT&lt;llvm::Value*&gt; &gt;, llvm::Twine const&amp;, 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)
  </code></pre>
  <p>I keep searching and find a <a href="https://stackoverflow.com/questions/31152499/undefined-symbols-for-architecture-x86-64-stdterminate-when-building-kal">similar question</a>. Another shot:</p>
  <blockquote>
  <p>clang++ -g -O3 fibonacci.cpp <code>llvm-config --cxxflags --ldflags --system-libs --libs core</code> -o fib</p>
  </blockquote>
  <p>Much shorter errors:</p>
  <pre><code>Undefined symbols for architecture x86_64:
    &quot;_LLVMInitializeX86Target&quot;, referenced from:
        _main in fibonacci-21f462.o
    &quot;_LLVMInitializeX86TargetInfo&quot;, referenced from:
        _main in fibonacci-21f462.o
    &quot;_LLVMInitializeX86TargetMC&quot;, referenced from:
        _main in fibonacci-21f462.o
    &quot;_LLVMLinkInInterpreter&quot;, referenced from:
        __GLOBAL__sub_I_fibonacci.cpp in fibonacci-21f462.o
    &quot;llvm::EngineBuilder::selectTarget()&quot;, referenced from:
        _main in fibonacci-21f462.o
    &quot;llvm::EngineBuilder::create(llvm::TargetMachine*)&quot;, referenced from:
        _main in fibonacci-21f462.o
    &quot;llvm::EngineBuilder::EngineBuilder(std::__1::unique_ptr&lt;llvm::Module, std::__1::default_delete&lt;llvm::Module&gt; &gt;)&quot;, referenced from:
        _main in fibonacci-21f462.o
    &quot;llvm::EngineBuilder::~EngineBuilder()&quot;, 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)
  </code></pre>
  <p>The executables of <code>llvm/build/bin</code> generated by CMake auto-build are good, which might prove the tool chain is working.</p>
  <p>But I still want to go into the code and observe.</p>
  <p>Could someone help me out?</p>
  <pre><code>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
  </code></pre>
  <hr />
  <p>update:</p>
  <p>I build a do-nothing <code>a-test.cpp</code>:</p>
  <pre><code>#include &quot;llvm/IR/Verifier.h&quot;
  #include &quot;llvm/ExecutionEngine/GenericValue.h&quot;
  #include &quot;llvm/ExecutionEngine/Interpreter.h&quot;
  #include &quot;llvm/IR/Constants.h&quot;
  #include &quot;llvm/IR/DerivedTypes.h&quot;
  #include &quot;llvm/IR/Instructions.h&quot;
  #include &quot;llvm/IR/LLVMContext.h&quot;
  #include &quot;llvm/IR/Module.h&quot;
  #include &quot;llvm/Support/TargetSelect.h&quot;
  #include &quot;llvm/Support/raw_ostream.h&quot;
  
  int main(int argc, char const *argv[])
  {
      printf(&quot;hello world\n&quot;);
      return 0;
  }
  </code></pre>
  <p>and successfully compile:</p>
  <blockquote>
  <p>clang++ -g -O3 -std=c++11 a-test.cpp <code>llvm-config --cppflags --ldflags --system-libs --libs core executionengine interpreter mc support nativecodegen</code> -o a-test</p>
  <p>./a-test</p>
  </blockquote>
  <pre><code>hello world
  </code></pre>
  <p>but not so good for <code>fibonacci.cpp</code>:</p>
  <blockquote>
  <p>clang++ -g -O3 -std=c++11 fibonacci.cpp <code>llvm-config --cppflags --ldflags --system-libs --libs core executionengine interpreter mc support nativecodegen</code> -o fib</p>
  </blockquote>
  <pre><code>Undefined symbols for architecture x86_64:
    &quot;typeinfo for llvm::CmpInst&quot;, 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)
  </code></pre>