-1

I have a header-only C++ library that is mainly composed of templates with all implementations in header files.

Now to reduce compiling times, I'm trying to make it a .so (shared library) file and link it externally from an application. Will it always re-compile whenever application is compiled?

Does being in Linux make a difference, using g++?

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97

1 Answers1

3

Yes and no.


Yes:

Your templates will all need to be recompiled whenever the application using them is recompiled.

No:

This is not because they will be in a shared library. They won't be. They will be in the headers still.

Unless they're not. If you can explicitly instantiate everything you need, then you can just bake them into your library like you would a non-template thing. There is a wealth of information about this somewhere on this page.


If you want to reduce compilation times and have loads of templates, it's usually precompiled headers that you want to look into.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • So, even if I give it with .so file, I still need to use some header files to define them but this also forces me to add implementations in headers too. And there is no way to use/fetch whatever(compiled to machine language) inside .so (with any version of compiler)file? – huseyin tugrul buyukisik Jan 11 '19 at 12:59
  • Is it because the ".so compiling" doesn't know anything about the applications that will use it yet? – huseyin tugrul buyukisik Jan 11 '19 at 13:02
  • 1
    @huseyintugrulbuyukisik Yeah, basically. It's because of how templates are [usually] instantiated at compile-time, not link-time. – Lightness Races in Orbit Jan 11 '19 at 13:16