Is it wrong to place inline functions in C headers?

I am creating a C project for several compilers, some of which are obsolete compilers that do not seem to support link time binding, so it was logical to place functions static inlinedirectly in the headers and actually have each translation unit have their own copy.

In addition, I need to make sure that certain functions are built-in, so there are no calls to other functions (i.e., changes to the CPU registers) when called inside certain low-level interrupt handlers, so not just letting the compiler choose will affect performance.

However, my colleague told me that this is an unusual thing, and I should avoid it. At the moment, in the project, I probably can still change everything, so I just would like to confirm if there are any problems that we may encounter in the long run if we decide to use heading headers?

+4
source share
3 answers

From n1570 (latest public draft C11), §6.7.4:

  1. A function declared using a function specifier inlineis an inline function. Creating a function an inline function assumes that function calls should be as fast as possible. The extent to which such proposals are effective is determined by implementation.

, , , C inline. , , inline.

, C ( ) , . , , ( ). , C inline , . , inline.

, , (, , _inline ), . , .

cmaster, " " - .

+3

, static _inline - , .

, , , , inline. , , , , , :

, , . .

, ( 1256 C99 1570 C11):

6.7.4
...
. , : ​​ , . extern, . , . , . ,

, inline, ,

+2

static inline, ISO 9899 , , extern .

, inline aaa, aaa .

6.7.4 Function specifiers

extern, . .

emacs uses this method of nesting in the header lisp.h, which defines lisp objects, I quote the source code for emacs:

Some operations are so often performed that they are implemented as macros, not functions, because otherwise, runtime performance suffers too much when compiling with GCC without optimization.
There is no need to embed everything, just operations that would otherwise cause a serious performance problem.

0
source

Source: https://habr.com/ru/post/1681845/


All Articles