Re:Adding Modules to C in 10 Lines of Code [pdf]
by agent281 on Monday June 27, 2022 @10:54AM
(#31895401)
I never really had a good grasp of C. Why is this code novel? It looks like importing a function in any language.
[ Parent
| on HN ]
Re:Adding Modules to C in 10 Lines of Code [pdf]
by phaer on Monday June 27, 2022 @11:00AM
(#31895514)
It's including another C file, normally you'd just include a "header file" in C and compile each C file separately.
Re:Adding Modules to C in 10 Lines of Code [pdf]
by usrn on Monday June 27, 2022 @12:03PM
(#31896512)
For those who don't use C: header files are more or less an interface description and (usually) don't include any code. The code is added from external objects during the second "linking" step.
Re:Adding Modules to C in 10 Lines of Code [pdf]
by agent281 on Monday June 27, 2022 @12:17PM
(#31896720)
Oh, I see: including a header file injects forward declarations into the calling file. Each c file gets compiled separately into object code unless you directly import them like the code above is doing. In that case it essentially dumps the full text of the file into the calling file, which causes serial compilation of a single object file.
Unless I'm mistaken that clears some stuff up for me. Thank you!
Re:Adding Modules to C in 10 Lines of Code [pdf]
by phaer on Monday June 27, 2022 @03:25PM
(#31899531)
That's my understanding as well, so I think you are correct but I am no C wizard myself :)
Re:Adding Modules to C in 10 Lines of Code [pdf]
by arinlen on Tuesday June 28, 2022 @01:25AM
(#31904406)
> I never really had a good grasp of C. Why is this code novel? It looks like importing a function in any language.
Unity builds are a very old and widely established concept. I dare to speculate they are older than the regular header and source file split, as C required a full blown preprocessor to split code in separate files and also someone had to come up with the include guard trick to get around the limitations of including the same definition multiple times.
They aren't employed that often because there are some tradeoffs and in large projects it's benefitial to split them in smaller translation units to limit the scope of incremental builds.