DEV Community

EffessDev for Necoz

Posted on Edited on

Why Can't We Just #include "functions.c" in "main.c"? ๐Ÿค”

Explains C preprocessor mechanics for beginners

Suppose we have two files.

functions.c:

#include <stdio.h>

void greet(void) {
  printf("Hi!");
}
Enter fullscreen mode Exit fullscreen mode

main.c:

int main() {
  // Empty
}
Enter fullscreen mode Exit fullscreen mode

And we need to call greet() from main.c. How do we do that? ๐Ÿค”

If you are coming from other languages, you might expect to import functions.c in main.c and call greet().

But C programmers usually create a file named functions.h first, containing the top portion of the function (called declaration):

functions.h:

void greet(void);
Enter fullscreen mode Exit fullscreen mode

Then #include it in main.c and call the function:

main.c:

#include "functions.h"

int main() {
  greet();
}
Enter fullscreen mode Exit fullscreen mode

Why not #include functions.c directly? Why use a header file (functions.h)?

How #include works

The working mechanism of #include "filename" is extremely simple--It just copies that file's contents into that spot before actual compilation begins (literally). main.c would look like this just before compilation:

main.c:

void greet(void); // <-- Contents of functions.h

int main() {
  greet();
}
Enter fullscreen mode Exit fullscreen mode

So functions.h is not strictly needed here, as long as you put void greet(void); (the declaration for the function greet()) in main.c. Like this:

main.c:

void greet(void); // <-- Declaration for greet()

int main() {
  greet();
}
Enter fullscreen mode Exit fullscreen mode

So the question becomes, why we need to declare functions.

Why do we need to declare functions? ๐Ÿค”

When we compile our program, we already specify all source files.

Example build command (using the C compiler gcc):

gcc main.c functions.c -o final_program
Enter fullscreen mode Exit fullscreen mode

Since we already specify all source files here, manual import inside the files (like Python) isn't needed. We do not import source files inside source files, we simply build (compile) them together.

The key part

But each one of those source files compile separately, often in parallel to utilise the multiple cores in your CPU. In our case, main.c and functions.c get compiled in parallel. This is the main reason why we need to declare functions.

Lets assume the compiler is compiling this part of main.c:

    greet();
Enter fullscreen mode Exit fullscreen mode

Here, we are calling a function which is not defined in the current file. The compiler doesn't know how to call greet(), like which arguments to pass, the types of the arguments, the return type, etc. Without that info, the compiler wouldn't be able to translate it to machine code

When we add a declaration void greet(void); in main.c, we are promising the compiler that a function named greet that takes no arguments and return nothing exists in one of the source files (note: void means "nothing"). Now, the compiler has our promise so it doens't throw an error saying that this function doesn't exist, as well as the necessary information to compile the calling part. The address to the actual function is left empty.

After compilation, each source file becomes an object file, with the file extension .o (main.o, functions.o).

Then, something called a linker, combines these object files together, giving us the final executable. It will fill in the empty function addresses, and throws an error if the function we promised to exit doesn't exist in any of the source files.

So, that's solved now. But we didn't answer the original question, did we?

Why Can't We Just #include "functions.c in main.c?

We said:

"Each one of those source files compile separately, often in parallel to utilise the multiple cores in your CPU."

But if we #include the contents of all files in main.c, main.c would be a single large file before compilation, and we would loose that benefit.

There is also a performance advantage as well. 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.

Other than that, there is one problem that happens if you #include "functions.c" in more than one file. Because #include just copies the code in, greet() gets defined more than once across your compiled files, and the linker throws a multiple definition error when combining them.

Putting #pragma once in functions.c wouldn't fix this, because it only stops a file from being included more than once inside the same single file during one compilation. It does not stop if separate .c files #include functions.c and create duplicate definitions for the linker.

Thanks for reading ๐Ÿ™‚

This is my first post for Necoz and was inspired by @csm18!


I wrote this entire article from my phone during a bus commute. Feel free to report if there are any typos/issues! ๐Ÿ˜Š

Top comments (102)

Collapse
 
effessdev profile image
EffessDev Necoz • • Edited

@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 ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

Collapse
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

My Peak can reach 92 max to max and my father Peak maybe 110 but you outperformed a 16year experience it head lol ๐Ÿ”ฅ ๐Ÿ’€ โ˜ ๏ธ

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks bro ๐Ÿซก

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

Bro today I'll post nothing, bro todays plans?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Why nothing? ๐Ÿค”

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

Bro per day 1 post?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Oh yeah... That isn't required...

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

bro what's plan I asked, also if you could come online in discord then it will be better.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

I do not like Discord bro... ๐Ÿ˜ช

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

why?

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

??

Thread Thread
 
effessdev profile image
EffessDev Necoz • • Edited

Because I cannot leave whenever I want...

Bro it was soo hard to find this GIF lol ๐Ÿคฆโ€โ™‚๏ธ From where did you get it?

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

hmm, bro what's todays plan ?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

You don't have any plans? ๐Ÿค”

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

