GCC and inlining C functions declared in other files (the "function body not available" problem)
I am heaving a weird issue with inlining functions defined in different files. Consider the following scenario.
in main.c:
#include "inline.h"
int main(void) {
int i = 0;
for (i = 0; i<=100000; i++) {
omfg(i);
}
return 0;
}
in inline.h:
inline int omfg(unsigned int num);
and in inline.c:
#include <stdio.h>
inline int omfg(unsigned int num) {
int i = 0;
for (i = 0; i<= 10; i++) {
printf(".");
num++;
}
return num;
}
When I compile with gcc using something similar to:
$ gcc inline.c main.c -o binary -Wall -Winline -Wextra -O2
I get:
main.c: In function 'main':
inline.h:2: warning: inlining failed in call to 'omfg': function body not available
main.c:7: warning: called from here
What am I doing wrong? Should I de开发者_StackOverflowclare omfg()
in a different way? Its quite puzzling...
Move the implementation to the header file. You can declare the function up front then define it below, or even #include a special file like inline.inl at the bottom of the header to hide it, but fundamentally the function definition needs to be available if it's to be inline.
You must put the implementation of your inline function in the header file if you want this to compile.
精彩评论