r/C_Programming • u/Majestic-Instance704 • Jul 25 '26
why do header files contain only function declarations instead of full code body ? why have the code in a sepreate lib files ?
for context I am trying to learn gamedev on my own and setting up raylib/sfml confused me , then I learnt that the ( include )files only contained function declarations and the actual code was in( lib ) files (mind you but it is a weird name) , wouldn't it be more convenient to have them in the same file instead of linking ?
sorry if I sound stupid.
104
Upvotes
View all comments
2
u/yuehuang Jul 25 '26
The true and only answer is legacy and backward compatibility. Newer programming language don't require you do this separation. They are often just one file that the compiler splits out the interface and implementation for you. This would had been expensive back then, but it is now a rounding error with today's hardware.
C and therefore C++ revolves around Linux/GNU as their default toolchain. Using more than that needs a generational reason to break. Clang is the closest and it is still a side project compared to GNU.