title: LLVM codegen linker error
body: <p>I'm writing a compiler for the Decaf language. I'm done with the lexer (flex) and parser (bison). Also, I've generated the AST and implemented type checking. I am now trying to implement codegen using LLVM IR.</p>
  
  <p>However, while linking the files, g++ throws up errors saying it cant find llvm::getGlobalContext() etc.</p>
  
  <pre><code>g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` -c ast.cc
  bison -d -t -y --verbose parser.y
  g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` y.tab.c -c
  flex -8     lexer.l
  g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` lex.yy.c -c -lfl
  g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` -c symtable.cc
  g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` ast.o lex.yy.o y.tab.o symtable.o -o dcc -lfl
  ast.o: In function `IntegerConstant::Codegen()':
  ast.cc:(.text._ZN15IntegerConstant7CodegenEv+0x6): undefined reference to `llvm::getGlobalContext()'
  ast.cc:(.text._ZN15IntegerConstant7CodegenEv+0xe): undefined reference to `llvm::Type::getInt64Ty(llvm::LLVMContext&amp;)'
  ast.cc:(.text._ZN15IntegerConstant7CodegenEv+0x1f): undefined reference to `llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)'
  ast.o: In function `CharConstant::Codegen()':
  ast.cc:(.text._ZN12CharConstant7CodegenEv+0x7): undefined reference to `llvm::getGlobalContext()'
  ast.cc:(.text._ZN12CharConstant7CodegenEv+0xf): undefined reference to `llvm::Type::getInt64Ty(llvm::LLVMContext&amp;)'
  ast.cc:(.text._ZN12CharConstant7CodegenEv+0x20): undefined reference to `llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)'
  ast.o: In function `BooleanConstant::Codegen()':
  ast.cc:(.text._ZN15BooleanConstant7CodegenEv+0x6): undefined reference to `llvm::getGlobalContext()'
  ast.cc:(.text._ZN15BooleanConstant7CodegenEv+0xe): undefined reference to `llvm::Type::getInt64Ty(llvm::LLVMContext&amp;)'
  ast.cc:(.text._ZN15BooleanConstant7CodegenEv+0x1f): undefined reference to `llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)'
  ast.o: In function `_GLOBAL__sub_I_ast.cc':
  ast.cc:(.text.startup._GLOBAL__sub_I_ast.cc+0x2b): undefined reference to `llvm::getGlobalContext()'
  collect2: error: ld returned 1 exit status
  Makefile:12: recipe for target 'dcc' failed
  make: *** [dcc] Error 1
  </code></pre>
  
  <p>I've included the LLVM headers in ast.h, and nowhere else (seeing as I only call the codegen() methods in ast.cc</p>
  
  <p>I'd be happy to provide more context if it helps. Also, the project compiles without errors in case I comment out the llvm parts (which for now include only a couple of codegen functions for integer/bool/char constant classes in my AST.</p>
  
  <p>Here's my Makefile, if it helps :</p>
  
  <pre><code>CCC = g++
  CCFLAGS= -O2 -Wno-write-strings -std=c++11
  LLVMFLAGS = `llvm-config --cxxflags`
  LEX = flex
  LFLAGS= -8    
  YACC= bison
  YFLAGS= -d -t -y --verbose
  
  RM = /bin/rm -f
  
  dcc: ast.o y.tab.o lex.yy.o symtable.o
          ${CCC} ${CCFLAGS} ${LLVMFLAGS} ast.o lex.yy.o y.tab.o symtable.o -o dcc -lfl
  
  symtable.o: symtable.h symtable.cc
          ${CCC} ${CCFLAGS} ${LLVMFLAGS} -c symtable.cc
  
  ast.o: ast.cc ast.h
          ${CCC} ${CCFLAGS} ${LLVMFLAGS} -c ast.cc
  
  y.tab.o: parser.y
          ${YACC} ${YFLAGS} parser.y
          ${CCC} ${CCFLAGS} ${LLVMFLAGS} y.tab.c -c
  
  lex.yy.o: lexer.l
          ${LEX} $(LFLAGS) lexer.l
          ${CCC} ${CCFLAGS} ${LLVMFLAGS} lex.yy.c -c -lfl
  
  clean:
          /bin/rm -f lex.yy.* y.tab.* *.o *.output dcc
  </code></pre>
  
  <p>Kindly help.</p>
  
  <h2>EDIT 1:</h2>
  
  <p>I've defined the virtual Codegen() functions inside ast.h, and their implementations inside ast.cc</p>
  
  <p>I'm using </p>
  
  <pre><code>using namespace llvm;
  
  Value *ErrorV(const char *Str) { return 0; }
  
  static Module *TheModule;
  static IRBuilder&lt;&gt; Builder(getGlobalContext());
  static std::map&lt;std::string, Value*&gt; NamedValues;
  
  Value *IntegerConstant::Codegen() {
      return ConstantInt::get(Type::getInt64Ty(getGlobalContext()), val_, true);
  }
  </code></pre>
  
  <p>at the end of ast.cc (as all other functions I wrote were under </p>
  
  <pre><code>using namespace std;
  </code></pre>