Home > Backend Development > C++ > What's the Difference Between `inline`, `static inline`, and `extern inline` in C and C ?

What's the Difference Between `inline`, `static inline`, and `extern inline` in C and C ?

Barbara Streisand
Release: 2024-12-24 17:51:11
Original
265 people have browsed it

What's the Difference Between `inline`, `static inline`, and `extern inline` in C and C  ?

Understanding extern inline

In C programming languages, inline functions offer a hint to the compiler to potentially optimize code by inserting the function body directly at the point of invocation rather than generating a separate subroutine call.

Differences between inline variations

In K&R C and C89, there were no standard semantics for inline, but many compilers supported it with varying interpretations. GCC introduced three variations: inline, static inline, and extern inline.

  • inline: Suggests inline optimization, but an out-of-line version is also generated. Only one definition can exist across compilation units.
  • extern inline: Similar to inline, but no out-of-line version is generated. However, a call to an out-of-line version may be made, which must exist in another compilation unit.
  • static inline: Generates a file-static, but not externally visible, inline version. No out-of-line version is generated.

C99 and GNU99

In C99, the semantics for extern inline were revised to mirror GNU89's inline behavior. Inline and static inline remained the same.

C

In C , inline functions must have the same definition everywhere they are used. Multiple definitions of the same symbol are handled by the compiler/linker. Neither static inline nor extern inline are defined in C , though some compilers may support them.

Ensuring inlining with extern inline

In your specific scenario, where you require inlining for functions using FILE and LINE macros, extern inline does not guarantee that the function will be inlined. However, it is the closest C99 equivalent to achieving this desired behavior. If the function does not get inlined, the compiler or linker may issue an error.

Compiler and vendor differences

Behavior across different compiler vendors and versions can vary, so it's always advisable to consult the specific documentation for your environment.

The above is the detailed content of What's the Difference Between `inline`, `static inline`, and `extern inline` in C and C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template