r/cprogramming • u/Barracuda-Bright • 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
24
u/flyingron Mar 30 '26
There's no distinction in the language beteween .h and .c files. Header files are only distinguished in the fact that they are #included rather than being compiled directly. The .h and .c naming is purely convention. As is what you put in the various files. The only hard rules is you can't define the same thing more than once.
What we have here is apparently some embedded system where the programmer (either out of design or necessity) decided the entire program must be one translation unit, so his split up parts are all put in include files, rather than splitting them across multiple translation units and linking the resulting objects together.
2
u/Conscious_Support176 Mar 31 '26
According to the readme, it is intended to be a header-only library in that it doesn’t require you to link anything. It is intended to be used by #including the primary .h in your source.
Seems it’s a tool that works alongside your project as opposed to a library of functions that can be used by your project, so you wouldn’t be including it in two source files.
5
u/JescoInc Mar 31 '26
Ahh, I see where the confusion is. Header files don't have to just be the contract for function signatures. They can also have implementation details. They can be strong or weak implementations that can be overridden by c files if they exist.
This is why you can find things like header only libraries where everything is implemented for you, but you need to link against the header files you need and use them in your application code.
As flyingron said, there is no distinction in the language between .h and .c files.
2
u/microOhm Mar 30 '26
Why do you think they are not really header files?
Also there are .c/.cpp files in the repo.
2
u/Srslyredit2 Mar 30 '26
I was told this makes projects less cluttered by only worrying about one .h file instead of a .c and .h file
2
u/Low_Lawyer_5684 Apr 03 '26
I am the author of this. The reason was simple: the Arduino IDE, which is used to compile this library tries to compile all .c files in the directory. And this is not what I wanted: the shell itself was once ago a single file. Later it become too big and I split it into several files. There is no other reasons. Properly, I should create .h/.c pairs, and let it compile as it should but.. Then I need to declare global functions. Right now there are no global functions - all of them are static. Should be renamed to .inc instead of .h but then I loose syntax highlighting :)
So right now it is a single .c file which #includes bunch of .h files where actual implementation lies. It has nothing to do with modern C++ "header only" code
1
u/CreepyValuable Apr 01 '26
I hate when they do that.
1
u/jwzumwalt Apr 02 '26
.h files are simply include files. I write most of my code without .h files. Every other programming language seems able to live without them - and I do too.
1
u/imaami Apr 04 '26 edited Apr 04 '26
This is an increasingly common form of brainrot. It's basically a meme that escaped a very narrow niche of situations where "header-only" makes practical sense. It's gotten to the point where it's sadly common for people to not understand why a split between interface declaration and implementation is a thing.
"Header-only" in a project description is strongly associated with a lack of rudimentary C project design, and ignorance about what a package manager even is.
1
u/Dontezuma1 Apr 05 '26
It’s just a style of library code. It’s less efficient at compile time but easier to export. Ok for small libs I guess. Less good for a system
1
u/ffd9k Mar 31 '26
These are header files. You can put full functions definitions in header files if you declare them as "static". These functions are then not linked as usual, but basically just pasted into the translation unit of the source file that includes the header file.
Doing is is usually not a good idea, because it leads to unnecessary dependencies between header files, slow compile times and can increase the size of the compiled program because of multiple copies of the same function.
It is sometimes done to allow the compiler to inline the function into functions that call it for better performance, but this is not really necessary nowadays thanks to link-time optimization.
1
u/somewhereAtC Mar 31 '26
It's possible that the author learned how to structure programs while using an ancient assembler that allowed only one compilation unit. Old habits die hard.