Why do I get missing symbols for an explicit template specialization in a static library?
If I compile the following code:
//
// g++ static.cpp -o static.o
// ar rcs libstatic.a static.o
//
#include <iostream>
template < typename T >
struct TemplatedClass
{
void Test( T value )
{
std::cout << "Foobar was: " << value << std::endl;
}
};
template struct TemplatedClass < long >;
I get a static library and if I run nm on the libra开发者_JAVA百科ry I get the following results:
testcase% nm libstatic.a | c++filt | grep TemplatedClass
0000000000000207 s global constructors keyed to _ZN14TemplatedClassIlE4TestEl
0000000000000300 s global constructors keyed to _ZN14TemplatedClassIlE4TestEl.eh
0000000000000118 T TemplatedClass<long>::Test(long)
00000000000002a0 S __ZN14TemplatedClassIlE4TestEl.eh
If however I compile the following code, which is the same except that I have added an explicit specialization of the templated class...
//
// g++ static.cpp -o static.o
// ar rcs libstatic.a static.o
//
#include <iostream>
template < typename T >
struct TemplatedClass
{
void Test( T value )
{
std::cout << "Foobar was: " << value << std::endl;
}
};
template <>
struct TemplatedClass < long >
{
void Test( long value )
{
std::cout << "Value was: " << value << std::endl;
}
};
template struct TemplatedClass < long >;
... and rerun the same command:
testcase% nm libstatic.a | c++filt| grep TemplatedClass
testcase%
I get no matching symbols. For some reason the compiler does not instantiate the template even though I explicitly asked it to.
Can someone explain to me what is going on here?
You have member function definitions within class (template) definitions. This causes the member functions (templates) to be inline
. That doesn't matter so much for the member function of the template class, since its linkage requirements are determined more by the nature of its instantiation(s).
But in the second example, the member function void TemplatedClass<long>::Test(long)
is not a function template and is still inline
. So the compiler is not required to do anything with it unless it's used, and it must be defined in all files where it's used. Since you claim this is in a static.cpp file, an inline function is probably not what you want.
I think you'll get results more like you're expecting if you change things to:
template <>
struct TemplatedClass < long >
{
void Test( long value );
};
void TemplatedClass<long>::Test( long value )
{
std::cout << "Value was: " << value << std::endl;
}
And when you define an explicit specialization, you probably don't also need an explicit instantiation (if that's even legal).
精彩评论