avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] inline vs. #define


From: David Brown
Subject: Re: [avr-gcc-list] inline vs. #define
Date: Wed, 26 Mar 2003 16:23:07 +0100

> On Wednesday 26 March 2003 01:32 am, David Brown wrote:
>
> > function, just in case another C module wants to use it.  The normal way
to
> > make always-inlinded functions is to declare them "static inline" - then
> > the compiler knows that they will never be used externally, and can
avoid
> > generating the original function.  "Static inline" functions are safe to
> > put in headers too - they are then as small and fast as a macro, with
all
> > the advantages of a function (improved readability, and improved
> > compile-time checking).
>
> I always use "extern inline" in headers.  I seem to remember reading this
in
> the gcc manual somewhere.  I'm guessing it's the same effect as far as
> generated code, but it seems like "static inline" would cause extraneous
> warnings.  If you had a "static inline" in a header file, included it in a
> source file, and never called the function, then I would expect the
compiler
> to give an error about "foo declared static but never referenced."
>
> Anyone know if these are equivalent, or what the preferred way is?
>

Do you mean something like:

/* utils.h */
extern inline int square(int n);

/* utils.c */
#include "utils.h"
inline int square(int n) { return n*n; }
int test(int x) { return square(x); }            /* square is inlined */

/* main.c */
#include "utils.h"
int test2(int x) { return square(x); }          /* square is a normal extern
funciton */

In this case, any functions in utils.c that come after the definition of
square will get inlined versions of square, while any other modules will
call a non-inlined version, that will always be generated in the utils.o
module (whether it is used or not).  This method will work fine even if
inlining is disabled (such as when no -O switch is used).

My method is:

/* utils.h */
static inline int square(int n) { return n * n; }

/* utils.c */
#include "utils.h"
int test(int x) { return square(x); }            /* square is inlined */

/* main.c */
#include "utils.h"
int test2(int x) { return square(x); }          /* square is inlined */


If inlining is disabled, however, then a seperate local normal square
function is generated for every module that includes the utils.h file,
whether it is used or not.  With inlining enabled, there no extra code or
warnings are generated, regardless of whether square is used in a module or
not.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]