title: Template declaration in header file
body: <p>The following code in a single .cpp file works fine</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  template&lt;typename T&gt;
  void print(int a) {
      std::cout &lt;&lt; a &lt;&lt; std::endl;
  }
  
  int main() {
      print&lt;int&gt;(5);
  }
  </code></pre>
  <p>But if I separate the code into the files main.cpp, print.h and print.cpp respectively like this</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;print.h&quot;
  
  int main() {
      print&lt;int&gt;(5);
  }
  </code></pre>
  <pre class="lang-cpp prettyprint-override"><code>template&lt;typename T&gt;
  void print(int a);
  </code></pre>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  template&lt;typename T&gt;
  void print(int a) {
      std::cout &lt;&lt; a &lt;&lt; std::endl;
  }
  </code></pre>
  <p>then gcc gives me the linker error &quot;undefined reference to `void print&lt;int&gt;(int)'&quot;. Why does this happen? What is going wrong when declaring a template function? Is there some other way to do what I want?</p>