title: explicit instantiation of parameterized template methods
body: <p>I've two different template classes, one of them using as parameter an object of the another template class. i'm getting an error message:
       collect2: ld returned 1 exit status
       make: <em>*</em> [main] Error 1</p>
  
  <p>I'm working with explicit instantiation of templates and I found some related posts but they were mostly related to instantiations of one single template class. In my case, I've 3 files per class (.h, .cpp, .inc following <a href="http://www.parashift.com/c++-faq/templates.html#faq-35.13" rel="nofollow">http://www.parashift.com/c++-faq/templates.html#faq-35.13</a> suggestion).</p>
  
  <h3>foo.h</h3>
  
  <pre><code> #ifndef FOO_H
   #define FOO_H
   template &lt; class S &gt;
   class Foo{
      public: ...
      private: ... 
   };
   #endif
  </code></pre>
  
  <h3>foo.cpp</h3>
  
  <pre><code>  ...
    template &lt; class S &gt;
    Foo&lt;S&gt;::Foo(){ .... }
    ...
    #include "foo.inc"
  </code></pre>
  
  <h3>foo.inc</h3>
  
  <pre><code> template class Foo&lt;int&gt;;
   template class Foo&lt;float&gt;;
  </code></pre>
  
  <h3>bar.h</h3>
  
  <pre><code> #ifndef BAR_H
   #define BAR_H
   #include &lt;mylib.h&gt;
   template &lt; class T &gt;
   class Bar{
      public: ...
             template &lt;class S&gt;
             void doSomething(Foo&lt;S&gt;*);
      private: ... 
   };
   #endif
  </code></pre>
  
  <h3>bar.cpp</h3>
  
  <pre><code>  ....
    template &lt;class T&gt; template &lt;class S&gt;
    void Bar&lt;T&gt;::doSomething(Foo&lt;S&gt;*p){
        ....
    }
    .....
    #include "bar.inc"
  </code></pre>
  
  <h3>bar.inc</h3>
  
  <pre><code> template class Bar&lt;int&gt;;
   template class Bar&lt;float&gt;;
   template void Bar&lt;int&gt;::doSomething(Foo&lt;int&gt;*);
   template void Bar&lt;float&gt;::doSomething(Foo&lt;float&gt;*);
  </code></pre>
  
  <h3>mylib.h</h3>
  
  <pre><code> #include &lt;foo.h&gt;
   #include &lt;bar.h&gt;
  </code></pre>
  
  <h3>main.cpp</h3>
  
  <pre><code>#include &lt;mylib.h&gt;
  #include &lt;common.h&gt;
  
  int main(){
      Foo&lt;float&gt; * pFoo = NULL;
      pFoo = new Foo&lt;float&gt;();
  
      Bar&lt;float&gt; * pBar = NULL;
      pBar = new Bar&lt;float&gt;();
      pBar-&gt;doSomething(pFoo);
  
      delete pFoo;
      delete pBar;
  
      return (0);
  }
  </code></pre>
  
  <p>Does anybody see any problem/bug on the way I'm instantiating the objects? The complete error message is (I changed the name of the classes and simplified the code so as to be more understandable):</p>
  
  <pre><code> g++ -O3 -Wall main.cpp -o ../bin/main -I../inc -L../lib -lmylib -lm
   /tmp/cciJHsKr.o: In function `main':
   main.cpp:(.text+0x19f): undefined reference to `void Bar&lt;float&gt;::doSomething&lt;float&gt;(Foo&lt;float&gt;*)'
   collect2: ld returned 1 exit status
   make: *** [main] Error 1
  </code></pre>
  
  <p>Note that, I compile both foo.cpp and bar.cpp separately, since I'm generating a library.</p>
  
  <pre><code> // Makefile for foo.cpp &amp; bar.cpp
   INC=./inc
   SRC=./src
   LIB=./lib
   OBJ=./obj
  
   CC=g++
   CFLAGS=-O3 -Wall
  
   mylib: $(LIB)/mylib.a
        echo "mylib was created!..."
  
   $(LIB)/mylib.a: \
   $(OBJ)/bar.o \
   $(OBJ)/foo.o 
  ar csr $(LIB)/mylib.a \
   $(OBJ)/bar.o \
   $(OBJ)/foo.o \
  
  
   $(OBJ)/bar.o: $(SRC)/bar.cpp
        $(CC) -c $(CFLAGS) $(SRC)/bar.cpp -I$(INC) \
        -o $(OBJ)/bar.o
  </code></pre>