In C++ the library can be distributed in a header-only format. I am wondering if it’s possible to convert every library into a header-only library? Or Vice Versa?
And what criteria should be used to decide whether I should distribute the library under header-only? Other than the header-only library has to be recompiled, is there any other considerations that might affect the decision? How does “inline” playing an import role here in header-only library?
Thanks,
In general, anything in a header is considered inline. This can be favorable for some libraries, but in general should be used with great consideration. Placing too much logic in a header can cause several undesirable consequences: inter-reliance of client code, long compilation times if the logic is ever changed, code bloat due to overuse of inline, and others. Some of these issues can be mitigated with a good optimizing compiler. In general I would recommend against placing complex logic in an inline function and hence in a header file, since its what causes the majority of these issues.
As for the reverse, yes, most libraries that are distributed as headers can be converted to a more traditional library. Exceptions to this would be libraries that are heavily reliant on templated code, as this is generally interpreted at compile time.
With regards to criteria, I would say that if the code is heavily templated and/or is primarily made up of simple functions then a header library would be an acceptable choice. Otherwise a normal library is almost definitely a better choice.