title: Undefined reference to explicitly instantiated template function
body: <p>I have a class with a template method that is declared in a header and defined in a cpp file, so</p>
  
  <pre><code>// A.h - declaration
  struct A
  {
      template&lt;typename T = int&gt; void fun();
  };
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>// A.cpp - definition
  #include &lt;iostream&gt;
  #include "A.h"
  
  template&lt;typename T&gt; void A::fun()
  {
      std::cout &lt;&lt; "Hello world template" &lt;&lt; std::endl;
  }
  
  // Explicit instantiation
  template void A::fun&lt;int&gt;();
  </code></pre>
  
  <p>Now, somewhere else, in a test, I have</p>
  
  <pre><code>#include "A.h"
  
  int main ()
  {
      A a;
      a.fun();
  }
  </code></pre>
  
  <p>and since I explicitly instantiated the function for <code>int</code> template parameter, all is well. Or should be!</p>
  
  <p>My code links fine with GCC 7.3.0 but fails to link with clang 6, which reports an undefined reference to function <code>fun</code>.</p>
  
  <p>What's going on here? Is there anything else that I should take care of? Perhaps something that GCC takes for granted while clang doesn't?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit</strong></p>
  
  <p>@WhozCraig, @Neil Butterworth here are the linking commands for the two toolchains.</p>
  
  <p>GCC:</p>
  
  <pre><code>/usr/bin/g++  -g   CMakeFiles/test_generator.dir/generator/generator.cpp.o CMakeFiles/test_generator.dir/test_utils.cpp.o  -o test_generator -Wl,-rpath,/home/mleoni/PhD/ABI/libcellml/build/src:/home/mleoni/PhD/ABI/libcellml/build/tests/gtest ../src/libcellmld.so.0.1.0 gtest/libgtest_main.so /usr/lib/x86_64-linux-gnu/libxml2.so gtest/libgtest.so -lpthread
  </code></pre>
  
  <p>clang: </p>
  
  <pre><code>/usr/bin/clang++  -g   CMakeFiles/test_generator.dir/generator/generator.cpp.o CMakeFiles/test_generator.dir/test_utils.cpp.o  -o test_generator -Wl,-rpath,/home/mleoni/PhD/ABI/libcellml/build/src:/home/mleoni/PhD/ABI/libcellml/build/tests/gtest ../src/libcellmld.so.0.1.0 gtest/libgtest_main.so /usr/lib/x86_64-linux-gnu/libxml2.so gtest/libgtest.so -lpthread
  </code></pre>
  
  <p>@IdeaHat: that didn't resolve the issue unfortunately, I am still getting the same error</p>
  
  <p><strong>Edit 2</strong>
  Since the code I am actually trying to compile is [obviously] not that one, I tried to compile this MWE and both GCC and clang link it fine with just <code>cc A.cpp main.cpp</code>.</p>
  
  <p>I am now trying to understand what's the difference between this minimal case and the code that I actually have.</p>