1

I recently encountered a codebase that favors the use of macros over extern inline functions. The language used here is C99, and the specific implementation is particularly performance sensitive (and so the argument given for the use of macro's is to avoid function calls / stack frame setup for frequently invoked functions).

As an improvement to readability, I've suggested that macro's be removed in favor of extern inline functions. For example, instead of:

#define FooBar(_AnArg, _AnotherArg) \ { \ if (_AnotherArg) \ { \ ... \ } \ else \ { \ ... \ } \ } 

I'm suggesting:

extern inline void FooBar(void* anArg, bool anotherArg) { ... } 

Looking over the generated assembly my function appears to be properly inlined and is therefore just as efficient as the macro while being easier to read and debug.

Is there any consideration of function inlining I am overlooking? Are there other benefits to macro use that I am not considering?

7
  • It depends on what they do. Commented Mar 4, 2020 at 18:57
  • Also, dont use extern inline. Define it inline and extern it normally Commented Mar 4, 2020 at 18:58
  • inline functions can access static variables in the file they are defined in, and called in other files. Macros can't Commented Mar 4, 2020 at 19:00
  • I just say use inline functions if you need to access static variables from another file Commented Mar 4, 2020 at 19:02
  • 1
    @Asadefa: What does “extern it normally” mean? Why make an inline function extern at all instead of static? Commented Mar 4, 2020 at 19:14

1 Answer 1

1

extern inline functions will cause to emit external symbols. Macros are only visible from the point they were defined. If you put extern inline function in a header file and two .c source files include that header, then your linker should complain with a "multiple definitions of the same function" error.

Use static inline for replacing macros.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.