0

I've long wondered when writing my C++ CMake files which is faster for the compiler:

  1. Putting all my project's cpp files in a single shared .so library using add_library()
  2. Using multiple libraries, one for each class / logical component.

Internet searching has not turned up any relevant articles about this curiosity so I thought I'd run a very simple experiment. I made two version of my project - one with a single library and one with two libraries. I found that, over runs, the single library version was 25% faster when compiling from scratch, and 1% faster when compiling after modifying a single line in one of the files. Granted, my experiments were not very thorough.

This is surprising to me - I've always thought having multiple libraries would speed up compiling of small changes in just one of the libraries, because the compiler wouldn't have to touch other libraries that did not depend on it.

  • Does anyone know a general performance rule about CMake libraries?
  • Can anyone explain why I see this difference?

Thanks!

Update: I'm compiling on Ubuntu 14.04 with gcc 4.8.4

  • 1
    I recommend creating libraries based on theme rather than a monstrous single library. The smaller libraries will allow you to share code easier among various projects. If you need faster compilation or build times, get a faster computer. – Thomas Matthews May 02 '16 at 21:28
  • what kind of library we talking about and what platform? library may be static (`.a`, `.lib`) or shared(`.so`,`.dll`), platform = `OS`, `compiler`, `linker` – fghj May 03 '16 at 01:44
  • I've done some performance measurements while looking into [cmake rebuild_cache for *just* a subdirectory?](http://stackoverflow.com/questions/35436945/cmake-rebuild-cache-for-just-a-subdirectory) While this was mainly checking the performance of CMake itself, it has shown e.g. that an anti-virus software scanning the CMake/compiler/linker every time it's called had a big influence. Since splitting into several libs does multiply your I/O operations, it's a good place to start looking into finding your performance bottle-neck. – Florian May 03 '16 at 07:31

0 Answers0