Inline?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi freaks,

I want to declare a inline function, but am not sure how to do it, and don't want to use #define.

When looking at some examples I see something like this in a .h file:

extern inline void  __attribute__((always_inline)) someFunction(u08 x)
{
........
}

But I don't want to write code in the .h file.

What if I want to use the "inline" function only in the file where it is declared?

What is the difference between inline, static inline and extern inline?

I've seen that it is necessary to provide a addressable variant of the function as well, is this true?

Thanks for the response.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
What is the difference between inline, static inline and extern inline?

inline directs the code of the function to be placed inline at the point where it is called so there is no CALL/RET overhead. However the compiler (depends which one in fact) will probably generate TWO copies of the code. In this same file, where it is called it will be inlined but there'll be a second copy with a RET in case code in another C file also calls the same function. The compiler cannot know if this is required at the compile time for this one .c file as such a cross-reference will only be fixed up at link time.

static inline (like all uses of static outside a function body) is defining the scope of the function. It's saying that it is ONLY used in this one .c file. As such the compiler knows it does not need to generate the second, callable copy.

extern inline is just the .h definition of an inline function so that the API can be passed to a secondary caller.

It sounds to me (if you only plan to call the code in the .c where it appears) that you most definitely want to be using static inline and forget the non-static or extern variants

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are three or four popular c compilers for the AVR. I'll bet you a coke that you are talking about the gcc compiler, which has ITS OWN FORUM for questions.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually Bob you are quite right (so my points whch were GCC based above ARE the way it is). The clue was in the use of __attribute__ above.

As such, over to GCC this goes...