Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to define C inline function in .c file rather than .h file?

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?

like image 306
eonil Avatar asked May 02 '10 06:05

eonil


People also ask

Can inline function be defined 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.

Do inline functions have to be in header?

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.

What is __ inline in C?

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.

How does inline function work in C?

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.


1 Answers

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.

like image 154
kennytm Avatar answered Sep 28 '22 17:09

kennytm