4 years ago

.h/.hpp They are so nasty! Why are they needed? Why doesn't C/C++ have a better system for that? #C #CPP #Programming #coding


.h files.... All C/C++ coders hate them. And they even seem inefficient. Programming languages such as Pascal, C#, Java, don't need them. Why does the language over all languages need something that unsophisticated then?

Well, that is, and I'll put it very extremely harshly, because C is unsophisticated. It's actually a rushed project, which was never meant to be sophisticated, at all. Bell Labs just needed C in order to have a programming language that could easily beat different CPU styles without having to deal with their technical differences (endian issues in particular), but leave the code as much the same for all platforms. C# and Java and such were meant to make coding simple on the programmer's end, so when it comes to sophistication, those languages outrank C by far.

No C is extremely primitive in every single way. And that's why it also lacks some features that later programming languages have.

C# and such have their own library system, and have their compilers set everything up automatically. Technically the headers are required too there, but the compilers are so sophisticated that you don't have to worry about that anymore. Frankly, the C# compiler is actually a series of stuff. A code analyzer which looks at what you need, also based on the project file, the compiler itself, and a linker. C only has a compiler. The linker C uses is just a linker you can use for any kind of object code.

To fully understand you have to know a bit about how executable files are actually being created, regardless of the programming language used, but the simplest way to go is to get it from the very core, machine language or its closest equivalent assembler. All assembler files are basically just being literally translated into object files (.o or .obj). The coder has to manually assign where to look inside the object files to find the stuff needed, but frankly assembler itself doesn't know your references. It just reads out whatever you ask. The linker merely puts it all together and make all references fit... well, that's the simplistic version of the story.

Now C comes in. C is an unsophisticated language in general, but still more sophisticated than assembler. C does actually not support libraries. We actually "cheat". C is just a kind of an assembler made easy. C can tackle endian issues and the differences between CPUs, but still all C can do is create object files, just like assembler. Now as C doesn't know what is inside other object files (C itself doesn't read that out during compiling), it cannot understand the identifiers you use unless they have been declared in your C code... Hey, now the header files come in. They tell C at least which functions and stuff you have so C does at least know what the stuff is. What's in there, C doesn't have a clue, but C doesn't need to know either. It can now create some ready to go references and that's all it needs. And when the linker comes the linker does is magic and ties all the stuff together and create the executable. And that's all there's to it.

When it comes to languages such as Pascal, the headers are needed there too, however how does Pascal know what headers in its units (the Pascal equivalent to libraries) when they are compiled without having to compile them again. That's because Turbo Pascal and its equivalents (Delphi and Free Pascal) create Compiled Unit files. .TPU files in Turbo Pascal and Delphi and PPU in Free Pascal. The .TPU file is just a file that contains both the object code and the headers generated from the unit source code.

Why can't C generate its own headers?
Although C does change over time, C has to follow a certain standard now. Second C is not designed to create executables were modern versions of Pascal and C# are primary set up for this. C has to be completely focused on the pure machine code even beyond the scope of operating systems. C is still used for developing kernels for starters, but quite often also for software that has to operate without any help of OSes at all (OSes are by default unstable, and that can threaten some projects). Self reliance is the still key in C.

How about C++?
C++ is nothing but an advancement on C. Bjarne did want to make OOP more accessible, but didn't want to re-invent the wheel so so he merely chose to enhance an existing language able to work on low level. C was simply chosen because it was most used. C++ still has to produce code that can easily be picked up by C, and C++ also has to be able to easily pick up code written in C. It's only logical that C++ would therefore follow the same setup as much as possible. Many software written today combines C and C++, and this get up makes that more easy, well, if you know what you are doing that is.

What is mostly inefficient from human view, is not always inefficient from computer's view. C and C++ are very efficient from computers view BECAUSE of their lack of sophistication. It takes therefore more work, but the end product can live better... at least, that's the idea.

Truth is, the higher level languages still use headers, just like C. The sophistication of the compiler packages only hid that pretty well. That's all.

General Blogs: https://trickyjeroenbroks.tumblr.com/
Indie Reviews: https://jeroensindiereviews.tumblr.com/
Game Classics Reviews: https://trickygameclassics.tumblr.com/



0 comments

Loading...

Next up

A #phrase of #wisdom from the French scientist and philosopher #BlaisePascal Two stories, the one tied to this game (new version), and a prequel novel based on this game have this quote. Perhaps you understand why....

Killing only brings you so far!

BallPlay future

Our 2024 Advent Calendar has opened! Day 24: @NickPerson is a great Creator who makes Five Nights at Freddy's fan games! Accept the quest and give them a follow to get Coins and a seasonal sticker!

Why do people wanna be a #moderator? Is it really such a #cooljob?

Our 2024 Advent Calendar has opened! Day 23: @Distant-Dimensions is a great Creator who creates 2D and 3D art and animations! They're also a game dev and music producer! Accept the quest and give them a follow to get Coins and a seasonal sticker!

Celebrities (almost) killed by one of their biggest fans

A few facts and fables about fire.

Our 2024 Advent Calendar has opened! Day 20: @MarcDWyz is a great Creator who develops fan games! Accept the quest and give them a follow to get Coins and a seasonal sticker!

Can you see why this pyramid deal could never be solved from the start (regardless what the already removed cards are)?