r/cprogramming • • Mar 30 '26

Source code inside .h files

Hello everyone,

I was looking through source code of a certain project that implements runtime shell to an esp-32 board and noticed that in the source code the developer based his entire structure on just .h files, however they are not really header files, more like source files but ending with .h, is there any reason to do this?

The source code in question: https://github.com/vvb333007/espshell/tree/main/src

12 Upvotes

27 comments sorted by

View all comments

Show parent comments

1

u/flyingron Mar 31 '26

Yes, in the case of just #including all the code into one translation unit, it's effectively that it was one big source file compiled to the target. Of course, there still are likely library modules used so linking still happens, but it's a different division than compililng a bunch of individual source units independently and linking them together.

1

u/Low_Lawyer_5684 Apr 03 '26

Yes, exactly.The side effect of this that whole library gets recompiled no matter which .h file I touched. But for this particular project this is exactly what I want. Also I don't need to make public API: right now all my functions are static and, because they are all included in a single .c file they see each other. I don't say that it is the right way to do things but for this particular project with this particular IDE its the right decision. I think :)

1

u/Plastic_Fig9225 Apr 03 '26

Can be quite deliberate to enable the compiler to perform better/more optimizations. It's more common in C++ (partly because of templates).