5

Consider this example of a function declaration and definition (in the same translation unit):

inline static int foo(int x);

...

int foo(int x)
{
  return x+1;
}

I know that the types need to match, but what about other keywords and qualifiers? Should inline static be in both cases? Or just the declaration?

And which part of the C standard or which coding guideline could I use to justify the answer?

Jason S
  • 184,598
  • 164
  • 608
  • 970

4 Answers4

2

No, for inline in particular, these should not be the same.

But the example is wrong from the start. For inline you need a definition (the whole function) with the inline in the .h file. By that, the inline, you avoid that the symbol is defined in several translation unit (.c) where you include the .h header.

Then, in exactly one translation unit you pout just the declaration without inline to indicate that the symbol should be generated in the corresponding .o object file.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • in my example they are in the same translation unit – Jason S Mar 22 '17 at 21:36
  • @JasonS, if it is just inside one TU, `inline` makes no sense at all. The compiler can inline every definition that it sees, anyhow. – Jens Gustedt Mar 22 '17 at 21:48
  • can != does. For this particular compiler I am using (Microchip XC16) the free version with `-O1` optimization doesn't automatically inline small functions (but you can cajole it into doing so with `inline`) whereas the premium version with `-O2` or `-O3` optimization does. But my question is general + I am looking for advice that would cover clang / gcc / XC16. – Jason S Mar 23 '17 at 03:54
  • 1
    @JasonS, I see. My advice would be to avoid to add noise just because you have one compiler that has bad optimization. If you are convinced that these functions *should* be inlined use the C model, that is put the definitions with `inline` in a header (or the start of your .c) and place an "instantiation", a declaration without `inline`, somewhere later. By that you can be sure that this is portable. – Jens Gustedt Mar 23 '17 at 08:03
  • Why is the instantiation necessary @JensGustedt – user129393192 Jul 20 '23 at 15:04
  • @user129393192 You need that for the cases where the compiler can't or wouldn't inline the function at some place or if the address of the function would be used for any reason. – Jens Gustedt Aug 01 '23 at 11:41
  • I recently got a linker error for an `inline` function where there was only one declaration with a definition attached. It couldn't resolve the symbol. Could that be why? @JensGustedt – user129393192 Aug 01 '23 at 14:37
  • @user129393192 yes, most likely – Jens Gustedt Aug 01 '23 at 14:46
1

I want to provide detailed information about inline,

you can separate the declaration and definition fine, but that definition must be available in every translation unit that uses the function, i.e in your case inline static int foo(int x);

Inline functions are included in the ISO C99 standard, but there are currently substantial differences between what GCC implements and what the ISO C99 standard requires.

To declare a function inline, use the inline keyword in its declaration, like this:

static inline int
inc (int *a)
{
  return (*a)++;
}

An Inline Function is As Fast As a Macro

Note that certain usages in a function definition can make it unsuitable for inline substitution.

Note that in C, unlike C++, the inline keyword does not affect the linkage of the function.

danglingpointer
  • 4,708
  • 3
  • 24
  • 42
0

Yes. inline and static should be include. For example, the line of code for the function should be the same in the .h file where you declare, and the .c file where you define (so yes in both cases), but in your main code.c file it should only have the function name when called, so "foo(parameters passed)".

Hope this helps!

  • 1
    What reference would corroborate what you're saying? I think I agree with you personally but I would rather be able to point to some part of the C standard or of a reputable style guideline. – Jason S Mar 22 '17 at 21:27
-1

You only need inline static in the declaration. You can leave it out of the function definition.

7.1.1/6:

A name declared in a namespace scope without a storage-class-specifier has external linkage unless it has internal linkage because of a previous declaration and provided it is not declared const. Objects declared const and not explicitly declared extern have internal linkage.

Nick Frost
  • 490
  • 2
  • 12
  • 1
    Not only that it is not relevant, for C it is just giving the wrong direction. Attaching `inline` to a declaration but not to a definition is quite senseless. – Jens Gustedt Mar 22 '17 at 21:46