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

157

u/f0xw01f Jul 25 '26

The biggest reason for separating declarations from implementations is so you can avoid build problems with circular dependencies when two source files each call code in the other.

64

u/Jan-Snow Jul 25 '26

This. It also has to be mentioned that this is necessary in C unlike many other languages because C is a single pass compiler, so it's for the same reason that functions have to be declared further up the file than they are called, which many other languages also dont require.

3

u/jason-reddit-public Jul 26 '26

My "C" transpiler sorts everything from N input files and spits out a single C source file (with optional header file if you're making a library). It's so nice to just refactor by making a new file and moving stuff around. I believe this makes the code much better.

(I think the combination of casts and typedefs make this generally impossible though I have a cast keyword so it seems to work.

While I was at it, I made typedefs not such so hard with a new syntax - typedef foo = struct {fields...}; and also typedef my_callback = fn_t(void, char*, int64_t);
)

10

u/Majestic-Instance704 Jul 25 '26

 Hmm... Why would there be circular dependencies inside a library that already works and compiles correctly?  I understand now how separating declarations from implementations helps solve that problem, but I'm still confused about why the problem would exist in the first place. Thanks!

10

u/f0xw01f Jul 26 '26

I did not mean to imply that the library had circular dependency problems. That's one of the reasons why we store declarations separately from implementations. Circular references aren't a problem when things are separated like this.

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.

→ 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.

1

u/Conscious_Support176 Aug 02 '26

Header files might be used for that. But it’s a bad solution to the problem. It is a problem for C++ modules for example. The idiomatic solution to break circular dependency is dependency inversion.