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

6

u/NotThatJonSmith Jul 25 '26

It’s also so you can call functions in a library you are given as a binary distribution. If you don’t want to ship the source code you must at least ship the interface. 

-1

u/yuehuang Jul 26 '26

Most other languages that has binary distribution include the api functions in the binary itself. It is a small section of ascii text that the dll/so loader would ignore.

You can say that one for runtime and the other is for compile time. However, there are .lib and .a that is used only for compile/linker. These could be unified as into one format avoiding the header issue all together. This is the goal of the C++ Modules.

1

u/NotThatJonSmith Jul 26 '26

I’m curious, because I’ve only worked with C/C++ for compiled languages. What other languages have the paradigm that ship binary libraries that contain enough information in source form that you can compile against them?  That’s kind of nifty.

1

u/Conscious_Support176 Jul 26 '26 edited Jul 26 '26

C/C++ is pretty much the only language that does this?
#include is a pre-processor directive. It’s the wrong tool for the job, but it’s so deeply intertwined with how C is written that it’s hard to back out of it now.

Edit: yes, COBOL does something similar, and I’m sure some other “dinosaurs” might do too 🤔

1

u/NotThatJonSmith Jul 26 '26

Could you give me a specific example where binary is shipped, in which you can call functions, but you don’t need separate headers?

1

u/Conscious_Support176 Jul 27 '26

Ok. You might recognise one of these.

Algol-68, Fortran-77, Modula‑2, Turbo Pascal, Oberon, Ada

Java, C#, Swift, Go, Rust, C++20 Modules, D

Haskell, OCaml, Standard ML, Erlang, F#

1

u/NotThatJonSmith Aug 01 '26

You’re just listing languages without telling me how this mechanism works for any one of them.

Just taking a look at rust, when you ship a crate, it’s static linkage only. If you want a shared library - code that maps into the VA space of more than one process at a time without needing to be duplicated physically (like how static linkage just embeds the library code into your application) then they officially instruct us to use extern C linkage and provide - you guessed it - header files. Rust doesn’t provide a way to explain to the compiler the contents of a binary such that it can be linked to on its own.

1

u/Conscious_Support176 Aug 02 '26 edited Aug 02 '26

You literally asked me for an example, and I gave you numerous examples. What you are talking about is ABI stability across compiler versions. The point there isn’t about shared libraries as much it is about shared libraries in the context of a compiler that is still evolving and may decide to break the binary ABI. If you ensure you are using compilers that compile to the same ABI then you don’t need to specify the extern linkage. Even then, you don’t need a header file for rust.

What you need a header file for is, C calling rust. Because C doesn’t know anything about rust modules and cannot extract the information it needs from them, and in fact there is no way to provide it with a rust adaptor because include files are the one and only way that the C compiler can learn this information about other libraries.

1

u/NotThatJonSmith Aug 03 '26

You’d need a C header for Rust to call Rust if you want dynamic linkage. Rust’s modules are static linkage. Granted you gave an example of shipping a binary without a header. The context as I read it, at this point in the conversation, was specifically shared libraries. So at that point, to me, it seemed like you’d given me examples that don’t demonstrate the property I was asking about.

It feels like the classic “talking past one another” or just not having the same conversation as each other. I wasn’t precise in what I was asking. That happens; to be clear I don’t think you’re arguing in bad faith. I could read my own comment as having implied I think you are.

I just don’t know anything about how other languages do the song and dance of dynamic linkage… Is extern C indelibly baked into the dynamic linker? It would appear so. If there’s a counterexample it must be doing something exotic, and that’s what I wanted to learn about.

Finding out that “ship shared library code in rust, and can it from rust” uses extern C was disappointing.

1

u/Conscious_Support176 Aug 03 '26 edited Aug 03 '26

What makes you think that you need a c header for rust to call rust with dynamic linkage?

Ah. I think I see the misunderstanding. By binary shipped, you mean the operating system binary, not the language binary. Obviously, if you only have the OS binary, you are going to lose type information, because there isn’t a cross language agreement on how to define types within the OS binary.

If you ship the language binary, in this case, rust crates, you will not need header files. If you choose to ship only the OS binary, that is the C style of doing things, that is the choice that requires header files.

Most compiled languages give you this option for various reasons. But very few give you no other option. Because, while header files can be handy way of providing ABI stability across libraries, it is a terrible way of managing intra-library dependencies.