I've mostly worked with languages that use imports, like TypeScript, Java, Python, and PHP, so learning how C handles separate compilation and linking was really refreshing! 😸
And congratulations on your first Necoz article! I love seeing members inspire each other to write new articles! 💚
Also, your post wasn't the inspiration. You were the inspiration (yeah, the post was also inspiration, but you were the first inspiration because the post came after I got inspired :))
Full-stack software engineer with 8+ years of experience in Python, JavaScript, TypeScript, APIs, and PostgreSQL. Passionate about building reliable software and connecting with fellow developers.
Really enjoyed this explanation! It's refreshing to see someone explain why C works this way rather than simply telling beginners to use header files and move on.
The distinction between compilation and linking is one of those fundamentals that becomes much more valuable as projects grow.
It also makes me wonder what you think about unity builds, where developers intentionally combine multiple source files to improve compilation performance. At what point do the benefits outweigh the drawbacks of separate compilation?
And writing this entire article on your phone during a bus commute? That's impressive. I can barely write a decent commit message on mine. 😂
Looking forward to more articles like this!
Thank you so much for the feedback! 😊 When I build an ESP-IDF project, the first compilation is very slow. But subsequent compilations are much faster. I think separate compilation is better during development, since we often update a few files and rebuild? 🤔
Everybody's noob on mobile. Particularly on my mobile. Because it only has 4GB RAM. Apps automatically get cleared so there is no multitasking :( If I am writing an article and want to Google something, and I come back to write the article, the entire browser reloads 💀
I tried everything I know to fix this but no wayyy :(
I personally hate compile errors and that fact that direct source inclusion does not work from .c to .c file. It would save me the headache of trying to manage variables in headers and functions in c and then updating all the headers when the C files change. This is why I like the NASM assembly compiler or python or go or even JavaScript because you can include the source files directly without headers.
Yeah same here. Updating header files are really annoying at times 😅 It feels like unnecessary effort to manage a copy of all function declarations in the header.
The explanation of why pragma once cannot prevent duplicate definitions across separate compilation units is especially useful. One detail I would clarify: reusing unchanged object files is normally the build system’s job. Running the shown gcc command again compiles both source files again; a Make or Ninja example would make that distinction visible.
Thanks for the comment. That is correct! 😊 I updated that section to this:
Also, assume we compiled our project first, then made a few changes to functions.c. We don't need to compile main.c again! We can reuse the main.o file we created earlier. Build systems like CMake do this automatically under the hood. If we had #includeed functions.c in main.c, this wouldn't have been possible, since the compiler sees a single, concatenated main.c file.
Thanks for updating it! That makes the object file benefit much clearer. I would keep one small qualification beside it: main.o can be reused when its own source and included headers are unchanged. A change to a shared header can still require recompiling both translation units.
Clear, beginner-friendly walkthrough — I liked how you showed the preprocessor literally copying the file in. The parallel-compilation and duplicate-definition reasons for using headers make a lot more sense now.
I've spent 16 years finding things that don't work. These days, I also teach AI how to find them.
Somewhere along the way, I started writing stories about AI, work, and life.
Now I run The Third Cup.
writing all this on a bus from your phone is impressive lol. fun twist for a follow up: some projects do include .c files on purpose, like unity builds and SQLite shipping as one big sqlite3.c
@akhourianmolkumar @star123 It has been a while since I took my last typing speed test. And guess what, it increased!!! 🤯 I never knew...
I got this on the first try 🔥🔥🔥
My Peak can reach 92 max to max and my father Peak maybe 110 but you outperformed a 16year experience it head lol 🔥 💀 ☠️
Thanks bro 🫡
Bro today I'll post nothing, bro todays plans?
Why nothing? 🤔
Bro per day 1 post?
I've mostly worked with languages that use imports, like TypeScript, Java, Python, and PHP, so learning how C handles separate compilation and linking was really refreshing! 😸
And congratulations on your first Necoz article! I love seeing members inspire each other to write new articles! 💚
🙀 Glad you learned something from me :)
Absolutely awesome! You nailed a topic that many beginners struggle with. 🔥
Also, I'm really thankful to you for considering my post an inspiration! 😊
Hehe thanks! 🙂
Absolutely!
Also, your post wasn't the inspiration. You were the inspiration (yeah, the post was also inspiration, but you were the first inspiration because the post came after I got inspired :))
trailing return types in cpp
while loop and for loop are not the same in C/C++
That means a lot to me man! Thank you 🧡
Thanks! 🙂
Really enjoyed this explanation! It's refreshing to see someone explain why C works this way rather than simply telling beginners to use header files and move on.
The distinction between compilation and linking is one of those fundamentals that becomes much more valuable as projects grow.
It also makes me wonder what you think about unity builds, where developers intentionally combine multiple source files to improve compilation performance. At what point do the benefits outweigh the drawbacks of separate compilation?
And writing this entire article on your phone during a bus commute? That's impressive. I can barely write a decent commit message on mine. 😂
Looking forward to more articles like this!
Thank you so much for the feedback! 😊 When I build an ESP-IDF project, the first compilation is very slow. But subsequent compilations are much faster. I think separate compilation is better during development, since we often update a few files and rebuild? 🤔
I'm quite noob in mobile too.
Everybody's noob on mobile. Particularly on my mobile. Because it only has 4GB RAM. Apps automatically get cleared so there is no multitasking :( If I am writing an article and want to Google something, and I come back to write the article, the entire browser reloads 💀
I tried everything I know to fix this but no wayyy :(
I personally hate compile errors and that fact that direct source inclusion does not work from .c to .c file. It would save me the headache of trying to manage variables in headers and functions in c and then updating all the headers when the C files change. This is why I like the NASM assembly compiler or python or go or even JavaScript because you can include the source files directly without headers.
Yeah same here. Updating header files are really annoying at times 😅 It feels like unnecessary effort to manage a copy of all function declarations in the header.
Bro, this is really well written.🙌🏻
બહુ સારું છે :D
Thanks Star ☺️
I was waiting for this!
Thanks! I remember you telling me :)
I think the parallel compilation point finally made headers click for me.
Thank you so much! Glad my explanation did what it's supposed to do! 🙂
The explanation of why pragma once cannot prevent duplicate definitions across separate compilation units is especially useful. One detail I would clarify: reusing unchanged object files is normally the build system’s job. Running the shown gcc command again compiles both source files again; a Make or Ninja example would make that distinction visible.
Thanks for the comment. That is correct! 😊 I updated that section to this:
Thank you so much for pointing that out!
Thanks for updating it! That makes the object file benefit much clearer. I would keep one small qualification beside it: main.o can be reused when its own source and included headers are unchanged. A change to a shared header can still require recompiling both translation units.
Yes, that's true as well! 😊
Clear, beginner-friendly walkthrough — I liked how you showed the preprocessor literally copying the file in. The parallel-compilation and duplicate-definition reasons for using headers make a lot more sense now.
Thanks a lot! 😊
honestly idc i just include everything (
#include functions.c) because why tire myself with .h files?It's also completely valid 🤣 Thanks a lot for the comment!
Why @xulingfeng why? 😢
I'll write the next article for The Third Cup...
😅
Hehe thanks 🙏🙂
writing all this on a bus from your phone is impressive lol. fun twist for a follow up: some projects do include .c files on purpose, like unity builds and SQLite shipping as one big sqlite3.c
Yes you are right! @johnclentoira also mentioned it in the comments. Thanks for the comment! 🙂