r/C_Programming • • Jul 25 '26

why do header files contain only function declarations instead of full code body ? why have the code in a sepreate lib files ?

for context I am trying to learn gamedev on my own and setting up raylib/sfml confused me , then I learnt that the ( include )files only contained function declarations and the actual code was in( lib ) files (mind you but it is a weird name) , wouldn't it be more convenient to have them in the same file instead of linking ?
sorry if I sound stupid.

104 Upvotes

106 comments sorted by

View all comments

Show parent comment

4

u/delinka Jul 25 '26 edited Jul 25 '26

Circular dependencies actually aren’t a problem if you’re using header guards or #pragma once

14

u/neppo95 Jul 26 '26

Wanna bet? File A includes file B. File B includes file A. Circular dependency. No pragma once or header guard will save you there. It will not compile unless you specifically take measures to prevent the dependency.

2

u/xwizardx007 Jul 26 '26

thats just language limitation(another reason why c is what it is)

the main reason is for perofmance on ancient pcs
they couldnt parse big apps and read whole project in one go when they had 64kb of ram
so they buffered it and only kept the headers you use in each compilation unit.

0

u/delinka Jul 26 '26

Yes, I'll take that bet. Working fine here: https://github.com/reddit-delinka/circular

Feel free to change out the header guard for a #pragma once and watch that work, too.

1

u/neppo95 Jul 26 '26

Your link isn't working mate. Or it is a private repo.

1

u/delinka Jul 26 '26

Try now

3

u/neppo95 Jul 26 '26

'Course it's not a circular dependency when they don't actually depend on eachother. Your example doesn't apply to what we were talking about.

1

u/delinka Jul 26 '26

We’re talking about header file dependencies. However, if you mean the two functions calling each other, fine, I’ve added that and it compiles just fine. Now, you run it and it’s gonna run out of stack space. But you said it wouldn’t compile.

2

u/neppo95 Jul 26 '26

You added that, in the implementation in the cpp file. Try it in the header and we'll talk again. Your case falls under " It will not compile unless you specifically take measures to prevent the dependency." - which you did by the way of forward declaring.

1

u/delinka Jul 26 '26

I think you need to write some code to explain what you’re talking about

0

u/neppo95 Jul 26 '26 edited Jul 26 '26

The error is different since I put them above eachother (as the include would), result is the same: Won't compile: https://godbolt.org/z/P9ndq83zv

Edit: Forgot we're talking C here, same applies but the example is different ofc:

https://godbolt.org/z/5nY5caWv1

→ More replies

0

u/Its_-_me_-_Mario Jul 26 '26

include doesn't have any sort of priority over other preprocessor declarations.

When file A includes file B, the second file gets preprocessed line by line, and when file B then includes file A it also gets preprocessed line by line and the include in file A is never reached because pragma once/header guards sit before it

1

u/neppo95 Jul 26 '26

That is not the issue. The issue is when those two files with different declarations depend on each other, hence, dependency. You'll end up with incomplete types that you can't use.