As I know, C inline function body should be defined in .h file because it causes an error 'function-name used but never defined" if body defined in .c file.
Is this the regular way? Or how to define inline function body in .c file?
Inline functions are static, i.e. visible only within translation unit where they are defined. One solution is to define, not declare, but define inline function in header file.
The definition of an inline function doesn't have to be in a header file but, because of the one definition rule (ODR) for inline functions, an identical definition for the function must exist in every translation unit that uses it. The easiest way to achieve this is by putting the definition in a header file.
The __inline keyword suggests to the compiler that it compiles a C or C++ function inline, if it is sensible to do so. The semantics of __inline are exactly the same as those of the inline keyword. However, inline is not available in C90. __inline is a storage class qualifier. It does not affect the type of a function.
The inline function can be substituted at the place where the function call is happening. Function substitution is always compiler choice. In an inline function, a function call is replaced by the actual program code. Most of the Inline functions are used for small computations.
Each .c
file is compiled independently into .o
output. If you define the inline function in a .c
file, other source files cannot see such function, so that cannot be inlined.
Therefore the inline function should be in the .h
file to allow the code to be shared.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With