0

I coded a program importing and making use of many headers from the boost library. After that the compilation time went from 1-2 seconds to 30s+. Since I am always importing the same library I was wondering if there was a way to compile them once and for all to speed up the following compilations.

I am very unfamiliar with shared/static libraries and could not find a tutorial answering my question.

I do not mind have a much larger executable in case this is the price to pay.

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • 2
    For that precompiled headers where invented, it is supported by visual studio. – knivil Mar 11 '16 at 12:20
  • Thanks for the input @knivil , it looks like it is supported by gcc as well, will look into that! – Learning is a mess Mar 11 '16 at 12:25
  • 1
    There are some helpful techniques here; http://stackoverflow.com/questions/373142/what-techniques-can-be-used-to-speed-up-c-compilation-times – sam Mar 11 '16 at 12:26

1 Answers1

1

If you include boost headers in one of your program's headers, and all / many of your program's .cpp files include that header, then the boost headers end up getting included in all of your .cpp files, and get compiled once for each one of them.

To avoid this, you can try to only include boost headers in (one) or a few .cpp files in your project.

You can also use PIMPL idiom, also known as "compilation firewall". The idea is that you expose only an interface in the header that your program uses, and if the implementation requires boost things, then that appears in the .cpp file only so you dont end up including boost everywhere.


Note that header-only libraries don't really have to do with shared vs. static. With shared / static libraries, you have object code of some kind which was obtained by compiling the libraries in advance. With header-only libraries, what you are importing is just template definitions into your code, and your compiler makes use of them. It's closer in spirit to static than to shared linking, but it's not really either.

Chris Beck
  • 15,614
  • 4
  • 51
  • 87