#Header and implementation files

31 messages · Page 1 of 1 (latest)

regal gate
#

I understand the basics of this concept but what if in my header file I have an inline or static function declaration. Is it possible to split them up? It seems like it wouldn’t make sense to do this or if it’s even allowed

jolly tendonBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

ripe spoke
#

Static functions can't be split like this, it makes no sense for them. The point of those is that they're only usable in one .c file

#

Defining them in a header will duplicate them into every .c file that includes it

#

As for inline functions, they are weird in C, and don't behave the same way as they do in C++

#

IIRC C requires you to have two definitions for those: one inline in a header, and another without inline in a .c file. This requires some macro shenanigans

primal jetty
regal gate
#

Yea so I see why it wouldn’t make sense for static

#

But for inline I know that you need to provide a separate definition if not used with static

#

I’m still confused though, is it valid to do inline void foo(); in my header and in an implementation file I do inline void foo{….}

#

So I think I understand it now, If I use inline, I should put the definition in the header, and to avoid needing a separate external definition, use static inline.

primal jetty
#

every file would have its own copied definition. and inline is just a hint, so in essence you're not forcing anyone to do anything

#

(inline does not mandate the compiler to inline in C... modern compilers are likely very good at inlining without you to say what they should do. a bit like register keyword. nowadays it's kind of obsolete and at best, a hint)

gleaming veldt
#

in practice you do inline functions like this: ```c
// GCC and Clang
attribute((always_inline))
static inline void foo(void) {
// ...
}

// MSVC
static __forceinline void foo(void) {
// ...
}

#

In other words you can have inline functions in header files by using compiler extensions to make inlining unconditional instead of a hint.

#

And yes, static inline (or static __forceinline in MSVC) is required

#

The GCC and Clang attribute needs the normal inline as well, whereas MSVC's __forceinline replaces it

primal jetty
#

wondering: what happens if you pass &foo ?

#

does it mean something special once the compiler-attribute has been written, or is it a regular function pointer?

gleaming veldt
olive spruce
gleaming veldt
gleaming veldt
#

In my example you'd write static force_inline instead of static inline, nothing else required

primal jetty
#

aka: are there cases where you can really choose forceinline independently on static?

gleaming veldt
gleaming veldt
primal jetty
#

from another perspective, you might argue that static is there because of the forced inline, so in some sense it would make sense to have it in the macro