7 ms·
Increasingly popular are the "single header libraries", which were popularised by Sean Barrett[0]. It's as simple as downloading the header, putting it somewher
by saticmotion 10y ago
Increasingly popular are the "single header libraries", which were popularised by Sean Barrett[0]. It's as simple as downloading the header, putting it somewhere in your tree and #including it where you need it. It's especially useful for redistributing libraries, but I've also found it useful to create these in my own projects.
[0]https://github.com/nothings/stb https://github.com/nothings/stb
- wott 10y agoOh, this kind of horror I've met lately has got a name? Well, I learned something today. Thanks ;-)
- stagger87 10y agoYou can copy the functions into a .c file if it makes you feel better.
- saticmotion 10y agoDespite what the code may look like, they all have a very easy to use API. Take stb_image.h for example. Most people need just two functions, stbi_load(), which loads any supported image format to a byte array. And stbi_image_free(), which frees the data. But if you need anything more than that it's all there. e.g. loading from memory, loading via callbacks, support for HDR images, support for custom allocators, preprocessor flags let you exclude code for unused image formats, etc etc
- chj 10y agotruetype font rendering: stb_truetype.h: LOC=3287 Wow. I am speechless. Going to dive in another day.
- flukus 10y agoDoesn't this hurt compile times and increase exe size?
- ndesaulniers 10y agoYes, but unfortunately for C++ libraries that make heavy use of templates, it's the only solution. Because templates need to be specialized to generate any code, they can't be compiled ahead of time into shared or static libraries. So in C++ you have this stupid non-sense.
- andrepd 10y agoStupid nonsense? What would your alternative be, mind you, an alternative that would be as powerful and performant? Is there even such a thing?
- reikonomusha 10y agoLisp does fine with separately compiled macros, which are more powerful than templates.
- dfox 10y agoWith Lisp you also need to have the macro definition ready when compiling code that depends on it. This is essentially the problem that all the various "system definition facilities" try to solve in at least somewhat usable manner.
- kazinator 10y agoAlso, the package definition. You need the package definition for the source-code-read time of whatever you're compiling, and the macros for its macro-expansion time. Practically speaking, these are tied together.
- swift 10y agoInstantiating the templates at link time would probably make more sense. With LTO we're already deferring a significant portion of the compilation process to link time so this is an obvious extension. This issue was also on the mind of the C++ standards committee when they began work on the new module system. I'm not sure how the current spec behaves with respect to template instantiation, though.
- naner 10y agoIs having large amounts of code in headers common in real world C code (aside from stb)? Seems like it would be a nightmare to chase down issues, though maybe compiler error messages have gotten smarter since I've last dealt with C.
- markrages 10y agoHeader files are not special in C. It is a common convention to put interface in .h and implementation in .c files. But the preprocessor expands all the #includes together into one big file before the compiler proper attempts to compile it. Modern compilers have error messages that show you the whole chain of #includes, and on top is the filename and line number, whether it ends with ".c" or ".h" or anything else.
- Cyph0n 10y agoDoesn't Boost have a number of header-only libs? I believe there is a header-only regex implementation for example.
- mwfunk 10y agoC++ is a little different in that templates have to go in header files. A lot of template-based C++ libraries are entirely templates in header files. It's weird because it does feel like it violates the separation of interface and implementation. On the other hand, it's really efficient. It has the advantages of doing generics with the C preprocessor (efficiency and de-duplication of code, if nothing else), minus many of the disadvantages of doing generics with the C preprocessor. Putting non-template code or data in headers is something I've seen people do for expediency, but it's more trouble than it's worth IMO. As soon as two files in the same project include it, you risk a future headache.
- ploxiln 10y agoThe compiled result of template-heavy C++ can be very efficient, but the compilation process is notoriously inefficient. Just to be clear :)
- gpderetta 10y agoTechnically you can put the template definition in a separate translation unit and rely on explicit instantiation. The linker will tell you exactly what you need to instantiate. It is tedious and seldomly done. /Pedantic
- Annatar 10y agoIt is worth noting that header files should not include code, just macro and variable definitions.
- clarry 10y agoThere is nothing wrong with having code in headers.
- pjc50 10y agoIf you put a bare function or variable in a header, then use it in multiple compilation units, the linker will reject your multiply-defined symbols. (Templates get de-duplicated).
- vonmoltke 10y agoUnless you mark them 'static', which is common practice for constants and small inlined functions.
- gpderetta 10y agoInline is the keyword here (litterally). Inline, in C++, is defined as disabling the requirement of having exactly one definition of a function (it is also a weak hint for the compiler to perform inlining). Template functions are implicitly inline. Inline in C has a similar but subtly different definition.
- Annatar 10y agoIt's called a header for a reason, unless we are now redefining the meaning and intent of headers, so that people could stick C++ template code in them?
- clarry 10y agoOh, where is the officially sanctioned definition of the meaning and intent of a header? What we call them doesn't matter. There is nothing wrong with putting code in one. You can call them source files if you prefer.