#Header and implementation files
31 messages · Page 1 of 1 (latest)
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.
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
the basic usage would be to declare (without implementation) function in headers, which wouldn't be static. then in C file, provide all the implementations and consider static as "private"
for inline functions, I think nowadays you could as well just rely on link-time-optimisation (likely flto) and compiler analysis
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.
I think that static inline makes no real sense
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)
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
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?
i think if you force inlining it won't work
a macro would be required to select which function definition to be used ?
#ifndef _MSC_VER
# define force_inline __attribute__((always_inline)) inline
#else
# define force_inline __forceinline
#endif
static force_inline void foo(void) {
// ...
}
it isn't that much hassle. you don't need to select between entire function definitions, you just need a macro to use in place of inline ^
In my example you'd write static force_inline instead of static inline, nothing else required
why not putting static in the macro directly?
aka: are there cases where you can really choose forceinline independently on static?
it hides a keyword for no benefit
no, but having the static in the macro would obfuscate the code
I'm not sure I'm convinced for now
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