title: C++: Compiler and Linker functionality
body: <p>I want to understand exactly which part of a program compiler looks at and which the linker looks at. So I wrote the following code:</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  #include &lt;string&gt;
  
  class Test {
  private:
      int i;
  
  public:
      Test(int val) {i=val ;}
      void DefinedCorrectFunction(int val);
      void DefinedIncorrectFunction(int val);
      void NonDefinedFunction(int val);
  
      template &lt;class paramType&gt;
      void  FunctionTemplate (paramType val) { i = val }
  };
  
  void Test::DefinedCorrectFunction(int val)
  {
      i = val;
  }
  
  void Test::DefinedIncorrectFunction(int val)
  {
      i = val
  }
  
  void main()
  {
      Test testObject(1);
      //testObject.NonDefinedFunction(2);
      //testObject.FunctionTemplate&lt;int&gt;(2);
  
  }
  </code></pre>
  <p>I have three functions:</p>
  <ul>
  <li><p>DefinedCorrectFunction - This is a normal function declared and defined correctly.</p>
  </li>
  <li><p>DefinedIncorrectFunction - This function is declared correctly but the implementation is wrong (missing ;)</p>
  </li>
  <li><p>NonDefinedFunction - Only declaration. No definition.</p>
  </li>
  <li><p>FunctionTemplate - A function template.</p>
  <p>Now if I compile this code I get a compiler error for the missing ';'in DefinedIncorrectFunction.<br />
  Suppose I fix this and then comment out testObject.NonDefinedFunction(2). Now I get a linker error.
  Now comment out testObject.FunctionTemplate(2). Now I get a compiler error for the missing ';'.</p>
  </li>
  </ul>
  <p>For function templates I understand that they are not touched by the compiler unless they are invoked in the code. So the missing ';' is not complained by the compiler until I called testObject.FunctionTemplate(2).</p>
  <p>For the testObject.NonDefinedFunction(2), the compiler did not complain but the linker did. For my understanding, all compiler cared was to know that is a NonDefinedFunction function declared. It didn't care for the implementation. Then linker complained because it could not find the implementation. So far so good.</p>
  <p>Where I get confused is when compiler complained about DefinedIncorrectFunction. It didn't look for implementation of NonDefinedFunction but it went through the DefinedIncorrectFunction.</p>
  <p>So I'm little unclear as to what the compiler does exactly and what the linker does. My understanding is linker links components with their calls. So for when NonDefinedFunction is called it looked for the compiled implementation of NonDefinedFunction and complained. But compiler didn't care about the implementation of NonDefinedFunction but it did for DefinedIncorrectFunction.</p>
  <p>I'd really appreciate if someone can explain this or provide some reference.</p>
  <p>Thank you.</p>