From 5ae84f274c4e1e9400fbd5d4aaa1d0328644a23e Mon Sep 17 00:00:00 2001
From: John McCall By default, Clang builds C code according to the C99 standard,
-which provides different inlining semantics than GCC's default
-behavior. For example, when compiling the following code with no optimization:C99 inline functions
inline
keyword
+than GCC's default behavior. For example, consider the following
+code:
inline int add(int i, int j) { return i + j; } @@ -89,11 +90,13 @@ int main() { }-
In C99, this is an incomplete (incorrect) program because there is
-no external definition of the add
function: the inline
-definition is only used for optimization, if the compiler decides to
-perform inlining. Therefore, we will get a (correct) link-time error
-with Clang, e.g.:
In C99, inline
means that a function's definition is
+provided only for inlining, and that there is another definition
+(without inline
) somewhere else in the program. That
+means that this program is incomplete, because if add
+isn't inlined (for example, when compiling without optimization), then
+main
will have an unresolved reference to that other
+definition. Therefore we'll get a (correct) link-time error like this:
Undefined symbols: @@ -101,17 +104,31 @@ Undefined symbols: _main in cc-y1jXIr.o+
By contrast, GCC's default behavior follows the GNU89 dialect,
+which is the C89 standard plus a lot of extensions. C89 doesn't have
+an inline
keyword, but GCC recognizes it as an extension
+and just treats it as a hint to the optimizer.
There are several ways to fix this problem:
add
to a static inline
- function. Static inline functions are always resolved within the
- translation unit, so you won't have to add an external, non-inline
- definition of the function elsewhere in your program.add
- somewhere in your program.static
+ inline
functions are always resolved within the translation
+ unit, so you won't have to add a non-inline
definition
+ of the function elsewhere in your program.
+
+ inline
keyword from this definition of
+ add
. The inline
keyword is not required
+ for a function to be inlined, nor does it guarantee that it will be.
+ Some compilers ignore it completely. Clang treats it as a mild
+ suggestion from the programmer.inline
) definition
+ of add
somewhere else in your program. The two
+ definitions must be equivalent!-std=gnu89
to the set of Clang options. This option is
only recommended if the program source cannot be changed or if the
@@ -119,6 +136,8 @@ Undefined symbols:
be changed.All of this only applies to C code; the meaning of inline
+in C++ is very different from its meaning in either GNU89 or C99.