I am trying to understand extern templates, but i can't get it to work. My goal is to compile some instanciations if Foo<>
in a seperate compilation unit in order to save compilation time. In my code base, the template argument is a enum class
, so in theory I am able to compile all instanciations in a compilation unit and link them with the rest of the project.
Here is a little example I come up with:
//Foo.hpp
#pragma once
template <class T>
struct Foo {
Foo();
~Foo();
};
extern template struct Foo<int>;
//Foo.cpp
#include "Foo.hpp"
template struct Foo<int>;
//main.cpp
#include <iostream>
#include "Foo.hpp"
int main(int argc, char **argv) {
Foo<int> foo;
return 0;
}
To for compilation i used a makefile, that boils down to this:
g++ -c ../Foo.cpp ../main.cpp
g++ Foo.o main.o
The output I get with gcc 7.1.1 and basically the same with clang 4.0.1 is:
main.o: In function `main':
main.cpp:(.text+0x27): undefined reference to `Foo<int>::Foo()'
main.cpp:(.text+0x38): undefined reference to `Foo<int>::~Foo()'
My main question is, why is Foo<int>::Foo()
and Foo<int>::~Foo()
not compiled into Foo.o
?
Aucun commentaire:
Enregistrer un commentaire