Benefits of header-only libraries

前端 未结 5 1422
天涯浪人
天涯浪人 2020-11-28 01:46

What are the benefits of a header only library and why would you write it that way oppose to putting the implementation into separate file?

5条回答
  •  温柔的废话
    2020-11-28 02:32

    There are situations when a header-only library is the only option, for example when dealing with templates.

    Having a header-only library also means you don't have to worry about different platforms where the library might be used. When you separate the implementation, you usually do so to hide implementation details, and distribute the library as a combination of headers and libraries (lib, dll's or .so files). These of course have to be compiled for all different operating systems/versions you offer support.

    You could also distribute the implementation files, but that would mean an extra step for the user - compiling your library before using it.

    Of course, this applies on a case-by-case basis. For example, header-only libraries sometimes increase code size & compilation times.

提交回复
热议问题