Nikolay Igotti
Explicit template instantiation in shared libraries
Now a little bit of C++ stuff. Usually I consider this language a bit undercooked, in its "advanced" features, like templates, but sometimes they could be useful. Consider following simple example:a.hpp:
class C {
public:
template <class T> void run(T x);
};
a.cpp:
#include "a.hpp"
#include <stdio.h>
template <class T> void C::run(T x) {
if (x > 0) {
printf("positive\n");
}
}
// Note this!
template void C::run<int>(int x);
b.cpp:
#include "a.hpp"
void foo() {
C().run(3);
}
If we link those files with g++ -fPIC -shared a.cpp b.cpp -o liba.so
unless explicit template instantiation line included, this library will fail to load, due to unresolved symbols.
Posted at 11:09PM Jul 19, 2007 by nike in Sun | Comments[0]
Comments:
Thursday Jul 19, 2007