Inline Functions in C: Difference between revisions

m
Remove from 'Languages' category. Fix 'at least with GCC' position. Change type of include.
[unchecked revision][unchecked revision]
(Solution for inlining functions for every optimization option.)
 
m (Remove from 'Languages' category. Fix 'at least with GCC' position. Change type of include.)
Line 1:
In C (at least with GCC) you can define inline functions for optimization purposes.
The problem is that the linker will fail (at least with GCC, with an undefined reference error) if no optimization option (e.g. -O2) is given to the compiler.
That is because by default, with no optimization, the compiler doesn't inline functions, and as the compiler doesn't create code for them, the linker won't find them.
Here is a possible solution that does not require defining the functions twice, and works for every optimization level. This is done by defining them in the header and declaring them as extern inline in the implementation file.
Line 16:
'''File: example.c'''
<source lang="c">
#include <"example.h>"
extern inline long something(long i);
</source>
Line 23:
 
[[Category:C]]
[[Category:Languages]]
16

edits