C, inline funtion and GCC -
this question has reply here:
is “inline” without “static” or “extern” ever useful in c99? 3 answersif have:
inline int foo(void) { homecoming 10 + 3; } int main(void) { foo(); }
with gcc file compiles linker returns undefined reference foo
instead if remove inline
linker happy!
it seems identifier of external definition visible linker identifier of inline definition not.
also if compile -o3
flag linker sees identifier of inline definition.
what's problem?
okay, after reading through vivieng's link, think i've understood exact reasoning behind error message. it's confusing , misleading (at to the lowest degree me; shouldn't happen if you've got 1 translation unit), yet possible explain:
assuming compiler doesn't want inline code, has know set function, when it's used in multiple translation units.
classic approach create multiple copies, 1 each translation unit (or @ to the lowest degree units it's used).
this may cause problems, e.g. when trying function pointer comparisons (still leaves question why you'd though).
to counter (and other issues perchance didn't list here), they've thought of quite neat (although - mentioned - in sentiment misleading) solution:
you declare function inline
way know, @ same time tell compiler set non-inline version extern
keyword.
so in example, you'd maintain function as-is , set in header file (so it's known it's going used):
inline int foo(void) { homecoming 10 + 3; }
in addition, tell compiler place non-inlined version, you'll have add together 1 more "forward" declaration in 1 translation unit:
extern inline int foo(void);
so whole concept reversed when compared classic functions: set implementation in header , short declaration in 1 file.
as mentioned already, while using -o3
parameter, code marked inline
inlined, won't cause issue happen.
c
No comments:
Post a Comment