I want to decrease the size of .obj files in a large project I'm working on (I know that linker removes duplicate definitions, but I want to speed up the build process). One of the reasons for their size is that each class which uses std::vector<double>
or std::vector<std::string>
ends up compiling the code of this class and placing it in its .obj file. I tried to explicitly instantiate std::vector<double>
and use extern template
declaration, but it doesn't work -- std::vector
in Visual Studio C++ STL has all methods inline. Short of modifying the STL code (which I won't do), is there any way to force the compiler not to inline instantiated methods and to use the externally instantiated version of std::vector<double>
?
The only thing that comes to mind is writing an inclusion header that defines the std::vector
template (but not its members, those only need to be declared) and include that instead of the vector
standard header.
Then you can explicitly instantiate std::vector<whatever>
in a separate compilation unit and link against that.
To explicitly instantiate the template, don’t use extern template
(that won’t work), just use the following:
#include <vector> // The standard header, not your forward-declaration!
template class std::vector<double>;
What I suggest now I would consider a hack, as it convolute the code to achieve a compiler optimization. Compiler flags should be investigated before you try this.
All in all what compiler puts into the obj files is compiler dependent, but I would suggest to try to inherit an specialization of the vector, and use this specialization. I can imagine that this will make the compiler create an obj file containing the specialization of the vector, allowing all users of the wrapped vector to use this instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With