why do header files even exist?

preview_player
Показать описание
So why do we use header files? Are they just there to look pretty? Is there actually a reason that we include them in all the code we write? In this video we explore how the compiler works, how the linker works, and how header files tie the whole process together.

🔥🔥🔥 SOCIALS 🔥🔥🔥
Рекомендации по теме
Комментарии
Автор

Header files are only there because it makes it easier for the parser. They are technically not needed but this is the legacy of C and other languages so it's stuck around. I don't mind them personally but it's hard to justify their existence because removing them is just an technical problem which can be solved with a little effort.

HairyPixels
Автор

You can also use a header files to declare structs w/o exposing their fields (you define them in the source file). That way you ensure that users of your library operate on structs only through pointers to them and API you provided, so you achieve encapsulation.

DanielTredewicz
Автор

Dealing with header files in c/c++ feels like doing the job of the compiler. I understand the need of a header file when linking with dynamic/static library. BUT in 99% of header files I wrote I also wrote the source file.

JkaBG
Автор

Something you didn't mention is that header files are included in the precompiler phase. The line _#include__ "myheader.h"_ is basically an instruction that tells the precompiler to replace this line with the contents of _myheader.h_ . This is why header inclusion is a # command, and also why headers start with a #ifndef command, to make sure that the same header isn't included more than once by the precompiler. It also means that you don't have to limit yourself to declare functions on the header file, you can technically write any code inside a header file and it will compile just fine, though it can lead to problems if multiple source files use the same header (multiple definition error).

snoW_
Автор

Be careful with those $(pwd) calls, they undergo bash word expansion and can be broken into two different arguments if your path has a space in it, always quote those things with double quotes ", same if you were to do "$PWD" (just reading the variable PWD instead of running the pwd command in a subshell)

rafaelkuhn
Автор

I know someone said you are a gem for embedded, but you are also one for game dev and low levelers with hardware restraints. I love it.
I am just a little to deep, but I saw 'header file' reading the raylib documentation as being modular and interchangable.
And I would be lying to say I had any clue what a header file or what a C file with a header file really...means.
This video clears up alot of over complicated imagined semantics. Thanks!

twenty-fifth
Автор

Instead of "why do header files even exist" you explained how header files work, but the question from the title of the video still stands.

You may have noticed that header files are almost exclusive to C/C++ languages, other languages somehow don't need them. So why do header files even exist?

For those wondering, header files exist mostly for historical reasons. Since memory was very limited back when C was developed, compilers couldn't afford to keep track of modules themselves, so it became the job of the programmer. Modern compilers are far less hardware restricted, which allows them to favor developer experience over efficiency.

ex-xghh
Автор

6 years after completing classes, i learned why <> and "" are used in the includes.

drditup
Автор

Can you make more videos about the building process?
I really enjoyed this one

abghany
Автор

can’t think this man enough for his absolutely no BS approach to systems computing/programming. he could have done 2 videos on zig or 3 videos on rust and just touched the surface of those .. for content. but he goes deep into basics of how real world systems works … not BS toy projects. thank you you kind man.

mintoocool
Автор

Headers are basically here to make compiling easier. They're a remnant of the past that stuck around.

You see, the early C compilers were dumb so if you tried to call a function that was declared later on in the source, they couldn't find it. Sometimes you could solve this by shuffling the functions around, but sometimes it didn't work when the functions depended on each other. (e.g. Function A needs to call function B that in turn either calls Function C or function B again in some recursion scenario).

To solve that dumbness of the early C compilers, the header files were invented where all the functions would be declared beforehand so the compiler won't think that a function that exist, doesn't.

Nowadays, the compilers such as GCC and MSVC are actually pretty smart about declarations and can find the function that was declared later on in the code, but the header files stuck around.

UltimatePerfection
Автор

"Declaration" and "definition" have very specific meanings in C. You got it right at one point in the video, where you said the declaration is in the header but not the definition, but you kept using the wrong terms throughout the rest of the video. K&R assumed an implicit declaration of unknown functions as returning an int - parameters were not part of the signature - but ANSI made declarations for functions mandatory and added parameters.

herberttlbd
Автор

I’ve never found header files an issue, if anything I find them a blessing. A nice readable prototype of an implementation for a function that I don’t want to know the implementation details from. I hear everybody complain about it and it makes no sense to me as to why. I hate those people who do implementations also in header files.

CallousCoder
Автор

This reminds me of my earliest programming experience where it was still common to do the compile and linking semi manually. It was also common to have an automation, but still when I started, and this was not as long ago as you might think, manually compiling and linking simpler programs was still considered common practice.

charlesmayberry
Автор

At first, I didn’t appreciate headers, I considered them cumbersome. I always thought that you’re repeating yourself. Then I started writing a rendering engine, and oh are they a lifesaver. Declaring one header file that’s used across 4 APIs has saved me a lot of time and effort instead of writing one for each.

BerayTriangle
Автор

I love your work man, although I know this info already but I'm enjoying watching you explaining it with this much practical details.

xmmn
Автор

Dude you have the best vids ever. Engaging AND technical.

for-lack-of-a-better-name-j
Автор

Basically the implicit function declaration already knows the arguments and return of the function.

If compiled to assembly/binary, it can pass its parameters into appropriate registers and read back an expected return from the return register.

But the binary also needs to know the address of the function, to jump there and continue with the code execution of that function. To find out this address, the files need to be linked.

If no matching function can be found at this time, the linker can only give up.

In many modern strongly typed languages and IDEs, we would expect the IDE to already have done that lookup in real time as we type the code, so we may not even be allowed to start compiling.

TKSSLCHN
Автор

thank you, you not only explained me header files but also the point of using Makefiles towards the end ❤

thisaintmyrealname
Автор

Technically, in the last step you could just list all your .c files instead of .o files, but it would mean you have to compile both files every time even if only one of them changes.

mihiguy