972
edits
[unchecked revision] | [unchecked revision] |
(Solution for inlining functions for every optimization option.) |
m (Bot: Replace deprecated source tag with syntaxhighlight) |
||
(3 intermediate revisions by one other user not shown) | |||
Line 1:
In C
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
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.
== Solution 1 ==
'''File:
<
inline long something(long i)
{
return i + 2;
}
</syntaxhighlight>
'''File:
<
#include
extern inline long something(long i);
</syntaxhighlight>
This way, the compiler inlines the function if possible, but additionally
== Solution 2 ==
'''File: example2.h'''
<syntaxhighlight lang="c">
static inline long something(long i)
{
return i + 2;
}
</syntaxhighlight>
This is another solution, adding the static keyword.
[[Category:C]]
|