(This question is a bit obscure without a lot of motivation so please bear with me.)
For the sake of discussion let's assume I have two files, a.cpp
and b.hpp
. Suppose a.cpp
has the declaration
template <typename T> void foo(T& t);
and somewhere in the code it has the commands:
foo<int>(1234);
foo<double>(12.34);
while b.hpp
holds the actual (templated) definition and nothing else.
Now, a.cpp
, compiled without including b.hpp
will compile into an object file, a.o
, which is expecting to be linked with other files containing the object code for foo<int>
and foo<double>
. In itself, b.hpp
makes no instantiations of foo()
, so it's useless to compile it alone.
However, if I were to determine which instantiations are necessary, and generate a.aux.cpp
containing minimal code to instantiate exactly those templates needed for a.cpp
, I would not have to manually ensure the instantiations are made.
So, how do I go about achieving this? I was thinking along the lines of doing
nm --undefined-only a.cpp | sed 's/^ *//;' | c++filt | grep foo
to get the signatures. Then I need to force their instantiation without actually causing any other side-effects... perhaps define a variable whose type is a pointer to foo<T>
for each value of T
I find.
Anyway, this all seems very kludgy and I was wondering whether some build-related tools do this for you. Or whether I should have another approach altogether.