bro I was asking your plans CTOs lol my plans is to update APIC V2 UI for today that's my work also I'm thinking to make akhouri systems website url: akhouri-systems.github.io not sure but yeah that's it now yours?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Yeah yeah yeah!!! akhouri-systems.github.io! Or maybe akhouri-systems.vercel.app in case you want a Next.js website or something with a backend!

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

bruh what's your plan?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Maybe add a select feature to APIC STUDIO or something? ๐Ÿค”

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

oh PR?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

PR? ๐Ÿค”

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

you said maybe a select feature in apic studio so you're saying me to add or you're adding? and if it was for me then I'm Asking what's your plans/.
also; bro does apic studio really need one more feature/?./
and if yes then why.?/

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Yeah. If I want to select a region and erase it, APIC STUDIO can't do it yet so I will have to go to a competitor's product.

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

Okay then I will add, still waiting for your plans lol, me begging 10 times leave it then if you don't have any, I'm online in discord if you want you can come.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Also, if I accidentally select a huge model but I meant to select a smaller one, I cannot stop removing the background so I can switch to a smaller one (it happened right now again; I lost count).

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

okay I'll fix.
I'm Fixing, /\

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Another bug I found. Here is how to reproduce:

  1. Open an image
  2. Add a new image layer
  3. Go to remove background and click Remove background

Expected: background removed
What happened: background wasn't removed, but APIC STUDIO said it did

Workaround: Clicking Undo then Redo removes the background.

Maybe the background was removed internally, but the preview wasn't updated? ๐Ÿค”

Thread Thread
 
effessdev profile image
EffessDev Necoz •

I opened an image, then I added a speech bubble image layer. I tried to remove the background of that speech bubble layer, but I can't, I think. It removes the background of the original layer instead. All layers should support all features.

Also, the AI background removal is very bad in certain situations. A manual select + erase is better when the background is white (like my speech bubble image). That's why I said we need a select feature (including sub-features like expand, contract, feather, invert, etc.). Also, there is no eraser tool; there is no way to draw anything. I am unable to add a box or circle or oval.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

The zoom doesn't work when I have more than 1 layer. Also, I can only access these controls in Remove Object and Advanced:

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Bro is there a feature to temporarily disable snapping? Like in other editors I can hold Shift to disable snapping while dragging.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Bro there is only 5 fonts we need more ๐Ÿ”ฅ

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Bro everything should apply to the currently selected layer, not the original layer. If I do anything (like remove bg, advanced, whatever) it happens to the original layer not the selected one.

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

bro currently does it really need more fonts just think yourself lol also all bugs fixed

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

okay will be added soon.

Thread Thread
 
effessdev profile image
EffessDev Necoz • • Edited

Bro dragging selects an area. But to remove that selected area, we have to scroll down, find the Clear button, and click it. It can be really annoying for new users. Either you should add an "X" button on the top right of the selection or something, or make selection only start by clicking a "Select" button. A better way is to make clicking "Crop" or "Blur" or "Pixelate" show the box.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Bro adding more fonts is easy... It really needs more fonts. ๐Ÿ‘

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

okay then I'm adding it,

Thread Thread
 
effessdev profile image
EffessDev Necoz • • Edited

๐Ÿ™Œ

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

Bro someone vibe coded a whole OS lol have you heard that viral AI slop OS made from claude? in youtube it is now a viral roast topic.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

No... ๐Ÿค”
But bro aren't you supposed to do your job?
Go and do your job... ๐Ÿซต

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

bro check apic studio now, also now my job over yeah I know people work 9-5 but I work 9-5(minutes hehehe hehehe hehehe hehehe) just joking but all bugs fixed right as of now? because I'm now scrolling shorts (uh I lied) actually I'm listening this masterpiece:
youtu.be/r-fBULbTtdM in fact while working I was listening this hehe

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Ok... You did all of that this quickly?!

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

yup, and now I'm thinking I do my homework(option A), or I play roblox (option B) or I talk with you in discord if you're ready which you're not ๐Ÿ˜‘uh (option C) or just stay randomly online in dev.to, while in multitasking watch 50% GODZILLA : THE KING OF MONSTERS movie 19th time guess I'll watch if I choosed this option(OPTION D)
now you tell me?
for me I WILL choose option C but I know you ๐Ÿซต

Thread Thread
 
effessdev profile image
EffessDev Necoz •

I am going to study for the exam... So you do option A! ๐Ÿ™‚

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

you're sly guy.
just like me ๐Ÿ˜๐Ÿ”ฅ
okay Founder mode off, School mode on going to do homework but while keep this tab and discord tab open.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Ok, I'll keep this tab open! ๐Ÿ™‚

Bro what did you do to APIC STUDIO? ๐Ÿ˜ง๐Ÿ˜ง๐Ÿ˜ง That is not what I meant lol ๐Ÿคฆโ€โ™‚๏ธ

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

what I did? bro tell is that too wrong?

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

peak brave

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Really?? ๐Ÿคฃ๐Ÿคฃ๐Ÿคฃ๐Ÿ’€

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

