Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline (non-static) function in C-source file

Tags:

c

inline

I've seen something like that in a C-code and I'm wondering what it exactly does:

file.h

int f(int x);

file.c

inline int f(int x)
{
    ...
}

Basically the function is declared as inline in the C-file, but "normally" declared in the header-file.

Assuming f is never called in file.c, then the compiler should not be able to inline it into other translation units. Except for it LTO is used. However, does LTO (let's say gcc LTO) care about this inline? Probably it anyway inlines the function if it's small?

Or what's the effect of this in practice?

like image 442
Kevin Meier Avatar asked May 25 '26 15:05

Kevin Meier


1 Answers

Assuming that foo.c includes foo.h, and assuming C99 or later, the declaration of f without the inline keyword makes the definition of foo an external definition.

This will allow foo to be called from other translation units, whether it's called in foo.c or not.

like image 134
dbush Avatar answered May 27 '26 04:05

dbush