Hi,
I'm trying to understand how to inline functions impemented in a
different source file. Below is an example:
circle.h:
inline float area(float radius);
circle.c:
#include <math.h>
#define PI 3.141592
inline float area(float radius)
{
return pow(PI*radius, 2);
}
main.c:
#include <stdio.h>
#include "circle.h"
int main(void)
{
float a = area(3.3);
printf("Area: %f\n", a);
return 0;
}
Trying to compile with the following command:
gcc-4.6 -O2 circle.c main.c -lm -Winline
Provides the warning:
circle.h:1: sorry, unimplemented: inlining failed in call to
‘area’: function body not available
main.c:5: sorry, unimplemented: called from here
The Resulting code is indeed not inlined.
GCC's handbook (http://gcc.gnu.org/onlinedocs/gcc/Inline.html)
recommends defining the function in the header as 'extern inline':
extern inline float area(float radius);
And compile the implementation in a library. Running:
ar rcs libcircle.a circle.o
And then:
gcc -O2 main.c -lm -Winline -L./ -lcircle
Throws the same warning, and still no inlining.
This was tested this in GCC 4.1.2 ,4.3 and 4.6.1. All with the same
results.
What is the proper way to make GCC inline the function?
Thanks,
Ami