yeah average brave user experiences this daily lol they are savage. I love brave browser a lot ๐Ÿ˜‚โ˜ ๏ธ

Thread Thread
 
effessdev profile image
EffessDev Necoz •

I also use Brave on mobile. On PC still Chrome...

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

bro what I did wrong? in apic studio

lol you're legend hehe brave in mobile and pc chrome
ME PURE BRAVE USER BOTH LAPTOPS WITH BRAVE, AND BOTH MOBILES TOO

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Bro you repeated all of that in the Layers section again. What I meant is that if we select a layer by clicking it (which it already does), we can go to ANY section in the whole editor (bg remove, obj remove, advanced, whatever) and make changes, and they apply to the currently selected layer. That's what all photo editors do; do not repeat all features in the Layers section ๐Ÿคฆ

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

bro it is urgent fix?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Not urgent but still...

Collapse
 
star123 profile image
Sแด›แด€ส€โญ Necoz •

Wow๐Ÿ˜ฎ๐Ÿ˜ญ 134WPM
Which website?

Collapse
 
effessdev profile image
EffessDev Necoz •
Thread Thread
 
star123 profile image
Sแด›แด€ส€โญ Necoz •

Okay bro, I'll write the post , and then share the result there ๐Ÿ˜…

Thread Thread
 
effessdev profile image
EffessDev Necoz •

What? ๐Ÿค”

Thread Thread
 
star123 profile image
Sแด›แด€ส€โญ Necoz •

I will ask people about their typing speed. Is this a good idea?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Yeah I think... ๐Ÿค”

Thread Thread
 
star123 profile image
Sแด›แด€ส€โญ Necoz •

Image edescription

Bro, this is good!๐Ÿ˜€

Thread Thread
 
effessdev profile image
EffessDev Necoz •

I added it just now!!! How did you see that? ๐Ÿค”

Thread Thread
 
star123 profile image
Sแด›แด€ส€โญ Necoz •

Bro, I learn by reading your code.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Really? ๐Ÿ™€๐Ÿ™€๐Ÿ™€ I am honored!!!

Thread Thread
 
star123 profile image
Sแด›แด€ส€โญ Necoz •

yeah huge respect!

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Huge respect to you as well ๐Ÿซก

Collapse
 
nyaomaru profile image
nyaomaru Necoz •

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! ๐Ÿ’š

Collapse
 
effessdev profile image
EffessDev Necoz •

๐Ÿ™€ Glad you learned something from me :)

Collapse
 
csm18 profile image
csm Necoz •

Absolutely awesome! You nailed a topic that many beginners struggle with. ๐Ÿ”ฅ

Also, I'm really thankful to you for considering my post an inspiration! ๐Ÿ˜Š

Collapse
 
effessdev profile image
EffessDev Necoz • • Edited

Hehe thanks! ๐Ÿ™‚

Collapse
 
csm18 profile image
csm Necoz •

Absolutely!

Thread Thread
 
effessdev profile image
EffessDev Necoz • • Edited

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 :))

Thread Thread
 
csm18 profile image
csm Necoz •

That means a lot to me man! Thank you ๐Ÿงก

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Thanks! ๐Ÿ™‚

Thread Thread
 
effessdev profile image
EffessDev Necoz •

@csm18 both of our posts got the exact number of reactions! ๐Ÿซจ

Thread Thread
 
csm18 profile image
csm Necoz •

Cool!

Collapse
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

Collapse
 
johnclentoira profile image
john Clent Oira •

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!

Collapse
 
effessdev profile image
EffessDev Necoz •

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? ๐Ÿค”

Collapse
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

I'm quite noob in mobile too.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

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 :(

Collapse
 
gtref profile image
G-type Necoz •

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.

Collapse
 
effessdev profile image
EffessDev Necoz •

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.

Collapse
 
star123 profile image
Sแด›แด€ส€โญ Necoz • • Edited

Bro, this is really well written.๐Ÿ™Œ๐Ÿป
เชฌเชนเซ เชธเชพเชฐเซเช‚ เช›เซ‡ :D

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks Star โ˜บ๏ธ

Collapse
 
technogamerz profile image
๐“๐ก๐ž ๐‹๐š๐ณ๐ฒ ๐†๐ข๐ซ๐ฅ Necoz •

I was waiting for this!

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks! I remember you telling me :)

Collapse
 
the_daily_flare profile image
The daily flare •

I think the parallel compilation point finally made headers click for me.

Collapse
 
effessdev profile image
EffessDev Necoz •

Thank you so much! Glad my explanation did what it's supposed to do! ๐Ÿ™‚

Collapse
 
emilywoodsnyc profile image
Emily Woods •

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.

Collapse
 
effessdev profile image
EffessDev Necoz •

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.

Thank you so much for pointing that out!

Collapse
 
emilywoodsnyc profile image
Emily Woods •

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.

Thread Thread
 
effessdev profile image
EffessDev Necoz • • Edited

Yes, that's true as well! ๐Ÿ˜Š

Collapse
 
enoch_hou profile image
Enoch Hou •

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.

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks a lot! ๐Ÿ˜Š